CRB and Tree

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 481    Accepted Submission(s): 151

Problem Description
CRB has a tree, whose vertices are labeled by 1, 2, …, .
They are connected by  –
1 edges. Each edge has a weight.

For any two vertices 

rev=2.4-beta-2" alt="" style=""> and (possibly
equal), 

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style=""> is
xor(exclusive-or) sum of weights of all edges on the path from 

rev=2.4-beta-2" alt="" style=""> to .

CRB’s task is for given ,
to calculate the number of unordered pairs  such
that 

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">.
Can you help him?

 
Input
There are multiple test cases. The first line of input contains an integer 

rev=2.4-beta-2" alt="" style="">,
indicating the number of test cases. For each test case:

The first line contains an integer  denoting
the number of vertices.

Each of the next  -
1 lines contains three space separated integers 

rev=2.4-beta-2" alt="" style=""> and  denoting
an edge between  and ,
whose weight is 

rev=2.4-beta-2" alt="" style="">.

The next line contains an integer  denoting
the number of queries.

Each of the next 

rev=2.4-beta-2" alt="" style=""> lines
contains a single integer .

1 ≤ 

rev=2.4-beta-2" alt="" style=""> ≤
25

1 ≤  ≤ 

rev=2.4-beta-2" alt="" style="">

1 ≤  ≤
10

1 ≤  ≤ 

rev=2.4-beta-2" alt="" style="">

0 ≤ 

rev=2.4-beta-2" alt="" style="">,  ≤ 

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

It is guaranteed that given edges form a tree.


 
Output
For each query, output one line containing the answer.
 
Sample Input
1
3
1 2 1
2 3 2
3
2
3
4
 
Sample Output
1
1
0
Hint
For the first query, (2, 3) is the only pair that f(u, v) = 2.
For the second query, (1, 3) is the only one.
For the third query, there are no pair (u, v) such that f(u, v) = 4.
 
Author
KUT(DPRK)

解题思路:
由于异或是可逆的,因此从前到后记录前缀异或和,用hash表记录每一个值出现的次数,每次仅仅须要加上x ^ sum[v]出现的次数就可以。由于此时,u到v的异或和就是x。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <algorithm>
#define LL long long
using namespace std;
const int MAXN = 100000 + 10;
struct Edge
{
int to, next, w;
}edge[MAXN<<1];
int tot, head[MAXN];
int read()
{
int res = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') f *= -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){res = res * 10 + ch - '0'; ch = getchar();}
return res;
}
void init()
{
tot = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w)
{
edge[tot].to = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
}
int N, Q;
int vis[MAXN], st[MAXN<<2], op;
LL ans;
void dfs(int u, int x)
{
vis[u] = 1; st[x]++;
ans += st[op ^ x];
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v = edge[i].to, w = edge[i].w;
if(!vis[v])
{
dfs(v, x ^ w);
}
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
N = read();
int u, v, w;
init();
for(int i=1;i<N;i++)
{
u = read(); v = read(); w = read();
addedge(u, v, w);
addedge(v, u, w);
}
scanf("%d", &Q);
while(Q--)
{
op = read();
memset(vis, 0, sizeof(vis));
memset(st, 0, sizeof(st));
ans = 0;
dfs(1, 0);
printf("%I64d\n", ans);
}
}
return 0;
}

 

HDU 5416 CRB and Tree (2015多校第10场)的更多相关文章

  1. hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)

    CRB and Tree                                                             Time Limit: 8000/4000 MS (J ...

  2. Hdu 5416 CRB and Tree (bfs)

    题目链接: Hdu 5416 CRB and Tree 题目描述: 给一棵树有n个节点,树上的每条边都有一个权值.f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v) ...

  3. HDU 5416 CRB and Tree(前缀思想+DFS)

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  4. HDU 5416——CRB and Tree——————【DFS搜树】

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. HDU 5416 CRB and Tree

    题目大意: T, T组测试数据 给你一个n有n个点,下标是从 1 开始的.这个是一棵树,然后下面是n-1条边, 每条边的信息是 s,e,w 代表 s-e的权值是w 然后是一个Q代表Q次询问. 每次询问 ...

  6. HDU 5416 CRB and Tree (技巧)

    题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...

  7. HDU 5305 Friends (搜索+剪枝) 2015多校联合第二场

    開始对点搜索,直接写乱了.想了想对边搜索,尽管复杂度高.剪枝一下水过去了. 代码: #include<cstdio> #include<iostream> #include&l ...

  8. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  9. HDOJ 5416 CRB and Tree DFS

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

随机推荐

  1. hdu 4888 2014多校第三场1002 Redraw Beautiful Drawings 网络流

    思路:一開始以为是高斯消元什么的.想让队友搞,结果队友说不好搞,可能是网络流.我恍然,思路立刻就有了. 我们建一个二部图.左边是行,右边是列,建个源点与行建边,容量是该行的和.列与新建的汇点建边.容量 ...

  2. jsp不通过form和Ajax提交

    在页面里面我们一般都通过form表单和Ajax向后台提交请求,但是我如今页面没有form表单,也不想通过ajax异步提交. 解决方式例如以下:location.href="${rootPat ...

  3. 继承的综合运用《Point类派生出Circle类而且进行各种操作》

    类的组合与继承 (1)先建立一个Point(点)类.包括数据成员x,y(坐标点). (2)以Point为基类.派生出一个Circle(圆)类,添加数据成员(半径),基类的成员表示圆心: (3)编写上述 ...

  4. UVA 1149 Bin Packing 二分+贪心

    A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...

  5. UVA 10593 Kites DP

    The season of flying kites is well ahead. So what? Let us make an inventory for kites. We are givena ...

  6. Rsync 服务器搭建

    Rsync简介 rsync 是一个 Unix 系统下的文件同步和传输工具. 它具备以下特性: 1. 能更新整个目录和树和文件系统 2. 有选择性的保持符号链链.硬链接.文件属于.权限.设备以及时间 等 ...

  7. Win10 UI入门 pivot multiable DataTemplate

    this is a dynamic pivot with sliderable navigation and multiableDatatemplate Control 看了 alexis 大哥的pi ...

  8. The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online J Press the Button

    BaoBao and DreamGrid are playing a game using a strange button. This button is attached to an LED li ...

  9. 关于函数调用约定-thiscall调用约定

    函数调用约定描述了如何以正确的方式调用某些特定类型的函数.包括了函数参数在栈上的分配顺序.有哪些参数将通过寄存器传入,以及在函数返回时函数栈的回收方式等. 函数调用约定的几种类型 stdcall,cd ...

  10. HDU1231 最长连续子序列

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...