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.
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.

Sample test(s)

input

3 3
3 2 10
1 2 10
3 1 5

output

1
0
1

input

4 5
1 2 10
1 3 10
2 3 5
4 2 15
3 4 5

output

0
0
1
1
0

Note

In the first test case, 10 flow units pass through path ,
and 5 flow units pass directly from source to sink: .

给出网络流的无向图几流量,问你边的方向。首先设所有点的flow[i][0]代表流出的方向。

可以想到如果一个点满足:flow[i][[0]==flow[i][1]即可确定方向了。

BFS做法:从已知不断BFS,直到一个点的:flow[i][[0]==flow[i][1],加入到队列中。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int maxn=2*(1e5+100);
struct node{
    int u,v,w;
    int num,next;
}e[maxn<<1];
int vis[maxn],head[maxn];
int pre[maxn],s[maxn];
int flow[maxn][2];
int n,m,cnt;
void add(int x,int y,int z,int n)
{
    e[cnt].u=x;e[cnt].v=y;
    e[cnt].w=z;e[cnt].num=n;
    e[cnt].next=head[x];
    head[x]=cnt++;
}
void BFS()
{
    CLEAR(vis,0);
    queue<int>q;
    q.push(1);
    vis[1]=1;
    while(!q.empty())
    {
        int st=q.front();
        q.pop();
        for(int i=head[st];i!=-1;i=e[i].next)
        {
            int v=e[i].v;
            int num=e[i].num;
            int w=e[i].w;
            if(vis[v])   continue;
            s[num]=st;//记录边起始点判断方向
            flow[v][1]+=w;
            flow[v][0]-=w;

            if(v!=n&&flow[v][1]==flow[v][0])
            {
                vis[v]=1;
                q.push(v);
            }
        }
    }
}
int main()
{
     int x,y,z;
     std::ios::sync_with_stdio(false);
     while(cin>>n>>m)
     {
         CLEAR(head,-1);
         CLEAR(flow,0);
         cnt=0;
         REPF(i,1,m)
         {
             cin>>x>>y>>z;
             pre[i]=x;
             add(x,y,z,i);
             add(y,x,z,i);
             flow[x][0]+=z;
             flow[y][0]+=z;
         }
         BFS();
         REPF(i,1,m)
         {
             if(pre[i]==s[i])   puts("0");
             else   puts("1");
         }
     }
     return 0;
}
时间: 2024-11-11 03:09:35

CF 269 E Flawed Flow的相关文章

CodeForces - 269C Flawed Flow

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

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

Codeforces 270E Flawed Flow 网络流问题

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

Python与rrdtool的结合模块

rrdtool(round robin database)工具为环状数据库的存储格式,round robin是一种处理定量数据以及当前元素指针的技术.rrdtool主要用来跟踪对象的变化情况,生成这些变化的走势图,比如业务的访问流量.系统性能.磁盘利用率等趋势图,很多流行监控平台都使用到rrdtool,比较有名的为Cacti.Ganglia.Monitorix等.更多rrdtool介绍见官网http://oss.oetiker.ch/rrdtool/.rrdtool是一个复杂的工具,涉及较多参数

/etc/postfix/main.cf

# cat main.cf     1  # Global Postfix configuration file. This file lists only a subset     2  # of all parameters. For the syntax, and for a complete parameter     3  # list, see the postconf(5) manual page (command: "man 5 postconf").     4  #

cf 434d Nanami&#39;s Power Plant 网络流

题目大意就是有n个发电机,每个发电机有l到r个档位可供选择,每个档位的输出功率是已知的,另外还有一些限制条件,形式为xu ≤ xv + d,表示发电机u的档位要小于v的档位加d,d是一个已知的整数.求n个发电机的最大功率. 假设没有最后那个限制条件,那么对于每个发电机i拆点成l-1,l...到r相邻两档位连边为max-f(i),f(i)是档位i的输出功率,max是一个大数,大于等于所有档位的输出功率.l-1与源点连inf的边,r与汇点连inf的边.假设最大流为flow,那么max*n-flow就

【CF708D】Incorrect Flow 最小费用可行流

[CF708D]Incorrect Flow 题意:给你一个点数为n,边数为m的流网络,每条边有一个容量c和流量f,这个网络可能是不合法的.你可以花费1的代价使c或f减少或增加1,可以修改无限次.你不需要使流量最大,你只需要花费最少的代价把原图改造成一个合法的网络. $n,m\le 100,c,f\le 10^6$ 题解:我们用有上下界的费用流来解决这个问题. 对于一条边a->b,如果c>f,则我们从a到b连一条下界和上界都是f,费用为0的边:因为可以减少流量,所以连一条从b到a,容量为f,费

CF 331 E. Biologist

CF 331 E. Biologist 题目描述 题目大意:有\(n\)个点,初始时每个点为黑色或者白色,你可以花费\(v_i\)的代价将一个点反色.然后你有许多计划,每个计划要求一个点集中的所有点为同种颜色.满足了一个计划就可以得到\(w_i\)相应的价值,某些计划如果没有被满足,还会付出\(g\)的代价. 感觉这个题有点最大权闭合子图的样子,\(g\)的额外代价也很鸡肋. 然后我们考虑这道题的反色操作.如果第\(i\)个点本来是白色的,那么我们连\((S,i,v_i)\),否则连\((i,T

viv of a rigid cylinder near a plane subjected to steady subcritical flow

Table Table of Contents 1. table 1.1. validation 1.2. khalak and Williamson,1997, JFS 2. setups 3. new contribution from guo fei 4. english 1 table viv of a cylinder near a plane boundary governing and influencing parameters: Re(flow regimes) G/D m*ξ