网络流相关(拓扑)CodeForces 269C:Flawed Flow

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 ofn vertices andm edges. Vertices are numbered from 1 ton. Vertices1 andn 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 volumeci. You should direct all edges in such way that the following conditions hold:

  1. for each vertexv(1 < v < n), sum ofci of incoming edges is equal to the sum ofci of outcoming edges;
  2. vertex with number1 has no incoming edges;
  3. the obtained directed graph does not have cycles.

Input

The first line of input contains two space-separated integersn andm (2 ≤ n ≤ 2·105,n - 1 ≤ m ≤ 2·105), the number of vertices and edges in the graph. The followingm lines contain three space-separated integersai,bi andci (1 ≤ ai, bi ≤ n,ai ≠ bi,1 ≤ ci ≤ 104), which means that there is an undirected edge fromai tobi with flow volumeci.

It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists.

Output

Outputm lines, each containing one integerdi, which should be 0 if the direction of thei-th edge isai → bi (the flow goes from vertexai to vertexbi) and should be 1 otherwise. The edges are numbered from 1 tom 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 <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 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<int>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<n;i++)in[i]/=2;
22     q.push(1);in[1]=0;vis[1]=1;
23     while(!q.empty()){
24         int x=q.front();q.pop();
25         for(int i=fir[x];i;i=nxt[i])
26             if(!vis[to[i]]){
27                 in[to[i]]-=cap[i];
28                 ans[i/2]=i%2;
29                 if(in[to[i]]==0){
30                     q.push(to[i]);
31                     vis[to[i]]=1;
32                 }
33             }
34     }
35     for(int i=1;i<=m;i++)
36         printf("%d\n",ans[i]);
37     return 0;    
38 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是原来的你吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值