CF 269 E Flawed Flow,269flawed
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 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 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 aito 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 test(s)input
3 33 2 101 2 103 1 5
output
101
input
4 51 2 101 3 102 3 54 2 153 4 5
output
00110
Note
In the first test case, 10 flow units pass through path , and 5 flow units pass directly from source to sink: .
給出網路流的無向圖幾流量,問你邊的方向。首先設所有點的flow[i][0]代表流出的方向。
可以想到如果一個點滿足:flow[i][[0]==flow[i][1]即可確定方向了。
BFS做法:從已知不斷BFS,直到一個點的:flow[i][[0]==flow[i][1],加入到隊列中。。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=2*(1e5+100);struct node{ int u,v,w; int num,next;}e[maxn<<1];int vis[maxn],head[maxn];int pre[maxn],s[maxn];int flow[maxn][2];int n,m,cnt;void add(int x,int y,int z,int n){ e[cnt].u=x;e[cnt].v=y; e[cnt].w=z;e[cnt].num=n; e[cnt].next=head[x]; head[x]=cnt++;}void BFS(){ CLEAR(vis,0); queue<int>q; q.push(1); vis[1]=1; while(!q.empty()) { int st=q.front(); q.pop(); for(int i=head[st];i!=-1;i=e[i].next) { int v=e[i].v; int num=e[i].num; int w=e[i].w; if(vis[v]) continue; s[num]=st;//記錄邊起始點判斷方向 flow[v][1]+=w; flow[v][0]-=w; if(v!=n&&flow[v][1]==flow[v][0]) { vis[v]=1; q.push(v); } } }}int main(){ int x,y,z; std::ios::sync_with_stdio(false); while(cin>>n>>m) { CLEAR(head,-1); CLEAR(flow,0); cnt=0; REPF(i,1,m) { cin>>x>>y>>z; pre[i]=x; add(x,y,z,i); add(y,x,z,i); flow[x][0]+=z; flow[y][0]+=z; } BFS(); REPF(i,1,m) { if(pre[i]==s[i]) puts("0"); else puts("1"); } } return 0;}