网络流相关(拓扑)CodeForces 269C:Flawed Flow
2016-10-02 22:13
501 查看
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet — it calculates the maximum flow in an undirected graph. The graph consists of n vertices and m edges. Vertices are numbered from 1 to n. Vertices 1 and n being the source and the sink respectively.
However, his max-flow algorithm seems to have a little flaw — it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow.
More formally. You are given an undirected graph. For each it's undirected edge (ai, bi) you are given the flow volume ci. You should direct all edges in such way that the following conditions hold:
for each vertex v (1 < v < n), sum of ci of incoming edges is equal to the sum of ci of outcoming edges;
vertex with number 1 has no incoming edges;
the obtained directed graph does not have cycles.
It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists.
If there are several solutions you can print any of them.
Output
Input
Output
However, his max-flow algorithm seems to have a little flaw — it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow.
More formally. You are given an undirected graph. For each it's undirected edge (ai, bi) you are given the flow volume ci. You should direct all edges in such way that the following conditions hold:
for each vertex v (1 < v < n), sum of ci of incoming edges is equal to the sum of ci of outcoming edges;
vertex with number 1 has no incoming edges;
the obtained directed graph does not have cycles.
Input
The first line of input contains two space-separated integers n and m (2 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105), the number of vertices and edges in the graph. The following m lines contain three space-separated integers ai, bi and ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 104), which means that there is an undirected edge from ai to bi with flow volume ci.It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists.
Output
Output m lines, each containing one integer di, which should be 0 if the direction of the i-th edge is ai → bi (the flow goes from vertex ai to vertex bi) and should be 1 otherwise. The edges are numbered from 1 to m in the order they are given in the input.If there are several solutions you can print any of them.
Sample Input
Input3 3 3 2 10 1 2 10 3 1 5
Output
1 0 1
Input
4 5 1 2 10 1 3 10 2 3 5 4 2 15 3 4 5
Output
0 0 1 1 0 可以发现这里有拓扑性质,可以直接做,O(N)复杂度。
#include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; const int N=200010,M=400010; int cnt=1,fir ,nxt[M],to[M],cap[M]; int n,m,in ,vis ,ans ;queue<int>q; void addedge(int a,int b,int c){ nxt[++cnt]=fir[a]; to[fir[a]=cnt]=b; cap[cnt]=c; } int main(){ scanf("%d%d",&n,&m); for(int i=1,a,b,c;i<=m;i++){ scanf("%d%d%d",&a,&b,&c); addedge(a,b,c);addedge(b,a,c); in[a]+=c;in[b]+=c; } for(int i=2;i<n;i++)in[i]/=2; q.push(1);in[1]=0;vis[1]=1; while(!q.empty()){ int x=q.front();q.pop(); for(int i=fir[x];i;i=nxt[i]) if(!vis[to[i]]){ in[to[i]]-=cap[i]; ans[i/2]=i%2; if(in[to[i]]==0){ q.push(to[i]); vis[to[i]]=1; } } } for(int i=1;i<=m;i++) printf("%d\n",ans[i]); return 0; }
相关文章推荐
- CodeForces 269C Flawed Flow 拓扑
- CodeForces - 269C Flawed Flow
- codeforces 269C Flawed Flow(网络流)
- Codeforces 903G Yet Another Maxflow Problem 线段树+网络流
- 网络流与dinic/Fulkerson模板以及相关题
- 网络流相关算法总结,最大流EK算法,SAP算法,最小费用最大流,最小费用路算法,最大流最小割定理
- 拓扑相关的封闭曲面的空间划分问题
- codeforces 498C Array and Operations 网络流
- Codeforces 513F1 513F2 Scaygerboss 网络流
- 网络流(费用流)CodeForces 321B:Ciel and Duel
- codeforces 687B - Remainders Game 数学相关(互质中国剩余定理)
- 【数学相关、规律】Codeforces 707C Pythagorean Triples
- 【笔记】二分图、网络流相关
- storm IDEA maven 打相关依赖jar包---用于提交拓扑
- 【网络流】codeforces 434D
- codeforces 871C. Points, Lines and Ready-made Titles (与图相关的计数)
- !codeforces 558C Amr and Chemistry-yy题-(位运算相关)
- Hadoop 网络拓扑相关配置脚本 topology.script.file.name
- 【Codeforces 808F】【网络流】Card Game 题解