hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)
CRB and Tree
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536
K (Java/Others)
Total Submission(s): 79 Accepted Submission(s): 16
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?
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.
1
3
1 2 1
2 3 2
3
2
3
4
1
1
0HintFor 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.
#include <iostream>
#include <cstring>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=131072;
struct EDGE
{
int to,v,next;
}edge[200010]; int ne=0;
int head[100010];
int sum[200010];
int n;
void addedge(int s,int e,int v)
{
edge[ne].to=e;
edge[ne].next=head[s];
edge[ne].v=v;
head[s]=ne++;
} void dfs(int now,int pre,int nows)
{
sum[nows]++;
for(int i=head[now];i!=-1;i=edge[i].next)
{
if(edge[i].to==pre) continue;
dfs(edge[i].to,now,nows^edge[i].v);
}
} int main()
{
int T,i;
cin>>T;
while(T--)
{
ne=0;
memset(head,-1,sizeof(head));
cin>>n;
for(i=0;i<n-1;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
memset(sum,0,sizeof(sum));
dfs(1,0,0);
int q,s;
cin>>q;
while(q--)
{
long long ans1=0,ans2=0;
cin>>s;
for(i=0;i<131072;i++)
{
int x=i,y=s^i;
if(x!=y)
ans1+=(1ll*sum[x]*sum[y]);
else
{
ans1+=(1ll*sum[x]*(sum[x]-1));
ans2+=1ll*sum[x];
}
}
cout<<ans1/2+ans2<<endl;
}
}
}
hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)的更多相关文章
- Hdu 5416 CRB and Tree (bfs)
题目链接: Hdu 5416 CRB and Tree 题目描述: 给一棵树有n个节点,树上的每条边都有一个权值.f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v) ...
- HDU 5416 CRB and Tree (2015多校第10场)
欢迎參加--每周六晚的BestCoder(有米!) CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 5416 CRB and Tree(前缀思想+DFS)
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- HDU 5416——CRB and Tree——————【DFS搜树】
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5416 CRB and Tree
题目大意: T, T组测试数据 给你一个n有n个点,下标是从 1 开始的.这个是一棵树,然后下面是n-1条边, 每条边的信息是 s,e,w 代表 s-e的权值是w 然后是一个Q代表Q次询问. 每次询问 ...
- HDU 5416 CRB and Tree (技巧)
题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...
- HDOJ 5416 CRB and Tree DFS
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple
CRB and Apple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries
CRB and Queries Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
随机推荐
- SQLState: 23000
今天登陆项目的时候,报500,日志显示如下: 解决办法是: 首先,删除序列:DROP SEQUENCE sys_log_seq 然后,新建序列:CREATE SEQUENCE sys_log_seq ...
- Deployd的使用
deployd一个生成后台数据的软件,可以创建json格式的数据,也可以对数据进行增删改查等操作,甚至可以验证登录,简直就是自学好帮手呀,不用后台搞定后台,就用deployd 下载:链接: https ...
- os下,vs code 自动编译ts
nodejs安装ts npm install -g typescript 进入工程目录,用命令初始化ts(生成tsconfig.json) tsc --init 如果要指定生成目录,打开tsconfi ...
- CAD绘制一个图象标记对象(com接口VB语言)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Unity中带有alpha通道的视频叠加播放
问题: 如何让两个透明视频叠加播放 解决播放: 1:使用Unity自带的shader,shader代码如下所示 Shader "Unlit/MaskVideo" { Propert ...
- C++ 11常见功能介绍:auto,decltype,nullptr,for,lambda
什么是C++11 C++11是曾经被叫做C++0x,是对目前C++语言的扩展和修正,C++11不仅包含核心语言的新机能,而且扩展了C++的标准程序库(STL),并入了大部分的C++ Technical ...
- 行内块+calc+margin 三列布局
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- libevent reference Mannual I
FYI:http://www.wangafu.net/~nickm/libevent-book/ This lib is a integral of asynchronous IO. we shoul ...
- UVA - 247 Calling Circles(Floyd求传递闭包)
题目: 思路: 利用Floyd求传递闭包(mp[i][j] = mp[i][j]||(mp[i][k]&&mp[k][j]);),当mp[i][j]=1&&mp[j][ ...
- Redis多实例配置以及主从同步
一.多实例配置 1.准备俩配置文件,开两个就准备两个 redis-6380.conf redis-6381.conf 2.分别写入配置信息(这里简化了配置) # 运行在6380端口 bind 172. ...