Flawed Flow
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
Input
The first line of input contains two space-separated integers n and m (2 ≤ n ≤ 2·10^5, n - 1 ≤ m ≤ 2·10^5), 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 ≤ 10^4), 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.
Examples
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
Tips
题意:
给定一个
n
n
n 顶带权无向图,指定
1
1
1 和
n
n
n 分别为源点和汇点,请你根据流量守恒原则(除源点和汇点外流入等于流出)确定每条边的方向。
题解:
最后的图中无环、且每条边都被分配了方向,因此是DAG,可用拓扑排序解决。
首先,初始化工作做的是,储存正反两条边进去,并用一个数组flow
保存各个结点的流经量,此后欲平衡某结点的流量,则在此基础上减去一个二倍的边权,相当于是正号变负号。则结点
i
i
i 的流量守恒,当且流经量flow[i]
为
0
0
0 。
接着,从源点出发,向汇点进行宽搜,这一路上,用队列维护的结点都是流量不守恒的结点。因此只要遇到这个结点,就用一条边去平衡它,同时,将这个边的反向边去除。这样能确定一条边的方向,把这个方向存进数组dirc
中,如此循环往复,直到完成宽搜。
最后,按序输出dirc
数组中的值即可。
Reference Code
#include <cstdio>
#include <cstring>
#include <queue>
using std::queue;
const int MAXN=2e5+10;
struct Edge{
int id,to,next,w;
bool d,flag;
}edge[MAXN<<1];
int tot;
int head[MAXN<<1];
void init(){
tot=0;
memset(head,-1,sizeof(head));
}
void addedge(int id,int u,int v,int w){
edge[tot]=(Edge){id,v,head[u],w,0,1};
head[u]=tot++;
edge[tot]=(Edge){id,u,head[v],w,1,1};
head[v]=tot++;
}
bool vis[MAXN],dirc[MAXN];
int flow[MAXN];
int n,m,a,b,c;
int main(){
init();
scanf("%d%d",&n,&m);
for (int i=0;i<m;++i){
scanf("%d%d%d",&a,&b,&c);
addedge(i,a,b,c);
flow[a]+=c;
flow[b]+=c;
}
queue<int> Q;
Q.push(1);
while (!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=true;
for (int i=head[u];~i;i=edge[i].next){
Edge &e=edge[i];
if (!e.flag) continue;
int v=e.to;
if (vis[v]) continue;
flow[v]-=2*e.w;
dirc[e.id]=e.d;
edge[i^1].flag=false;
if (!flow[v]&&v!=n)
Q.push(v);
}
}
for (int i=0;i<m;++i){
printf("%d\n",dirc[i]);
}
return 0;
}