Codeforces 547 D. Mike and Fish
2015-08-02 14:53
465 查看
http://codeforces.com/contest/547/problem/D
还是太弱了,怎么都想不出来,最后看了标签贴着 graphs,感觉有了点方向,想到了网络流。。。最近做好多题都需要标签指路。
源点S和横坐标连线,S->x , cnt 是横坐标为x的点数,则这条边的下界是 cnt / 2,上界是 (cnt + 1) / 2 ,纵坐标与汇点T相连。对于点(x,y) , 连一条x -> y 的边,下界0,上界1。
然后就是求有源汇的有上下界的可行流。
还是太弱了,怎么都想不出来,最后看了标签贴着 graphs,感觉有了点方向,想到了网络流。。。最近做好多题都需要标签指路。
源点S和横坐标连线,S->x , cnt 是横坐标为x的点数,则这条边的下界是 cnt / 2,上界是 (cnt + 1) / 2 ,纵坐标与汇点T相连。对于点(x,y) , 连一条x -> y 的边,下界0,上界1。
然后就是求有源汇的有上下界的可行流。
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i,n) for(int i=0; i mpx,mpy; void edge(int a,int b,int c) { v[tol] = b; f[tol] = c; nxt[tol] = fst[a]; fst[a] = tol++; v[tol] = a; f[tol] = 0; nxt[tol] = fst[b]; fst[b] = tol++; } int cur[N<<2],d[N<<2]; bool vis[N<<2]; bool BFS() { mst(vis,0); queue Q; Q.push(SS); d[SS] = 0; vis[SS] = 1; while(!Q.empty()) { int u = Q.front();Q.pop(); for(int e=fst[u]; ~e; e=nxt[e]) if(!vis[v[e]] && f[e] > 0) { vis[v[e]] = 1; d[v[e]] = d[u] + 1; Q.push(v[e]); } } return vis[TT]; } int DFS(int u,int a) { if(u == TT || a == 0) return a; int flow = 0,tmpf; for(int& e=cur[u]; ~e; e=nxt[e]) if(d[v[e]] == d[u] + 1) { if((tmpf = DFS(v[e],min(a,f[e]))) > 0) { f[e] -= tmpf; f[e^1] += tmpf; flow += tmpf; a -= tmpf; if(a == 0) break; } } return flow; } int Maxflow() { int flow = 0; while(BFS()) { memcpy(cur,fst,sizeof(fst)); flow += DFS(SS,INF); } return flow; } int main() { scanf("%d",&n); for(int i=0; i> 1; int high = cnt_x[X[i]] - low; edge(S,i+1,high-low); edge(S,TT,low); edge(SS,i+1,low); } for(int i=0; i<_Y; i++) { int low = cnt_y[Y[i]] >> 1; int high = cnt_y[Y[i]] - low; edge(i+_X+1,T,high-low); edge(i+_X+1,TT,low); edge(SS,T,low); } edge(T,S,INF); Maxflow(); for(int i=0; i
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Gym100571A Cursed Query
- Codeforces Div2 244AB
- Coder-Strike 2014 - Finals (online edition, Div. 2)AB
- Codeforces Div2 243AB
- Codeforces Div2 242AB