【BZOJ】1934: [Shoi2007]Vote 善意的投票（网络流/-二分图匹配）

http://www.lydsy.com/JudgeOnline/problem.php?id=1934

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=310, M=N*N/2, oo=~0u>>1;
int ihead[N], cnt=1, n, m, ly[N], x[N], cx, y[N], cy, vis[N];
struct ED { int from, to, cap, next; } e[M];
inline void add(const int &u, const int &v) {
}
const bool ifind(const int &x) {
vis[x]=1; int y;
for(int i=ihead[x]; i; i=e[i].next) if(!vis[y=e[i].to]) {
vis[y]=1;
if(!ly[y] || ifind(ly[y])) {
ly[y]=x;
return true;
}
}
return false;
}
int main() {
int t, ans=0;
for1(i, 1, n) {
if(t) x[++cx]=i;
else y[++cy]=i;
}
for1(i, 1, cx) {
CC(vis, 0);
if(ifind(x[i])) ++ans;
}
print(ans);
return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=320, M=N*N*2, oo=~0u>>1;
int ihead[N], cnt=1, cur[N], gap[N], d[N], p[N], n, m;
struct ED { int from, to, cap, next; } e[M];
inline void add(const int &u, const int &v, const int &w) {
}
int isap(const int &s, const int &t, const int &n) {
int ret=0, i, f, u=s;
gap[0]=n;
while(d[s]<n) {
for(i=cur[u]; i; i=e[i].next) if(e[i].cap && d[u]==d[e[i].to]+1) break;
if(i) {
p[e[i].to]=cur[u]=i; u=e[i].to;
if(u==t) {
for(f=oo; u!=s; u=e[p[u]].from) f=min(f, e[p[u]].cap);
for(u=t; u!=s; u=e[p[u]].from) e[p[u]].cap-=f, e[p[u]^1].cap+=f;
ret+=f;
}
}
else {
if(! (--gap[d[u]]) ) break;
for(i=ihead[u]; i; i=e[i].next) if(e[i].cap && d[u]>d[e[i].to]+1) d[u]=d[e[i].to]+1;
++gap[d[u]];
if(u!=s) u=e[p[u]].from;
}
}
return ret;
}
int main() {
int s=n+10, t=s+1, tp;
for1(i, 1, n) {
}
for1(i, 1, m) {
int t1=getint(), t2=getint();
}
print(isap(s, t, t+1));
return 0;
}


3 3
1 0 0
1 2
1 3
3 2

1

Source

posted @ 2014-08-25 09:49  iwtwiioi  阅读(310)  评论(5编辑  收藏