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. Scapy介绍官方文档翻译

    关于Scapy Scapy为何如此特别 高速的报文设计 一次探測多次解释 Scapy解码而不解释 高速展示Quick demo 合理的默认值 学习Python 本人英文水平有限,翻译不当之处,请參考官 ...

  2. 假设让我又一次设计一款Android App

    转载请注明出处: 本文来自aspook的博客:http://blog.csdn.net/ahence/article/details/47154419 开发工具的选择 开发工具我将选用Android  ...

  3. man pthread_mutex_init 或 man pthread_mutex_lock 没有结果的解决的方法

    问题: 在刚装好的 Mint/Ubuntu 可能会出现 man pthread_mutex 相关的函数没结果, 报No manual entry for pthread_mutex_init 的错误. ...

  4. Java语言的优点

    1)Java为纯面向对象的语言. <Thinking in Java>提到Java语言是一种“Everything is object”的语言,它能够直接反应现实生活中的对象, 例如火车, ...

  5. 了解和解决SQL SERVER阻塞问题(copy)

    http://support.microsoft.com/kb/224453 Summary In this article, the term "connection" refe ...

  6. Edge浏览器+微软小娜

  7. Five Invaluable Techniques to Improve Regex Performance

    Regular expressions are powerful, but with great power comes great responsibility. Because of the wa ...

  8. asp.net中Server.MapPath的使用

    http://www.studyofnet.com/news/184.html   本文导读:Server.MapPath()的全名是System.Web.HttpContext.Current.Se ...

  9. 01《UML大战需求分析》阅读笔记之一

    在大二的时候就已经在课堂上对UML也就是统一建模语言有了初步的了解,但是却不怎么明白,虽然可以画图可以完成任务,但是有些糊里糊涂.所以特地把这门书作为精读书籍,想要更加深度地学习UML.很多内容只用语 ...

  10. CDR查找替换对象操作详解

    您可以使用CorelDRAW软件中提供的查找和替换向导,在绘图中定位和编辑对象.这在设计绘图中经常用到,查找和替换中为用户提供多种搜索方法,其中包括包含对象类型及其相关属性.填充和轮廓属性.应用于对象 ...