CF269C.Flawed Flow

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn vertices and mm edges. Vertices are numbered from 1 to nn . Vertices 11 and nn 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 ( aia_{i} , bib_{i} ) you are given the flow volume cic_{i} . You should direct all edges in such way that the following conditions hold:

  1. for each vertex vv (1<v<n) , sum of cic_{i} of incoming edges is equal to the sum of cic_{i} of outcoming edges;
  2. vertex with number 11 has no incoming edges;
  3. the obtained directed graph does not have cycles.

输入格式

The first line of input contains two space-separated integers nn and mm ( 2<=n<=21052<=n<=2·10^{5} , n1<=m<=2105n-1<=m<=2·10^{5} ), the number of vertices and edges in the graph. The following mm lines contain three space-separated integers aia_{i} , bib_{i} and cic_{i} ( 1<=ai,bi<=n1<=a_{i},b_{i}<=n , aibia_{i}≠b_{i} , 1<=ci<=1041<=c_{i}<=10^{4} ), which means that there is an undirected edge from aia_{i} to bib_{i} with flow volume cic_{i} .

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

输出格式

Output mm lines, each containing one integer did_{i} , which should be 0 if the direction of the ii -th edge is aibia_{i}→b_{i} (the flow goes from vertex aia_{i} to vertex bib_{i} ) and should be 1 otherwise. The edges are numbered from 1 to mm in the order they are given in the input.

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

输入输出样例

  • 输入#1

    3 3
    3 2 10
    1 2 10
    3 1 5
    

    输出#1

    1
    0
    1
    
  • 输入#2

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

    输出#2

    0
    0
    1
    1
    0
    

说明/提示

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

首页