杭电 4707 pet(并查集求元素大于k的集合)
Description
Input
Output
Sample Input
1
10 2
0 1
0 2
0 3
1 4
1 5
2 6
3 7
4 8
6 9
Sample Output
2 大意:
n个接点从0~n。n-1中关系,问到0节点距离大于k的节点数。
#include<cstdio>
int n,b,a,k,fa[],i,ans;
int find(int a) //只查询不压缩
{
int r=a;
while(r != fa[r])
{
r=fa[r];
}
return r;
}
int f1(int a) //深度搜索
{
int s=;
while(a != fa[a])
{
a=fa[a];
s++; //s表示到0节点的距离
}
return s;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&k);
for(i = ; i < n ;i++)
{
fa[i]=i;
}
for(i = ; i < n ;i++)
{
scanf("%d %d",&a,&b);
if(a>b) //令大的数为小的数的子节点
{
fa[a]=b;
}
else
{
fa[b]=a;
}
}
ans=;
for(i = n- ; i >= ; i--) //从最大的数开始找(数大的节点在树的下面 )
{
if(find(i) == )
{
if(f1(i) > k)
{
ans++;
}
}
}
printf("%d\n",ans);
}
}
杭电 4707 pet(并查集求元素大于k的集合)的更多相关文章
- 杭电--1162--Eddy's picture--并查集
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 杭电 1856 More is better (并查集求最大集合)
Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块
C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
- More is better——并查集求最大集合(王道)
Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
- BC68(HD5606) 并查集+求集合元素
tree Accepts: 143 Submissions: 807 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
- 利用并查集求最大生成树和最小生成树(nlogn)
hdu1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- 449B
B. Chtholly's request time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Java正确URL解码方式:URLDecoder.decode
//解码,为了解决中文乱码 String str = URLDecoder.decode(request.getParameter("orderJson"),"UTF-8 ...
- April Fools Contest 2017 B
Description Programmers' kids solve this riddle in 5-10 minutes. How fast can you do it? Input The i ...
- 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun
题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...
- 题解报告:poj 1738 An old Stone Game(区间dp)
Description There is an old stone game.At the beginning of the game the player picks n(1<=n<=5 ...
- [已读]Sass与Compass实战
介绍了Sass基础语法与Compass框架,这个网上参考文档就OK了,另外介绍了compass生成图片精灵和相应的css,貌似现在单纯用sass和compass的挺少,要不grunt,要不FIS,而g ...
- HDU 5808 Price List Strike Back bitset优化的背包。。水过去了
http://acm.hdu.edu.cn/showproblem.php?pid=5808 用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = ...
- MongoDB管理练习
一.索引 1.插入10W条数据 文档内容为:{name:zs-i,age:1} 2016-06-07T14:35:57.041+0800 I CONTROL [initandlisten] > ...
- NIO客户端主要创建过程
NIO客户端主要创建过程: 步骤一:打开SocketChannel,绑定客户端本地地址(可选,默认系统会随机分配一个可用的本地地址),示例代码如下: SocketChannel client ...
- iOS 二维码扫描 通过ZBar ZXing等第三方库
扫描二维码的开源库有很多如 ZBar.ZXing等 ZBar的使用方法: 下载ZBar SDK 地址https://github.com/bmorton/ZBarSDK ZBarSDK是一个开源的SD ...