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 ...
随机推荐
- 零基础入门学习Python(24)--递归:汉诺塔
知识点 这节课主要讲解用递归的方法,实现汉诺塔的解答 对于游戏的玩法,我们可以简单分解为三个步骤: 1) 将前63个盘子从X移动到Y上. 2) 将最底下的第64个盘子从X移动到Z上. 3) 将Y上的6 ...
- pxc集群安装
一.pxc镜像url:https://hub.docker.com/r/percona/percona-xtradb-cluster/ 二.安装及重命名: 1.安装:docker pull perco ...
- centos passwo文件被删除
错误提示 该问题一般由/etc/passwd被清空,删除,移动,改名等造成,需要通过救援模式恢复,操作步骤如下 真实环境已经解决,这里使用vmware模拟.光盘启动,选择救援模式: 语言选择,键盘布局 ...
- tomcat排错以及优化
jstack $PID #查看java进程的状态,分析tomcat卡死原因,定位java进程卡死的函数,调整代码 #RUNNABLE,在虚拟机内执行的.运行中状态,可能里面还能看到locked字样,表 ...
- buf.keys()
buf.keys() 返回:{Iterator} 创建并返回一个包含 Buffer 键名(索引)的迭代器. const buf = Buffer.from('buffer'); for (var ke ...
- CSS+DIV命名
原地址:http://www.cnblogs.com/hylaz/archive/2012/10/27/2742743.html#2521377 页头:header 登录条:loginBar 标志:l ...
- Python之爬虫-中国大学排名
Python之爬虫-中国大学排名 #!/usr/bin/env python # coding: utf-8 import bs4 import requests from bs4 import Be ...
- 洛谷 4933 洛谷10月月赛II T2 大师
[题解] f[i][j]表示最后一个数为h[i],公差为j的等差数列的个数.n方枚举最后一个数和倒数第二个数转移即可.注意公差可能为负数,需要移动为正数再作为下标. #include<cstdi ...
- 杭电 1862 EXCEL排序(sort+结构体)
Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (<=100000 ...
- hdu 4948 Kingdom(推论)
hdu 4948 Kingdom(推论) 传送门 题意: 题目问从一个城市u到一个新的城市v的必要条件是存在 以下两种路径之一 u --> v u --> w -->v 询问任意一种 ...