1. 程式人生 > >[JLOI2010]冠軍調查 BZOJ2768 最小割

[JLOI2010]冠軍調查 BZOJ2768 最小割

個人 type lse 學院 ostream deque 聯賽 說明 ring

題目描述

一年一度的歐洲足球冠軍聯賽已經進入了淘汰賽階段。隨著衛冕冠軍巴薩羅那的淘汰,英超勁旅切爾西成為了頭號熱門。

新浪體育最近在吉林教育學院進行了一次大規模的調查,調查的內容就是關於切爾西能否在今年問鼎歐洲冠軍。新浪體育的記者從各個院系中一共抽取了n位同學作為參與者,大家齊聚一堂,各抒己見。每一位參與者都將發言,闡述自己的看法。

參與者的心裏都有一個看法,比如FireDancer認為切爾西不可能奪冠,而WaterDancer認為切爾西一定問鼎。但是因為WaterDancer是FireDancer的好朋友,所以可能FireDancer為了遷就自己的好朋友,會在發言中支持切爾西。也就是說每個參與者發言時闡述的看法不一定就是心裏所想的。

現在告訴你大家心裏的想法和參與者的朋友網,希望你能安排每個人的發言內容,使得違心說話的人的總數與發言時立場不同的朋友(對)的總數的和最小。

輸入輸出格式

輸入格式:

第一行兩個整數nnn和mmm,其中nnn(2≤n≤3002≤n≤3002n300)表示參與者的總數,mmm($0≤m≤frac{1}{2} {n(n-1)$)表示朋友的總對數。

第二行nnn個整數,要麽是000要麽是111。如果第iii個整數的值是000的話,表示第iii個人心裏認為切爾西將與冠軍無緣,如果是111的話,表示他心裏認為切爾西必將奪魁。

下面mmm行每行兩個不同的整數,iii和jjj(1≤i,j≤n1≤i, j≤n1i,jn)表示i和j是朋友。註意沒有一對朋友會在輸入中重復出現。朋友關系是雙向的,並且不會傳遞。

輸出格式:

只有一個整數,為最小的和。

輸入輸出樣例

輸入樣例#1: 復制
3 3
1 0 0
1 2
1 3
2 3
輸出樣例#1: 復制
1

說明

最好的安排是所有人都在發言時說切爾西不會奪冠。這樣沒有一對朋友的立場相左,只有第1個人他違心說了話。

吐槽一句:和bzoj 1934一模一樣。。。。

考慮建圖:

我們將兩種意見分為st,ed;

同意的和st相連,不同意的和ed相連;

如果由兩個人是好朋友,那麽連邊(雙向邊);

原因:

我們求的最小割意思就是使這兩個集合不聯通,但是矛盾必須最小(最小割);

那麽顯然最大流=最小割;直接跑一遍dinic即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == ‘-‘) f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/



ll qpow(ll a, ll b, ll c) {
	ll ans = 1;
	a = a % c;
	while (b) {
		if (b % 2)ans = ans * a%c;
		b /= 2; a = a * a%c;
	}
	return ans;
}

int n, m;
int st, ed;
struct node {
	int u, v, nxt, w;
}edge[maxn<<2];

int head[maxn], cnt;
void addedge(int u, int v, int w) {
	edge[cnt].u = u; edge[cnt].v = v; edge[cnt].w = w;
	edge[cnt].nxt = head[u]; head[u] = cnt++;
}
int rk[maxn];

int bfs() {
	queue<int>q;
	ms(rk);
	rk[st] = 1; q.push(st);
	while (!q.empty()) {
		int tmp = q.front(); q.pop();
		for (int i = head[tmp]; i != -1; i = edge[i].nxt) {
			int to = edge[i].v;
			if (rk[to] || edge[i].w <= 0)continue;
			rk[to] = rk[tmp] + 1; q.push(to);
		}
	}
	return rk[ed];
}

int dfs(int u, int flow) {
	if (u == ed)return flow;
	int add = 0;
	for (int i = head[u]; i != -1; i = edge[i].nxt) {
		int v = edge[i].v;
		if (rk[v] != rk[u] + 1 || !edge[i].w)continue;
		int tmpadd = dfs(v, min(edge[i].w, flow - add));
		if (!tmpadd) { rk[v] = -1; continue; }
		edge[i].w -= tmpadd; edge[i ^ 1].w += tmpadd; add += tmpadd;
	}
	return add;
}

int ans;
void dinic() {
	while (bfs())ans += dfs(st, inf);
}



int main()
{
	//ios::sync_with_stdio(0);
	memset(head, -1, sizeof(head));
	rdint(n); rdint(m); st = n + 1; ed = st + 1;
	for (int i = 1; i <= n; i++) {
		int x; rdint(x);
		if (x == 0) {
			addedge(st, i, 1); addedge(i, st, 0);
		}
		else {
			addedge(i, ed, 1); addedge(ed, i, 0);
		}
	}
	for (int i = 1; i <= m; i++) {
		int a, b; rdint(a); rdint(b);
		addedge(a, b, 1); addedge(b, a, 1);
	}
	dinic();
	cout << ans << endl;
	return 0;
}

[JLOI2010]冠軍調查 BZOJ2768 最小割