HDU 5416 CRB and Tree(前缀思想+DFS)
CRB and Tree
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2112 Accepted Submission(s): 635
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.
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.
题目链接:HDU 5416
和NBUT上一道题很像,只是前者是统计路径异或和为0的个数,这个就稍微复杂一点,统计异或和为s的个数,显然异或和不管是线性的还是树形的都是符合异或性质的
即prefix_{R}^prefix_{L-1}=prefix_{L~R},由于给的是边权而不是点权,就不用处理LCA这个点的问题了,设s=i^j,则j=s^i,DFS一遍得到储存前缀异或和xorsum个数的cnt[]数组,然后分类讨论一下,若i==j由于u可以等于v,显然组合可能数为cnt[i]*(cnt[i]+1)/2(由于int的取整问题把除以二放到最后),否则显然是cnt[i]*cnt[j],最后由于你遍历的时候除了遍历到0其他情况会重复计算,比如s=1时,遍历到2会组合到3,遍历到3又组合到2,因此若s不为0的话则答案要除以2。
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1e5+7;
const int R=1<<17;
struct edge
{
int to,nxt,w;
};
edge E[N<<1];
int head[N],tot;
LL cnt[R]; void init()
{
CLR(head,-1);
tot=0;
CLR(cnt,0);
}
inline void add(int s,int t,int w)
{
E[tot].to=t;
E[tot].w=w;
E[tot].nxt=head[s];
head[s]=tot++;
}
void dfs(int u,int sum,int pre)
{
++cnt[sum];
for (int i=head[u]; ~i; i=E[i].nxt)
{
int v=E[i].to;
if(v!=pre)
dfs(v,sum^E[i].w,u);
}
}
int main(void)
{
int tcase,n,m,a,b,s,c,i;
scanf("%d",&tcase);
while (tcase--)
{
init();
scanf("%d",&n);
for (i=0; i<n-1; ++i)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
dfs(1,0,-1);
scanf("%d",&m);
while (m--)
{
LL ans=0;
scanf("%d",&s);
for (i=0; i<R; ++i)
{
if(i==(s^i))
ans+=(cnt[i]*(cnt[i]+1))>>1;
else
ans+=(cnt[i]*cnt[s^i]);
}
if(s)
ans>>=1;
printf("%I64d\n",ans);
}
}
return 0;
}
HDU 5416 CRB and Tree(前缀思想+DFS)的更多相关文章
- 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——————【DFS搜树】
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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(2015 Multi-University Training Contest 10)
CRB and Tree Time Limit: 8000/4000 MS (J ...
- 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 ...
- HDU 5274 Dylans loves tree(LCA+dfs时间戳+成段更新 OR 树链剖分+单点更新)
Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes on tree i ...
- HDU 2489 Minimal Ratio Tree(prim+DFS)
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- Java类与对象的基础学习
1. 请输入并运行以下代码,得到什么结果? public class Test{ public static void main(String args[]){ Foo obj1=new Foo(); ...
- JSP获取客户端的IP地址的方法
//声明以下方法 <%! public String getRemortIP(HttpServletRequest request) { if (request.getHeader(" ...
- Android Force Close的原因:
1.程序空指针: 2.程序加载的资源找不到: 3.未加载布局文件时直接使用了对象: 4.后台service挂掉导致不可捕捉的ANR或crash: 5.Activity未在配置文件中注册.
- Jenkins控制台中文输出乱码解决方法
1. 设置jenkins所在服务器环境变量,右键我的电脑→属性→高级系统设置→环境变量,添加JAVA_TOOL_OPTIONS 2.修改Tomcat配置,进入apache_tomcat/conf文件夹 ...
- zookeeper启动错误 transaction type: 2 error: KeeperErrorCode = NoNode for /hbase
hbase伪分布式,与zookeeper同一台机器的时候,运行一段时间,启动zookeeper的时候,日志中有如下错误,导致无法启动zookeeper java.io.IOException: Fai ...
- 每天一个linux命令--locate
linux下,不知道自己安装的程序放在哪里了,可以使用locate命令进行查找. [hongye@dev107 ~]$ locate activemq.xml /home/hongye/hongyeC ...
- BZOJ 1355 & KMP
BZOJ放这种丝帛我也是醉了... 不过来填一下求最小循环节的坑... 以这道题为例,相同文本串粘起来的串中取一小节,可以把任意一个字符看做文本串头. 那么我们一次KMP求出next函数然后显见,最后 ...
- hdu 2099 整除的尾数
Problem Description 一个整数,只知道前几位,不知道末二位,被另一个整数除尽了,那么该数的末二位该是什么呢? Input 输入数据有若干组,每组数据包含二个整数a,b(0< ...
- 阴影:box-shaw
box-shaw:0px 0px 2px 3px while; 水平方向 垂直方向 模糊半径 模糊系数(大小)颜色
- ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德
POJ 1061 青蛙的约会 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu Descr ...