|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + void nextLine() throws Exception { |
| 17 | + st = new StringTokenizer(br.readLine()); |
| 18 | + } |
| 19 | + |
| 20 | + String nextToken() throws Exception { |
| 21 | + while (!st.hasMoreTokens()) nextLine(); |
| 22 | + return st.nextToken(); |
| 23 | + } |
| 24 | + |
| 25 | + int nextInt() throws Exception { |
| 26 | + return Integer.parseInt(nextToken()); |
| 27 | + } |
| 28 | + |
| 29 | + long nextLong() throws Exception { |
| 30 | + return Long.parseLong(nextToken()); |
| 31 | + } |
| 32 | + |
| 33 | + double nextDouble() throws Exception { |
| 34 | + return Double.parseDouble(nextToken()); |
| 35 | + } |
| 36 | + |
| 37 | + void close() throws Exception { |
| 38 | + bw.flush(); |
| 39 | + bw.close(); |
| 40 | + } |
| 41 | + |
| 42 | + void write(String content) throws Exception { |
| 43 | + bw.write(content); |
| 44 | + } |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +public class Main { |
| 49 | + |
| 50 | + static IOController io; |
| 51 | + |
| 52 | + // |
| 53 | + |
| 54 | + static int N, M; |
| 55 | + static List<Integer>[] V; |
| 56 | + static int[] par; |
| 57 | + static int[] OD, OU, ED, EU; |
| 58 | + |
| 59 | + public static void main(String[] args) throws Exception { |
| 60 | + |
| 61 | + io = new IOController(); |
| 62 | + |
| 63 | + init(); |
| 64 | + solve(); |
| 65 | + |
| 66 | + io.close(); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + public static void init() throws Exception { |
| 71 | + |
| 72 | + N = io.nextInt(); |
| 73 | + M = io.nextInt(); |
| 74 | + V = new List[N+1]; |
| 75 | + for(int i=1;i<=N;i++) V[i] = new ArrayList<>(); |
| 76 | + for(int i=1;i<N;i++) { |
| 77 | + int a = io.nextInt(), b = io.nextInt(); |
| 78 | + V[a].add(b); |
| 79 | + V[b].add(a); |
| 80 | + } |
| 81 | + |
| 82 | + par = new int[N+1]; |
| 83 | + OD = new int[N+1]; |
| 84 | + OU = new int[N+1]; |
| 85 | + ED = new int[N+1]; |
| 86 | + EU = new int[N+1]; |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + static void solve() throws Exception { |
| 91 | + |
| 92 | + dfs1(1,0); |
| 93 | + dfs2(1,0); |
| 94 | + |
| 95 | + while(M-->0) { |
| 96 | + int u = io.nextInt(), v = io.nextInt(), c = io.nextInt(); |
| 97 | + if(par[v] == u){ |
| 98 | + if(c == 0) io.write((OU[v] * OD[v] + EU[v] * ED[v]) + "\n"); |
| 99 | + else io.write((OU[v] * ED[v] + EU[v] * OD[v]) + "\n"); |
| 100 | + } |
| 101 | + else{ |
| 102 | + if(c == 0) io.write((OU[u] * OD[u] + EU[u] * ED[u]) + "\n"); |
| 103 | + else io.write((OU[u] * ED[u] + EU[u] * OD[u]) + "\n"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + static void dfs1(int n, int p) { |
| 110 | + par[n] = p; |
| 111 | + for(int i:V[n]) if(i != p) { |
| 112 | + dfs1(i,n); |
| 113 | + OD[n] += ED[i]; |
| 114 | + ED[n] += OD[i]; |
| 115 | + } |
| 116 | + ED[n]++; |
| 117 | + } |
| 118 | + |
| 119 | + static void dfs2(int n, int p) { |
| 120 | + if(n != 1) { |
| 121 | + OU[n] = EU[p] + ED[p] - OD[n]; |
| 122 | + EU[n] = OU[p] + OD[p] - ED[n]; |
| 123 | + } |
| 124 | + for(int i:V[n]) if(i != p) dfs2(i,n); |
| 125 | + } |
| 126 | + |
| 127 | +} |
| 128 | +``` |
0 commit comments