hdu5606 tree (并查集)
tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 823 Accepted Submission(s): 394
for each test case,the first line is a nubmer n,means the number of the points,next n-1 lines,each line contains three numbers u,v,w,which shows an edge and its weight.
T≤50,n≤105,u,v∈[1,n],w∈[0,1]
in consideration of the large output,imagine ansi is the answer to point i,you only need to output,ans1 xor ans2 xor ans3.. ansn.
3
1 2 0
2 3 1
in the sample.
$ans_1=2$
$ans_2=2$
$ans_3=1$
$2~xor~2~xor~1=1$,so you need to output 1.
#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1e6+;
int flag[N],p[N];
int findset(int x)
{
if(x!=p[x])return p[x]=findset(p[x]);
return x;
}
int main()
{
int T,n,u,v,w;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
flag[i]=;
p[i]=i;
}
for(int i=;i<n-;i++)
{
scanf("%d%d%d",&u,&v,&w);
if(w==)
{
int a=findset(u);
int b=findset(v);
p[a]=b;
}
}
for(int i=;i<=n;i++)
{
flag[findset(i)]++;
}
int ans=flag[findset()];
for(int i=;i<=n;i++)
{
ans=(ans^flag[findset(i)]);
}
printf("%d\n",ans);
}
return;
}
hdu5606 tree (并查集)的更多相关文章
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- [Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Is It A Tree?(并查集)(dfs也可以解决)
Is It A Tree? Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
- tree(并查集)
tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- 树上统计treecnt(dsu on tree 并查集 正难则反)
题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...
随机推荐
- Downloading jQuery
Compressed and uncompressed copies of jQuery files are available. The uncompressed file is best used ...
- shell 遍历所有文件包括子目录
1.代码简单,但是难在校验,不像python那么好理解 建议在Notepad++下编辑. 2.注意引用linux命令的`是[tab]键上面那个 3.if[] 这里 Error : syntax er ...
- CSU 1663: Tree(树链剖分)
1663: Tree Time Limit: 5 Sec Memory Limit: 128 MB Submit: 26 Solved: 11 [Submit][id=1663"> ...
- cg语言的一些术语
在Cg中,用uniform修饰符声明一个参数表明它的值是由外部的数据源初始化的,而且在给定这批向量的处理中保持不变. Uniform inputs,表示一些与三维渲染有关的离散信息数据,这些数据通常由 ...
- pdf文件编辑
下载软件:Foxit PDF Editor,这个工具挺好用的,可以对pdf文件内容进行编辑 Foxit PDF Editor 是第一个真正的PDF文件编辑软件.许多人都希望能找到一个象编辑其它类型的文 ...
- json格式转数组注意事项
今天遇到一个特别奇葩的问题,json格式明明是正确的,转数组时却就是出不来,后来才发现是文件的编码问题,文件的编码一定要是utf-8无BOM格式,这点一定要切记!切记! 如果想确认json是否正确:h ...
- 一个Lumen多层拆分手记
这个项目除了最基本的MVC,routes和之前讲过的ServiceProvider(服务商)依赖注入,还有Action (动作) .Repositories(仓储)等... 先讲一下仓储吧, 一般JA ...
- Linux 随手记(文件操作)
新建文件夹 mkdir 文件夹名 新建文件 touch 文件名 重命名 mv 文件名 新文件名 将/a目录移动到/b下,并重命名为c mv /a /b/c 复制文件 cp [选项] 源文件或目录 目标 ...
- Python的Django框架中的Context使用
Python的Django框架中的Context使用 近期整理些Python方面的知识,一旦你创建一个 Template 对象,你能够用 context 来传递数据给它. 一个context是一系列变 ...
- 旋转卡壳求两个凸包最近距离poj3608
#include <iostream> #include <cmath> #include <vector> #include <string.h> # ...