CF 269 E Flawed Flow

Source: Internet
Author: User

CF 269 E Flawed Flow

Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet cipher it calculates the maximum flow in an undirected graph. The graph consistsNVertices andMEdges. Vertices are numbered from 1N. Vertices 1 andNBeing the source and the sink respectively.

However, his max-flow algorithm seems to have a little flaw too much 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 shocould 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 volumeCI. You shoshould direct all edges in such way that the following conditions hold:

  1. For each vertexV(1? V? N), SumCIOf incoming edges is equal to the sumCIOf 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 integersNAndM(2? ¡U?N? ¡U? 2 Tib limit 105,N? -? 1? ¡U?M? ¡U? 2 rows between 105), the number of vertices and edges in the graph. The followingMLines contain three space-separated integersAI,BIAndCI(1? ¡U?AI,?BI? ¡U?N,AI? Too many?BI, 1? ¡U?CI? ¡U? 104), which means that there is an undirected edge fromAIToBIWith flow volumeCI.

    It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists.

    Output

    OutputMLines, each containing one integerDI, Which shocould be 0 if the direction ofI-Th edge isAI? ~Ú?BI(The flow goes from vertexAITo vertexBI) And shoshould be 1 otherwise. The edges are numbered from 1MIn the order they are given in the input.

    If there are several solutions you can print any of them.

    Sample test (s) 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
    Note

    In the first test case, 10 flow units pass through path, and 5 flow units pass directly from source to sink :. <symbol · blank "http://www.bkjia.com/kf/ware/vc/" target = "_ blank" class = "keylink"> vcD4KPHA + Crj4s/bN + MLnwfe1xM7ez/keys/yoaPK18/IyejL + keys/keys + keys + CjxwPgo8cHJlIGNsYXNzPQ = "brush: java; "> # include # Include # Include # Include Typedef 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) 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 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 ¼ µ ¶ ½ · ½ Ï ò 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.