网络流相关(拓扑)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 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·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

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.

Sample Input

Input

3 33 2 101 2 103 1 5

Output

101

Input

4 51 2 101 3 102 3 54 2 153 4 5

Output

00110  可以发现这里有拓扑性质,可以直接做,O(N)复杂度。
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 5 using namespace std;
 6 const int N=200010,M=400010;
 7 int cnt=1,fir[N],nxt[M],to[M],cap[M];
 8 int n,m,in[N],vis[N],ans[N];queue<int>q;
 9 void addedge(int a,int b,int c){
10     nxt[++cnt]=fir[a];
11     to[fir[a]=cnt]=b;
12     cap[cnt]=c;
13 }
14 int main(){
15     scanf("%d%d",&n,&m);
16     for(int i=1,a,b,c;i<=m;i++){
17         scanf("%d%d%d",&a,&b,&c);
18         addedge(a,b,c);addedge(b,a,c);
19         in[a]+=c;in[b]+=c;
20     }
21     for(int i=2;i<n;i++)in[i]/=2;
22     q.push(1);in[1]=0;vis[1]=1;
23     while(!q.empty()){
24         int x=q.front();q.pop();
25         for(int i=fir[x];i;i=nxt[i])
26             if(!vis[to[i]]){
27                 in[to[i]]-=cap[i];
28                 ans[i/2]=i%2;
29                 if(in[to[i]]==0){
30                     q.push(to[i]);
31                     vis[to[i]]=1;
32                 }
33             }
34     }
35     for(int i=1;i<=m;i++)
36         printf("%d\n",ans[i]);
37     return 0;
38 }
时间: 2024-08-05 09:34:40

网络流相关(拓扑)CodeForces 269C:Flawed Flow的相关文章

CodeForces - 269C Flawed Flow

http://codeforces.com/problemset/problem/269/C 题目大意: 给定一个边没有定向的无法增广的残量网络且1是源点,n是汇点,给定每条边中的流.? 让你把所有边定向的同时保证这是一个合法的无法增广的无环残量网络(n,m?<=2*10^5) 题解: 这道题首先从流量守恒的角度入手 我们知道对于每个中间点来说一定满足流量守恒性质(流入量 == 流出量) 而我们又可以对所有相连的边权加和求得所有(流入它)的和(流出的它的)流量之和 所以我们可以直接把相连的边的权

Codeforces 270E Flawed Flow 网络流问题

题意:给出一些边,给出边的容量.让你为所有边确定一个方向使得流量最大. 题目不用求最大流, 而是求每条边的流向,这题是考察网络流的基本规律. 若某图有最大,则有与源点相连的边必然都是流出的,与汇点相连的边必然是流入的,其它所有点流入和流出的流量是相等的. 我们可以根据这一规律来求解. 先求出所有点(除了源点和汇点)的总流量(表示流入的流量的2倍),每次流过该边,更新的时候减去流入流量的2倍. 从源点出发广搜每个点,搜的过程可以确定经过边的流向,当某个点的剩余总流量为0时,表示流入该点的流量边已经

CF 269 E 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. Vert

网络流相关知识点以及习题(持续更新)

首先来认识一下网络流中最大流的问题 给定一个有向图G=(V,E),把图中的边看做成管道,边权看做成每根管道能通过的最大流量(容量),给定源点s和汇点t,在源点有一个水源,在汇点有一个蓄水池,问s-t的最大水流量是多少 网络流图里,源点流出的量等于汇点流入的量,除源汇外的任何点,其流入量之和等于流出量之和 . 首先我们来看下面的图 s是源点,t是汇点 先这么想,先用dfs找出一条从s-t的路线,把他塞满,然后流量就是路径中容量最小的那条路的容量,然后把路径上的容量都剪去这个流量,再重新从s-t找可

[bzoj1565][NOI2009]植物大战僵尸_网络流_拓扑排序

植物大战僵尸 bzoj1565 题目大意:给你一张网格图,上面种着一些植物.你从网格的最右侧开始进攻.每个植物可以对僵尸提供能量或者消耗僵尸的能量.每个植物可以保护一个特定网格内的植物,如果一个植物被保护,那么如果僵尸想吃掉该植物就必须先吃掉保护它的植物.问:僵尸最多能获得多少能量. 注释:1<=N(网格的宽)<=20,1<=M(网格的长)<=30,-20,000<=代价和收益<=20,000. 想法:前置题目([NOI2006]最大获利).这道题和最大获利比较相像,如

【CodeForces】990F Flow Control

题目链接 Luogu & CodeForces 题目描述 You have to handle a very complex water distribution system. The system consists of nn junctions and mm pipes, ii -th pipe connects junctions xixi and yiyi . The only thing you can do is adjusting the pipes. You have to c

网络流相关模板及结论

一.一些结论 1.最大流最小割定理(Maximum Flow, Minimum Cut Theorem):网络的最大流等于最小割 2.任意一个流都小于等于任意一个割(废话) 对于一个网络流图G=(V,E),其中有源点s和汇点t,那么下面三个条件是等价的: 1. 流f是图G的最大流 2. 残留网络Gf不存在增广路 3. 对于G的某一个割(S,T),此时f = C(S,T)   原文地址:https://www.cnblogs.com/member-re/p/10403065.html

[模板] 二分图/网络流相关定理

待更 最小点集覆盖==最大匹配.在这里解释一下原因,首先,最小点集覆盖一定>=最大匹配,因为假设最大匹配为n,那么我们就得到了n条互不相邻的边,光覆盖这些边就要用到n个点.现在我们来思考为什么最小点击覆盖一定<=最大匹配.任何一种n个点的最小点击覆盖,一定可以转化成一个n的最大匹配.因为最小点集覆盖中的每个点都能找到至少一条只有一个端点在点集中的边(如果找不到则说明该点所有的边的另外一个端点都被覆盖,所以该点则没必要被覆盖,和它在最小点集覆盖中相矛盾),只要每个端点都选择一个这样的边,就必然能

Codeforces 512C Fox And Dinner 奇偶建图 网络流

题目链接:点击打开链接 题意: 给定n个人和其权值. 让人坐到任意多张圆形的桌子上使得任意两个相邻的人权值和为素数,且一张桌子至少3个人 输出桌子数和每张桌子坐的人(输出任意一个具体方案) 思路: 首先人的权值都是>=2的,也是就说组合成的素数一定是奇数 奇数 = 奇数+偶数.所以桌子上的人一定是奇偶交错的,而且一张桌子人数是偶数个(即n必须为偶数,一半是奇数,一半是偶数) 由上得:一个奇数要选择2个偶数,一个偶数要被2个奇数选择. 所以得到一个二部图,X集为奇数,Y集为偶数 网络流: 奇数连源