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 (aibi) 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 aibi and ci (1 ≤ ai, bi ≤ nai ≠ 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.

题目大意:给出一张网络流构成的图,给出每对点之间的流量,求流的方向。

思路:直接套算法求网络流必须超时,注意到每个点的流入=流出,而流入+流出可以从给的数据中求出,那么流入等于总流量的一半,利用拓扑排序的思路即可在O(n+m)的时间内求出解。

 #include <cstdio>
#include <cctype>
#include <stack> const int MAXN = ; int n, m, ecnt;
int a[MAXN], b[MAXN], inflow[MAXN], outflow[MAXN], direct[MAXN];
int head[MAXN], to[MAXN*], next[MAXN*], c[MAXN*], from[MAXN*]; inline int readint(){
char c = getchar();
while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)){
x = x * + c - '';
c = getchar();
}
return x;
} inline void addEdge(int &u, int &v, int &i){
int x = readint();
outflow[u] += x; outflow[v] += x;
to[ecnt] = v; c[ecnt] = x; from[ecnt] = i;
next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; c[ecnt] = x; from[ecnt] = i;
next[ecnt] = head[v]; head[v] = ecnt++;
} void makeDirect(){
std::stack<int> st;
st.push(); outflow[] = ;
while(!st.empty()){
int u = st.top(); st.pop();
for(int p = head[u]; p; p = next[p]){
int &v = to[p];
if(inflow[v] == outflow[v] && v != n) continue;
inflow[v] += c[p]; outflow[v] -= c[p];
if(inflow[v] == outflow[v] && v != n) st.push(v);
int x = from[p];
if(v == a[x]) direct[x] = ;
}
}
} int main(){
scanf("%d%d",&n,&m);
ecnt = ;
for(int i = ; i < m; ++i){
a[i] = readint();
b[i] = readint();
addEdge(a[i], b[i], i);
}
makeDirect();
for(int i = ; i < m; ++i) printf("%d\n", direct[i]);
}

codeforces 269C Flawed Flow(网络流)的更多相关文章

  1. CodeForces - 269C Flawed Flow

    http://codeforces.com/problemset/problem/269/C 题目大意: 给定一个边没有定向的无法增广的残量网络且1是源点,n是汇点,给定每条边中的流.  让你把所有边 ...

  2. Codeforces 270E Flawed Flow 网络流问题

    题意:给出一些边,给出边的容量.让你为所有边确定一个方向使得流量最大. 题目不用求最大流, 而是求每条边的流向,这题是考察网络流的基本规律. 若某图有最大,则有与源点相连的边必然都是流出的,与汇点相连 ...

  3. Codeforces 269C Flawed Flow (看题解)

    我好菜啊啊啊.. 循环以下操作 1.从队列中取出一个顶点, 把哪些没有用过的边全部用当前方向. 2.看有没有点的入度和 == 出度和, 如果有将当前的点加入队列. 现在有一个问题就是, 有没有可能队列 ...

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

    Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious prog ...

  5. Codeforces 1045A Last chance 网络流,线段树,线段树优化建图

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1045A.html 题目传送们 - CF1045A 题意 你有 $n$ 个炮,有 $m$ 个敌人,敌人排成一 ...

  6. codeforces gym 100357 J (网络流)

    题目大意 有n种物品,m种建筑,p个人. n,m,p∈[1,20] 每种建筑需要若干个若干种物品来建造.每个人打算建造一种建筑,拥有一些物品. 主角需要通过交易来建造自己的建筑,交易的前提是对方用多余 ...

  7. @codeforces - 708D@ Incorrect Flow

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个有源点与汇点的图 G,并对于每一条边 (u, v) 给定 ...

  8. codeforces 653D. Delivery Bears 网络流

    题目链接 我们二分每个人携带的数量, 然后每个边的容量就相当于min(权值/二分的值, x). x是人的数量. 然后判断是否满流就可以. 这么裸的网络流为竟然没看出来. 注意写fsbs(r-l)> ...

  9. Codeforces Round #165 (Div. 2)

    C. Magical Boxes 问题相当于求\[2^p \gt \max{a_i \cdot 2^{k_i}},p \gt k_i\] D. Greenhouse Effect \(dp(i,j)\ ...

随机推荐

  1. 『ACM C++』 PTA 天梯赛练习集L1 | 007-011

    真的是忙头晕了,学业.ACM打题.班级活动.自学新东西,哇这充实的大学~ ------------------------------------------------L1-007--------- ...

  2. mysql 8.0.12 日常出错

    最近不知道怎么回事,数据库老是会输出一个: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and co ...

  3. 利用SoapUI 测试web service的一些问题总结

    总结两个利用SoapUI 测试web service的一些问题: 1.请求一个soap service 请求的时候:按照下面的配置输入请求地址后, 2.根据实际service接口的需要,传入相应的参数 ...

  4. python__PIP : 安装第三方库

    pipy国内镜像目前有: http://pypi.douban.com/  豆瓣 http://pypi.hustunique.com/  华中理工大学 http://pypi.sdutlinux.o ...

  5. vim ,vi总是卡死,终于找到原因了。

    玩了这么多年linux 居然不知道这个..特此记录. 使用vim时,如果你不小心按了 Ctrl + s后,你会发现不能输入任何东西了,像死掉了一般,其实vim并没有死掉,这时vim只是停止向终端输出而 ...

  6. 农民工自学java到找到工作的前前后后

    我是一名地地道道的农民工,生活在经济落后的农村,有一个哥哥和一个弟弟,父母都是地道的农民,日出而作,日落而息,我从小到大学习一直很好,从小学到高一都,成绩在全级一直名列前茅,这样我也顺利了考上省的重点 ...

  7. 小程序开发-11-Promise正确用法与函数签名设计技巧

    配置taBar "tabBar": { "selectedColor": "#000000", "backgroundColor& ...

  8. Spring Cloud 分布式事务管理

    Spring Cloud 分布式事务管理 在微服务如火如荼的情况下,越来越多的项目开始尝试改造成微服务架构,微服务即带来了项目开发的方便性,又提高了运维难度以及网络不可靠的概率. Spring Clo ...

  9. 北京Uber优步司机奖励政策(3月10日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. Mac下node.js安装与卸载

    安装: 访问 http://nodejs.org/ 进入官网,下载 Mac 版本的 node.js,双击打开安装即可. 通过终端输入命令 node -v 验证 node 是否安装正确:npm -v 验 ...