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 (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·105n - 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 ≠ bi1 ≤ 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)的时间内求出解。

 

 1 #include <cstdio>

 2 #include <cctype>

 3 #include <stack>

 4 

 5 const int MAXN = 200010;

 6 

 7 int n, m, ecnt;

 8 int a[MAXN], b[MAXN], inflow[MAXN], outflow[MAXN], direct[MAXN];

 9 int head[MAXN], to[MAXN*2], next[MAXN*2], c[MAXN*2], from[MAXN*2];

10 

11 inline int readint(){

12     char c = getchar();

13     while(!isdigit(c)) c = getchar();

14     int x = 0;

15     while(isdigit(c)){

16         x = x * 10 + c - '0';

17         c = getchar();

18     }

19     return x;

20 }

21 

22 inline void addEdge(int &u, int &v, int &i){

23     int x = readint();

24     outflow[u] += x; outflow[v] += x;

25     to[ecnt] = v; c[ecnt] = x; from[ecnt] = i;

26     next[ecnt] = head[u]; head[u] = ecnt++;

27     to[ecnt] = u; c[ecnt] = x; from[ecnt] = i;

28     next[ecnt] = head[v]; head[v] = ecnt++;

29 }

30 

31 void makeDirect(){

32     std::stack<int> st;

33     st.push(1); outflow[1] = 0;

34     while(!st.empty()){

35         int u = st.top(); st.pop();

36         for(int p = head[u]; p; p = next[p]){

37             int &v = to[p];

38             if(inflow[v] == outflow[v] && v != n) continue;

39             inflow[v] += c[p]; outflow[v] -= c[p];

40             if(inflow[v] == outflow[v] && v != n) st.push(v);

41             int x = from[p];

42             if(v == a[x]) direct[x] = 1;

43         }

44     }

45 }

46 

47 int main(){

48     scanf("%d%d",&n,&m);

49     ecnt = 1;

50     for(int i = 0; i < m; ++i){

51         a[i] = readint();

52         b[i] = readint();

53         addEdge(a[i], b[i], i);

54     }

55     makeDirect();

56     for(int i = 0; i < m; ++i) printf("%d\n", direct[i]);

57 }
View Code

 

你可能感兴趣的:(codeforces)