codeforces 269C Flawed Flow(网络流)
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.
 
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 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(网络流)的更多相关文章
- CodeForces - 269C  Flawed Flow
		
http://codeforces.com/problemset/problem/269/C 题目大意: 给定一个边没有定向的无法增广的残量网络且1是源点,n是汇点,给定每条边中的流. 让你把所有边 ...
 - Codeforces 270E Flawed Flow 网络流问题
		
题意:给出一些边,给出边的容量.让你为所有边确定一个方向使得流量最大. 题目不用求最大流, 而是求每条边的流向,这题是考察网络流的基本规律. 若某图有最大,则有与源点相连的边必然都是流出的,与汇点相连 ...
 - Codeforces 269C Flawed Flow (看题解)
		
我好菜啊啊啊.. 循环以下操作 1.从队列中取出一个顶点, 把哪些没有用过的边全部用当前方向. 2.看有没有点的入度和 == 出度和, 如果有将当前的点加入队列. 现在有一个问题就是, 有没有可能队列 ...
 - 网络流相关(拓扑)CodeForces 269C:Flawed Flow
		
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious prog ...
 - Codeforces 1045A Last chance 网络流,线段树,线段树优化建图
		
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1045A.html 题目传送们 - CF1045A 题意 你有 $n$ 个炮,有 $m$ 个敌人,敌人排成一 ...
 - codeforces gym 100357 J (网络流)
		
题目大意 有n种物品,m种建筑,p个人. n,m,p∈[1,20] 每种建筑需要若干个若干种物品来建造.每个人打算建造一种建筑,拥有一些物品. 主角需要通过交易来建造自己的建筑,交易的前提是对方用多余 ...
 - @codeforces - 708D@ Incorrect Flow
		
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个有源点与汇点的图 G,并对于每一条边 (u, v) 给定 ...
 - codeforces 653D. Delivery Bears   网络流
		
题目链接 我们二分每个人携带的数量, 然后每个边的容量就相当于min(权值/二分的值, x). x是人的数量. 然后判断是否满流就可以. 这么裸的网络流为竟然没看出来. 注意写fsbs(r-l)> ...
 - 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)\ ...
 
随机推荐
- JavaScript数组处理方法
			
JavaScript中创建数组有两种方式 (一)使用 Array 构造函数: var arr1 = new Array(); //创建一个空数组 var arr2 = new Array(20); / ...
 - C++ map练习
			
C++ STL之map map介绍 C++里的map数据结构,会存储键值对信息key-value,通过key得到value的信息.map的key与value有一个特点就是:每个唯一的key拥有唯一对应 ...
 - RCF的简单使用教程以及什么是回调函数
			
RCF的使用教程 RCF(Remote Call Framework)是一个使用C++编写的RPC框架,在底层RCF支持多种传输实现方式(transport implementations). 包括T ...
 - keepalived+haproxy 安装配置
			
1.安装配置keepalived 修改配置文件/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_de ...
 - 如何理解Hibernate的持久化?
			
学习Hibernate,必须要理解什么是持久化?结合了一下网上的各位大佬的观点和自己的理解: 持久化概念 持久化是将程序数据在持久状态和瞬时状态间转换的机制.通俗的讲,就是瞬时数据(比如内存中的数据, ...
 - 纯JS实现前端动态分页码
			
思路分析:有3种情况 第一种情况,当前页面curPage < 4 第二种情况,当前页面curPage == 4 第三种情况,当前页面curPage>4 此外,还要考虑,当前页码 curPa ...
 - hive 学习系列六 hive 去重办法的思考
			
方法1,建立临时表,利用hive的collect_set 进行去重. create table if not exists tubutest ( name1 string, name2 string ...
 - C# 面试题 (二)
			
1. 什么是C#? C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序设计语言.C#是一种安全的.稳定的.简单的.优雅的,由C和C++衍生出来的面向对象的编程语言. ...
 - BZOJ1085_骑士精神_KEY
			
题目传送门 乍一看好像是搜索题,但搜索明显会超时. 此处采用IDA*的方法求解. IDA*算法就是基于迭代加深的A*算法. code: /******************************* ...
 - 三 Hive 数据处理  自定义函数UDF和Transform
			
三 Hive 自定义函数UDF和Transform 开篇提示: 快速链接beeline的方式: ./beeline -u jdbc:hive2://hadoop1:10000 -n hadoop 1 ...