fork download
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.StreamTokenizer;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.Iterator;
  9. import java.util.TreeSet;
  10. import java.lang.Integer;
  11. import java.util.Stack;
  12.  
  13. public class flow_kp_java {
  14.  
  15.  
  16. static int nextInt() {
  17. try {
  18. st.nextToken();
  19. return (int) st.nval;
  20. } catch (IOException ex) {
  21. return 0;
  22. }
  23. }
  24.  
  25. static double nextDouble() {
  26. try {
  27. st.nextToken();
  28. return st.nval;
  29. } catch (IOException ex) {
  30. return 0;
  31. }
  32. }
  33.  
  34. static class Edge {
  35.  
  36. int b, c, d, i;
  37.  
  38. public Edge(int b, int c, int d, int i) {
  39. this.b = b;
  40. this.c = c;
  41. this.d = d;
  42. this.i = i;
  43. }
  44. }
  45.  
  46. public static void main(String[] args) {
  47. int n = nextInt();
  48. int m = nextInt();
  49.  
  50. ArrayList<ArrayList<Edge>> inc = new ArrayList<ArrayList<Edge>>();
  51. for (int i = 0; i <= n; i++) {
  52. inc.add(new ArrayList<Edge>());
  53. }
  54.  
  55. int[] half = new int[200001];
  56. int[] current = new int[200001];
  57. boolean[] visited = new boolean[200001];
  58.  
  59. int[] direction = new int[200005];
  60.  
  61. Stack<Integer> q = new Stack<Integer>();
  62.  
  63.  
  64. int a, b, c;
  65. for (int i = 0; i < m; i++) {
  66. a = nextInt();
  67. b = nextInt();
  68. c = nextInt();
  69. inc.get(a).add(new Edge(b, c, 0, i));
  70. inc.get(b).add(new Edge(a, c, 1, i));
  71. current[a] += c;
  72. current[b] += c;
  73. }
  74. for (int i = 0; i < n; i++) {
  75. half[i] = current[i] / 2;
  76. }
  77. visited[1] = true;
  78. q.push(1);
  79. while (!q.empty()) {
  80. int next = q.pop();
  81. for (Edge e : inc.get(next)) {
  82. if (!visited[e.b]) {
  83. direction[e.i] = e.d;
  84. current[e.b] -= e.c;
  85. if (current[e.b] == half[e.b] && e.b != n) {
  86. visited[e.b] = true;
  87. q.push(e.b);
  88. }
  89. }
  90. }
  91. }
  92. for (int i = 0; i < m; i++) {
  93. System.out.println(direction[i]);
  94. }
  95. }
  96. }
  97.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty