HDU 5416
CRB and Tree
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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?
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
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.
///
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define push_back pb
#define mem(a) memset(a,0,sizeof(a))
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a) scanf("%d",&a)
#define mod 1000000007
#define inf 100000
#define maxn 300000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//******************************************************************
struct ss
{
int to,next,v;
}e[];
int vis[],head[],t,n;
ll hashs[];
void add(int u,int v,int w)
{
ss kk={v,head[u],w};
e[t]=kk;
head[u]=t++;
}
void dfs(int x,int pre)
{
hashs[pre]++;
vis[x]=;
for(int i=head[x];i;i=e[i].next)
{//TS;
if(vis[e[i].to])continue;
dfs(e[i].to,pre^e[i].v);
}
}
int main()
{
int a,b,c;
int T=read();
while(T--)
{
t=;mem(head);mem(hashs);mem(vis);
n=read();
FOR(i,,n-)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
//cout<<t<<endl;
dfs(,);
/// FOR(i,0,3)cout<<hashs[i]<<" ";
int q=read();
FOR(i,,q)
{ ll ans=;
a=read(); for(int j=;j<=maxn;j++)
{if(!hashs[j])continue;
if((a^j)==j)ans+=(hashs[j]*(hashs[j]-));
else {
ans+=(hashs[j]*hashs[a^j]);
}
//if(ans!=0)cout<<j<<" "<<ans<<endl;
}ans=ans/;
if(a==)ans+=n;
cout<<ans<<endl;
} }
return ;
}
代码
HDU 5416的更多相关文章
- 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 (bfs)
题目链接: Hdu 5416 CRB and Tree 题目描述: 给一棵树有n个节点,树上的每条边都有一个权值.f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v) ...
- 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——————【DFS搜树】
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5416 CRB and Tree (技巧)
题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...
- hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)
CRB and Tree Time Limit: 8000/4000 MS (J ...
- HDU 5416 CRB and Tree (2015多校第10场)
欢迎參加--每周六晚的BestCoder(有米!) CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 5416 CBR and tree
#include<bits/stdc++.h> using namespace std; #define for(i,a,b) for(int i=a;i<=b;++i) //T,N ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- Java设计模式之策略模式(Strategy Pattern)
简介 策略模式定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 组成 1.抽象策略角色(Strategy): 策略类,通常由一个接口或者抽象类实现. 2.具 ...
- 安装php扩展(以swoole)为例
一.下载swoole到/usr/local/src目录下,操作 git clone https://gitee.com/swoole/swoole.git; 二.cd swoole,phpize(如果 ...
- DNS服务器原理简述、搭建主/从DNS服务器并实现智能解析
1. TLD:Top Level Domain 顶级域名 组织域:.com, .net, .org, .gov, .edu, .mil 国家域:.iq, .tw, .hk, .jp, .cn, ... ...
- JavaScript关键字
JavaScript关键字 制作人:全心全意 abstract continue finally instanceof private this boolean default float int p ...
- asp.net:Parser Error & HTTP 错误 500.21 - Internal Server Error
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABEcAAAF8CAIAAACiwUIQAAAgAElEQVR4nO2dyZHzOpZGaVEaADNkBB ...
- 集训第六周 数学概念与方法 J题 数论,质因数分解
Description Tomorrow is contest day, Are you all ready? We have been training for 45 days, and all g ...
- Postman 安装与使用
本文是基于 Chrome 浏览器的扩展插件来进行的安装,并非单独应用程序. 1. 官网安装(容易打开出错) 打开官网,https://www.getpostman.com 点击那个灰灰色的「Chro ...
- CodeForces788B 欧拉路
B. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Spring Data Jpa系列教程--------实体解析和关联关系
Spring Data Jpa是基于HIbernate开发的,所以建立实体建的实体和映射关系需要好好好的去了解一下,本文有以下内容,实体管理器介绍,实体与数据库表的映射介绍,关联关系(一对多,多对多) ...
- redis持久化机制【十三】
一.Redis提供了哪些持久化机制: redis的高性能是因为其所有数据都存在了内存中 ,为了使redis在重启之后数据仍然不丢失,需要将数据同步到硬盘中,这一过程就是持久化. redis支持两种方式 ...