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. 在linux環境下安裝jprofiler_linux_8_0_2.sh

    1.安装jprofiler_linux_8_0_2.sh chmod+x jprofiler_linux_8_0_2.sh ./jprofiler_linux_8_0_2.sh –c 安装过程略..差 ...

  2. h5-news_index

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdcAAABuCAIAAAD6VM6fAAAUEUlEQVR4nO2dfXRTZZ7Hfx735Z/lr9 ...

  3. FFMpeg在Windows下搭建开发环境【转】

    本文转载自:http://blog.csdn.net/wootengxjj/article/details/51758621 版权声明:本文为博主原创文章,未经博主允许不得转载. FFmpeg 是一个 ...

  4. bzoj3715: [PA2014]Lustra(乱搞)

    3715: [PA2014]Lustra 题目:传送门 题解: 随手一发水题x1 随便排序一下...小学生题??? 代码: #include<cstdio> #include<cst ...

  5. ES failed to notify ClusterStateListener java.lang.IllegalStateException: environment is not locked

    ES出现异常: failed to notify ClusterStateListenerjava.lang.IllegalStateException: environment is not loc ...

  6. .NET Core 开发:永远的Hello Word

    从.NET Core发布到现在已经很长一段时间了,园子里的各路大神也和它打的火热,本着与时俱进,啥火学啥的原则,我也开始了我的.NET Core学习之旅. 简介 .NET从2002年发行到现在,从呱呱 ...

  7. Elasticsearch部署异常Permission denied

    异常描述 在Linux上部署ElasticSearch时抛出了一个异常如下: log4j:ERROR setFile(null,true) call failed. java.io.FileNotFo ...

  8. Tomcat 初探(二) server.xml 配置

    前言 在上一篇文章中,我们在示例中演示了网站的发布,其中涉及到了 server.xml 的修改,本文中我会给大家详细解释一下 server.xml 文件中的节点及其属性的作用,本片文章参考并摘抄了他人 ...

  9. Android Fragment RecycleListView

    1.新建SuperActivity package com.example.ting.criminalintentpractise; import android.os.Bundle;import a ...

  10. COM基础

    为什么说COM的可重用性是建立在二进制级别? COM本身是语言无关,它的标准建立在二进制级别.对于使用COM组件的客户程序,它只需要要使用的COM对象信息就可以通过COM库的帮助创建和使用COM对象, ...