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. NodeJ Koa2 安装使用 reeber

    介绍 Koa 是由 Express 原班人马打造的,致力于成为一个更小.更富有表现力.更健壮的 Web 框架. 使用 koa 编写 web 应用,通过组合不同的 generator,可以免除重复繁琐的 ...

  2. 【Django笔记三】Django2.0配置mysql模型

    一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 Mysql版本: 5.5.53   安装mysql 二.安装Mysqlclient: 1. ...

  3. POJ 3528--Ultimate Weapon(三维凸包)

    Ultimate Weapon Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 2430   Accepted: 1173 ...

  4. Mysql的TIMESTAMPDIFF和TIMESTAMPADD的用法

    [1.]TIMESTAMPDIFF(interval,colum1,colum2) 字段类型:date或者datetime 计算过程:colum2减去colum1,即后面的减去前面的 计算结果:整数 ...

  5. MySQL数据库修改数据表类型(引擎)的方法

    MySQL数据库使用事务,相关数据表必须为InnoDB引擎 查看数据表状态: SHOW TABLE STATUS FROM wawa WHERE NAME='ww_invite_code_temp'; ...

  6. Vue——关于css过渡和动画那些事

    1. 单元素/组件的过渡transition Vue 提供了 transition 的封装组件,在下列情形中,可以给任何元素和组件添加进入/离开过渡 条件渲染 (使用 v-if) 条件展示 (使用 v ...

  7. swoole学习(二)----搭建server和client

    1.搭建server 1.1搭建server.php 1.搭建websocket服务器,首先建立 server.php 文件, <?php $server = new swoole_websoc ...

  8. Hadoop原理之——HDFS原理

    Hadoop 3个核心组件: 分布式文件系统:Hdfs——实现将文件分布式存储在很多的服务器上(hdfs是一个基于Linux本地文件系统上的文件系统) 分布式运算编程框架:Mapreduce——实现在 ...

  9. Java核心技术36讲----------谈谈final、finally、finalize有什么不同

    一.final 1.final修饰方法时,需要注意的点: #final修饰方法时,之前的第二个原因是效率.但是如果方法过于庞大,可能看不到内嵌调用带来的任何性能提升.在最近的Java版本中,不需要使用 ...

  10. hiveserver2不能启动

    我的hiveserver2一直不能启动,命令行一直卡住不动,然后我就想是不是配置文件没有配置相关的参数,然后就来修改hive-site.xml 最终修改完后的hive-site.xml: <?x ...