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)\ ...
随机推荐
- Linux基础(05)、Linux进阶命令
目录 一.进阶命令 二.系统命令 三.压缩和归档 3.1.归档 3.2.压缩 3.3.归档并压缩 归档.接档:tar -cf.tar -tvf 压缩.解压:gzip.gunzip 归档并压缩:tar ...
- JQuery制作网页——第五章 初识 jQuery
1.jQuery简介: ● jQuery由美国人John Resig于2006年创建 ● jQuery是目前最流行的JavaScript程序库,它是对JavaScript对象和函数的封装 ● 它的设计 ...
- jQuery 常用核心方法
jQuery 常用核心方法 .each() 遍历一个jQuery对象,为每个匹配元素执行一个函数 $('p').each(function(idx,node){ $(node).text(idx + ...
- day06-codes and exercise in class
# Author: Ghost # Email: jiaci.liu@gmail.com ''' 1-Review of last week 2-interface class, abstract c ...
- Python习题(分页显示)
class Page: def __init__(self, lst, pageSize): self.lst = lst # 数据 self.pageSize = pageSize # 每页显示多少 ...
- Centos配置网卡子接口
1.检查OS是否加载802.1q模块: 方法一: [root@rs2 ~]# modinfo 8021q 方法二: [root@rs2 ~]# modinfo -F filename 8021q 方法 ...
- C# 截取屏幕图像
#region 截取屏幕图像 private static Bitmap GetScreenCapture() { Rectangle tScreenRect = , , Screen.Primary ...
- WebRTC中Android Demo中的远程视频流的获取到传输
1.CallActivity#onCreate 执行startCall开始连接或创建房间 2.WebSocketClient#connectToRoom 请求一次服务器 3.回调到CallActivi ...
- sql server 对Geography 的增(insert)和查询(select)
insert: Location为 Geography类型 INSERT INTO [oss1].[dbo].[Order] ([Location]) VAL ...
- dubbo之main启动
一.dubbo的main启动在使用上面会简单的多,但是需要做一些简单的配置. dubbo.spring.config=classpath*:META-INF/spring/*.xml 备注:这个是默认 ...