tree(简单并差集)
tree
There is a tree(the tree is a connected graph which contains nn points and n-1n−1 edges),the points are labeled from 1 to nn,which edge has a weight from 0 to 1,for every point i\in[1,n]i∈[1,n],you should find the number of the points which are closest to it,the clostest points can contain ii itself.
the first line contains a number T,means T test cases.
for each test case,the first line is a nubmer nn,means the number of the points,next n-1 lines,each line contains three numbers u,v,wu,v,w,which shows an edge and its weight.
T\le 50,n\le 10^5,u,v\in[1,n],w\in[0,1]T≤50,n≤105,u,v∈[1,n],w∈[0,1]
for each test case,you need to print the answer to each point.
in consideration of the large output,imagine ans_iansi is the answer to point ii,you only need to output,ans_1~xor~ans_2~xor~ans_3..~ans_nans1 xor ans2 xor ans3.. ansn.
1
3
1 2 0
2 3 1
1 in the sample. ans_1=2ans1=2 ans_2=2ans2=2 ans_3=1ans3=1 2~xor~2~xor~1=12 xor 2 xor 1=1,so you need to output 1.
题解:找每个点距离自己最近的点的个数的异或值,注意最近点包括自己;
思路:并差集,将权值为0的点组成一颗树,这棵树代表的就是距离自己最近的点的个数;
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAXN=100010;
int a[MAXN];
int n;
struct Node{
int u,v,w;
Node init(){SI(u);SI(v);SI(w);}
}dt[MAXN];
int pre[MAXN],num[MAXN];
int find(int x){
return pre[x]=x==pre[x]?x:find(pre[x]);
}
void merge(Node a){
int f1=find(a.u),f2=find(a.v);
if(pre[f1]!=f2)pre[f2]=f1,num[f1]+=num[f2];
}
void initial(){
for(int i=1;i<=n;i++)pre[i]=i,num[i]=1;
}
int main(){
int T;
SI(T);
while(T--){
mem(a,0);
SI(n);
initial();
int u,v,w;
for(int i=0;i<n-1;i++){
dt[i].init();
w=dt[i].w;
if(!w)merge(dt[i]);
}int ans=0;
for(int i=1;i<=n;i++)ans^=num[pre[i]];
printf("%d\n",ans);
}
return 0;
}
tree(简单并差集)的更多相关文章
- BTree和B+Tree 简单区别
本篇作用于各种树之间的区别,非算法详细介绍,只是给我们这种非科班出身的一种大概的印象,现在网上更多是讲各种树的怎么实现的细节问题,本篇不涉及那么高深,如果详细了解可以查阅他人的资料,很多大神已经说的很 ...
- 关于Trie Tree简单实现
最近突然有兴致hiho一下了,实现了下trie tree,感觉而言,还是挺有意思的,个人觉得这货不光可以用来查单词吧,其实也可以用来替代Hash,反正查找,插入复杂度都挺低的,哈哈,啥都不懂,瞎扯.. ...
- Huffman Tree 简单构造
//函数:构造Huffman树HT[2*n-1] #define MAXVALUE 9999//假设权值不超过9999 #define MAXLEAF 30 #define MAXNODE MAXLE ...
- Codeforces 1153D Serval and Rooted Tree (简单树形DP)
<题目链接> 题目大意: Serval拥有的有根树有n个节点,节点1是根. Serval会将一些数字写入树的所有节点.但是,有一些限制.除叶子之外的每个节点都有一个写入操作的最大值或最小值 ...
- 【hdu6121】 Build a tree 简单数学题
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6121 我好像推得挺久的诶..... 题目大意:给你一棵有$n$个点的树,根节点为$0$,对于其余节点 ...
- easy html+css tree 简单的HTML+css导航树
code: show:
- HDU 6228 tree 简单思维树dp
一.前言 前两天沈阳重现,经过队友提点,得到3题的成绩,但是看到这题下意识觉得题目错了,最后发现实际上是题目读错了....GG 感觉自己前所未有的愚蠢了....不过题目读对了也是一道思维题,但是很好理 ...
- 二叉树:B+tree等
二叉树:(树是一种可以递归定义数据结构) 度:节点的个数 深度:层数(即从根点到叶子节点的层数) 满二叉树:指底层叶子节点左右均存在的二叉树. 完全二叉树:指底层叶子节点的右侧均存在的二叉树. 一般二 ...
- LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)
669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...
随机推荐
- 解决IE8下VS2005,VS2008一些向导提示脚本错误问题
Some VS2005 and VS2008 Wizards Pop Up Script Error. Visual C++ team has discovered that after inst ...
- Windows下载安装jmeter
一.下载 jmeter下载地址: http://jmeter.apache.org/download_jmeter.cgi Binaries-apache-jmeter-3.0.zip 二.安装 1. ...
- chrome实现全浏览器跨域ajax请求
如图,在chrome快捷方式上打开属性栏,在‘目标’栏加上后缀--disable-web-security --user-data-dir.即可实现在此浏览器上所有网页的跨域请求.
- github/python/ show me the code 25题(一)
先上网址 https://github.com/Show-Me-the-Code/show-me-the-code 初学python拿来练手,记住一些常用的库和函数 第 0000 题:将你的 QQ 头 ...
- 自定义Edit控件控制输入范围
//自定义Edit,实现十六进制输入控制,使用OnChar()函数实现 //MyEdit.h #pragma once class CMyEdit : public CEdit { DECLARE_D ...
- Entity Framework 数据部分更新之Attach &&Detach
我们经常会遇到这样的问题:Update一个entity的部分数据时,通常需要new一个新的对象,然后事这新的对象Attach到Context中,代码如下所示: /// <summary> ...
- java如何在函数中调用主函数的数组
import javax.swing.JOptionPane; public class Test { /** * @zdz */ public static void main(String[] a ...
- linux 磁盘空间扩容 vg(+pv) lv(+空间) lv(缩减磁盘空间)
preFace APP scenario description: 当你未能合理的规划存储时,在后期的维护工作中可能会涉及的存储的 再规划(eg,某一个 or 数个App 对某一个lv 即挂载点写Bi ...
- 用showModalDialog写的简单弹出框传参与反参
vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures]) sURL -- 必选参数,类型:字符串.用来指定对话框要 ...
- Qt通过Http上传文件(路过)
web端: <?php $c = $GLOBALS['HTTP_RAW_POST_DATA']; $n = $_GET["filename"]; $fp = fopen($n ...