|
CRB and TreeTime Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 607 Accepted Submission(s): 194
Problem Description
CRB has a tree, whose vertices are labeled by 1, 2, …,
For any two vertices CRB’s task is for given
Input
There are multiple test cases. The first line of input contains an integer
The first line contains an integer Each of the next The next line contains an integer Each of the next 1 ≤ 1 ≤ 1 ≤ 1 ≤ 0 ≤ It is guaranteed that given edges form a tree.
Output
For each query, output one line containing the answer.
Sample Input
Sample Output
Author
KUT(DPRK)
Source
Recommend
|
#include <cstdio> #include <cstring> #include <iostream> #include <set> using namespace std; #define LL long long #define N 100000 + 10 int n, q, s, T; int head[N], tot; int m[2 * N]; int dist[N]; struct edge { int v, w, next; }e[2 * N]; void init() { tot = 0; memset(head, -1, sizeof head); memset(m, 0, sizeof m); } void adde(int u, int v, int w) { e[tot].v = v; e[tot].w = w; e[tot].next = head[u]; head[u] = tot++; } void dfs(int u, int fa, int val) { dist[u] = val; m[val]++; for(int i = head[u]; i != -1; i = e[i].next) { int v = e[i].v; if(v == fa) continue; dfs(v, u, val ^ e[i].w); } } void solve() { LL ans = 0; int j ; for(int i = 1; i <= n; i++) { j = dist[i] ^ s; if(j == dist[i]) ans += m[j] - 1; else ans += m[j]; } ans /= 2; if(s == 0) ans += n; printf("%I64d\n", ans); } int main() { scanf("%d", &T); while(T--) { scanf("%d", &n); init(); int u, v, w; for(int i = 1; i < n; i++) { scanf("%d%d%d", &u, &v, &w); adde(u, v, w); adde(v, u, w); } dfs(1, -1, 0); scanf("%d", &q); for(int i = 0; i < q; i++) { scanf("%d", &s); solve(); } } return 0; } /* 1 3 1 2 1 2 3 2 3 2 3 4 1 5 1 2 1 3 5 2 1 3 2 2 4 2 10 0 3 2 */
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/dojintian/article/details/47834933