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. 剑指Offer读书笔记(持续更新中)

    (1)定义一个空的类型,里面没有不论什么成员变量和成员函数,对该类型求sizeof,得到的结果是多少? 答案是1.空类型的实例中不包括不论什么信息,本来求sizeof应该是0,可是当我们声明该类型实例 ...

  2. 用友ERP T6技术解析(六) 库龄分析

    2.4 库存管理   2.4.1 库龄分析 介绍:库存账龄是在某时间节点,某种或某类存货的库存时间的加权平均值,跟库存周转率关系明显,库存周转率越高,库存账龄越低,可是二者又不是反比关系.不能简单把库 ...

  3. DirectX11 学习笔记1 - 第一个程序

    为了加快学习速度,和使程序更加easy理解.  我把sampler tutorial里面的一个样例 的固定代码和常常修改的代码经过简单的类的封装了一下. 以后学习的时候就能够仅仅在还有一个文件写ren ...

  4. hdoj--1171--Number Sequence(KMP)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. 在Maven中引入spring的DAO、DOMAIN、CONTROLLER、VIEW

    除了mysql外麻雀虽小,五脏俱全. 参照之前的博客建立好的maven项目如图. 第一步 : 向maven项目中的pom文件添加依赖 ,然后maven install

  6. Tomcat转jboss踩的那些坑

    问题背景 今天发版本,是一个httpclient的跳转(由于公司网络原因,所以对外网的访问都经过这个代理服务出去). 问题原因 之前的开发一直在window系统的tomcat服务器上进行的,对jbos ...

  7. 话说普通的TPlink ip地址是192.168.1.2 在LAN里有台电脑共享打印机 ip 是192.168.0.2 计算机名为j02 然后我把这台电脑加到DMZ里,让根路由器同一网段的可以访问 但添加打印机的时候 提示 计算机名重复 后来在需要添加打印机电脑的hosts文件里加了 192.168.1.2 j02 式了一样不行 话说,这个打印机该怎么添加

    开启端口映射,从外网访问内网的文件共享: 已经在路由器里开了远端WEB管理设了端口,另外端口映射局域网里的一台电脑,比如WEB端口设的是8080,映射192.168.1.100到4877端口,现在我想 ...

  8. 50个技巧提高你的PHP网站程序执行效率

    1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP手册中说echo是语言结构,不是真正的函数,故 把函数加上了双引号). 2.如果能将类的方法定义成static,就尽量定义成static ...

  9. 使用 Sublime 或其他编辑器调试 Tampermonkey 油猴脚本

    作者说由于 Chrome 安全限制,没办法调用外部编辑器调试,但提供了一个间接办法,那就是脚本中使用@require file:///引入本地文件的形式,具体的方法是 打开 chrome://exte ...

  10. ZBrush中的实时遮罩

    在ZBrush®中有许多遮罩类型,包括柔滑遮罩.反转遮罩,实时遮罩等.其中,实时遮罩又包含很多种类,它不同于一般的遮罩是不显示的,实时遮罩是根据实时信息产生新的遮罩. 在“Brush”菜单下“Auto ...