CRB has a tree, whose vertices are labeled by 1, 2, …, N. They are connected by N – 1 edges. Each edge has a weight.
For any two vertices u and v(possibly
equal), f(u,v) is
xor(exclusive-or) sum of weights of all edges on the path from u to v.
CRB’s task is for given s,
to calculate the number of unordered pairs (u,v) such
that f(u,v) = s.
Can you help him?
 
Input
There are multiple test cases. The first line of input contains an integer T,
indicating the number of test cases. For each test case:
The first line contains an integer N denoting
the number of vertices.
Each of the next N -
1 lines contains three space separated integers a, b and c denoting
an edge between a and b,
whose weight is c.
The next line contains an integer Q denoting
the number of queries.
Each of the next Q lines
contains a single integer s.
1 ≤ T ≤
25
1 ≤ N ≤ 105
1 ≤ Q ≤
10
1 ≤ a, b ≤ N
0 ≤ c, s ≤ 105
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

题意:
f(u,v) =  s(从u->v的异或)中u,v可以有多少对

所以f(u,v)  = f(1 , u) ^ f(1 , v)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define N 100050
typedef long long ll; int head[N], tot;
int num[2*N];
int dis[N]; struct edge
{
int v, w, next;
} edg[2*N]; void add(int u, int v, int w)
{
edg[tot].v = v;
edg[tot].w = w;
edg[tot].next = head[u];
head[u] = tot++;
} void dfs(int u, int fa, int val)
{
dis[u] = val;
num[val]++; //记录val的个数
for(int i = head[u]; ~i; i = edg[i].next)
{
int v = edg[i].v;
if(v == fa)
continue;
dfs(v, u, val^edg[i].w);
}
}
int main()
{
int T,u,v,w,m,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
tot = 0;
memset(head,-1,sizeof(head));
memset(num,0,sizeof(num));
for(int i = 1; i < n; i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
} dfs(1,-1,0);
scanf("%d", &m);
int s;
for(int i = 0; i < m; i++)
{
scanf("%d", &s);
int temp;
ll ans = 0;
for(int i = 1;i <= n;i++)
{
temp = s^dis[i];
if(temp == dis[i]) //除去本身的那个
ans += num[temp]- 1;
else
ans += num[temp];
}
ans/=2;
if(s == 0) //加上自己与自己异或的情况
ans += n;
printf("%I64d\n",ans);
}
}
return 0;
}

  

2015 多校联赛 ——HDU5416(异或)的更多相关文章

  1. 2015 多校联赛 ——HDU5299(树删边)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  2. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  3. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. 2015 多校联赛 ——HDU5294(最短路,最小切割)

    Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  5. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  6. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  7. 2015 多校联赛 ——HDU5323(搜索)

    Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. 2015 多校联赛 ——HDU5319(模拟)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  9. 2015 多校联赛 ——HDU5301(技巧)

    Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...

随机推荐

  1. jQuery函数学习

    函数:after(content) 功能:在每个匹配的元素后面添加html内容 返回:jQuery对象 参数:content (<Content>): Content to insert ...

  2. eclipse下maven一些配置方法汇总

    随着eclipse的不同版本的变更:对maven插件的安装也有着不同的差异:之前也在一些版本的eclipse上安装成功地,但是最近又遇到了一些麻烦,故将这些方法记录下来: 大家都知道的最常用的一种方式 ...

  3. Python 迭代器之列表解析

     [TOC] 尽管while和for循环能够执行大多数重复性任务, 但是由于序列的迭代需求如此常见和广泛, 以至于Python提供了额外的工具以使其更简单和高效. 迭代器在Python中是以C语言的 ...

  4. nyoj 背包问题

    背包问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在有很多物品(它们是可以分割的),我们知道它们每个物品的单位重量的价值v和重量w(1<=v,w< ...

  5. 安装nodejs时:The error code is 2503.

    在windows下安装nodejs时老是报错: The installer has encountered an unexpected error installing . 有三种方法可以尝试: &q ...

  6. hadoop2.6.0理论:hdfs、yarn、mapreduce的架构

    HDFS2的架构:负责数据的分布式存储 主从结构 主节点,可以有2个: namenode 从节点,有很多个: datanode namenode负责: 接收用户操作请求,是用户操作的入口 维护文件系统 ...

  7. 新概念英语(1-1)Excuse me!

    Excuse me!Whose handbag is it? A:Excuse me! B:Yes? A:Is this your handbag? B:Pardon? A:Is this your ...

  8. My97设置开始、结束 时间区间及输入框不能输入只能选择的方法

    时间区间开始: <input type="text" id = "first_time" name="first_time" valu ...

  9. js、jQuery 获取文档、窗口、元素的各种值

    基于两年开发经验,总结了 javascript.jQuery 获取窗口.文档.元素的各种值 javascript: 文档:是整个document所有的内容 浏览器当前窗口文档body的宽度: docu ...

  10. python基础——类名称空间与对象(实例)名称空间

    python基础--类名称空间与对象(实例)名称空间 1 类名称空间 创建一个类就会创建一个类的名称空间,用来存储类中定义的所有名字,这些名字称为类的属性 而类的良好总属性:数据属性和函数属性 其中类 ...