Codeforces 269C Flawed Flow

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:

  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·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;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值