2015 多校联赛 ——HDU5416(异或)
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.
3
1 2 1
2 3 2
3
2
3
4
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(异或)的更多相关文章
- 2015 多校联赛 ——HDU5299(树删边)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
- 2015 多校联赛 ——HDU5334(构造)
Virtual Participation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- 2015 多校联赛 ——HDU5302(构造)
Connect the Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 2015 多校联赛 ——HDU5294(最短路,最小切割)
Tricks Device Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 2015 多校联赛 ——HDU5325(DFS)
Crazy Bobo Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Tota ...
- 2015 多校联赛 ——HDU5316(线段树)
Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...
- 2015 多校联赛 ——HDU5323(搜索)
Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 2015 多校联赛 ——HDU5319(模拟)
Painter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- 2015 多校联赛 ——HDU5301(技巧)
Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...
随机推荐
- B-day7
1.昨天的困难,今天解决的进度,以及明天要做的事情 昨天的困难:美化了登录页面,对导入导出的bug进行相关修改,对用户编辑页面进行相关美化,对第三方逻辑进行相应调整. 今天解决的进度:解决了导入和导出 ...
- Django Haystack 全文检索与关键词高亮
Django Haystack 简介 django-haystack 是一个专门提供搜索功能的 django 第三方应用,它支持 Solr.Elasticsearch.Whoosh.Xapian 等多 ...
- 使用XIB实现嵌套自定义视图
在进行iOS开发的过程中,对于一些复杂的界面,我们可以通过Interface Builder这个Xcode集成的可视化界面编辑工具在完成,这回节省大部分时间以及代码量.它的使用方法这里不做介绍了,这次 ...
- python的dir、help、str用法
当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表.当没有为其提供参数时, 它返回当前模块中定义的名字的列表.dir() 函数使用举例: 1 2 3 4 5 6 >>& ...
- EasyUI中Tabs添加远程数据的方法。
tabs加载远程数据: $(function () { $("#btnquery").click(function () { if (!$("#tcontent" ...
- hadoop大数据技术架构详解
大数据的时代已经来了,信息的爆炸式增长使得越来越多的行业面临这大量数据需要存储和分析的挑战.Hadoop作为一个开源的分布式并行处理平台,以其高拓展.高效率.高可靠等优点越来越受到欢迎.这同时也带动了 ...
- SpringCloud的Config:ConfigServer注册到EurekaServer中,变成一个Eureka服务
一.概念与定义 1.将SpringCloud ConfigServer注册到 EurekaServer,以便ConfigClient以服务的方式引用ConfigServer 2.客户端不再引用 Con ...
- ORA-01578和ORA-26040--NOLOGGING操作引起的坏块-错误解释和解决方案(文档ID 1623284.1)
ORA-01578和ORA-26040--NOLOGGING操作引起的坏块-错误解释和解决方案(文档ID 1623284.1) (一)NOLOGGING操作引起的坏块(ORA-01578和ORA-26 ...
- js常用的数组方法
1.创建数组的基本方法: 1.1 空数组 var obj=new Array(); 1.2 指定长度数组 var obj=new Array(size); ...
- python基础(常用内容)
python基础(常用内容) 机器数: 一个数在计算机中的二进制表示形式就是机器数. 例如: +3用机器数表示就用<00000011>表示 -3用机器数表示就用<10000011&g ...