网络流相关(拓扑)CodeForces 269C:Flawed Flow
发布日期:2022-03-13 05:36:12 浏览次数:22 分类:技术文章

本文共 2630 字,大约阅读时间需要 8 分钟。

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:

 

  1. for each vertex v (1 < v < n), sum of ci of incoming edges is equal to the sum of ci of outcoming edges;
  2. vertex with number 1 has no incoming edges;
  3. 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

Input
3 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)复杂度。
1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 const int N=200010,M=400010; 7 int cnt=1,fir[N],nxt[M],to[M],cap[M]; 8 int n,m,in[N],vis[N],ans[N];queue
q; 9 void addedge(int a,int b,int c){10 nxt[++cnt]=fir[a];11 to[fir[a]=cnt]=b;12 cap[cnt]=c;13 }14 int main(){15 scanf("%d%d",&n,&m);16 for(int i=1,a,b,c;i<=m;i++){17 scanf("%d%d%d",&a,&b,&c);18 addedge(a,b,c);addedge(b,a,c);19 in[a]+=c;in[b]+=c;20 }21 for(int i=2;i

 

转载于:https://www.cnblogs.com/TenderRun/p/5928230.html

转载地址:https://blog.csdn.net/weixin_30323961/article/details/98232006 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring Cloud Vault介绍
下一篇:删除.svn文件

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年03月24日 02时54分37秒