并查集:HDU5326-Work(并查集比较简单灵活的运用)
来源:互联网 发布:sqlserver exists 编辑:程序博客网 时间:2024/06/07 11:13
Work
HDU原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5326
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2109 Accepted Submission(s): 1236**
Problem Description
It’s an interesting experience to move from ICPC to work, end my college life and start a brand new journey in company.
As is known to all, every stuff in a company has a title, everyone except the boss has a direct leader, and all the relationship forms a tree. If A’s title is higher than B(A is the direct or indirect leader of B), we call it A manages B.
Now, give you the relation of a company, can you calculate how many people manage k people.**
Input
There are multiple test cases.
Each test case begins with two integers n and k, n indicates the number of stuff of the company.
Each of the following n-1 lines has two integers A and B, means A is the direct leader of B.
1 <= n <= 100 , 0 <= k < n
1 <= A, B <= n**
Output
For each test case, output the answer as described above.
Sample Input
7 2
1 2
1 3
2 4
2 5
3 6
3 7
Sample Output
2
解题心得:
- 这个题不难,但是容易受并查集的思维的影响,不能够太固化,对于新手来说是很好的一个并查集的应用,思维不能固化啊,不然要折腾好半天,代码很容易看明白还是看代码吧。
#include<bits/stdc++.h>using namespace std;const int maxn = 110;int father[maxn],sum[maxn];int n,m,ans;int find(int x){ int r; int k = x; while(k != father[k]) { r = father[k]; k = r; sum[r]++;//前面的根找的一个则加一,很简单, }}void merge(int a,int b){ father[b] = a;}int main(){ while(scanf("%d%d",&n,&m) != EOF) { ans = 0; memset(sum,0,sizeof(sum)); for(int i=0; i<=n+1; i++) father[i] = i; int N = n-1; while(N--) { int a,b; scanf("%d%d",&a,&b); if(a != b)//这里不用判断是否是同一根,直接建立关系就行了 { merge(a,b);//先合并 find(b);//逐层上找,直到找到最前的那个根 } } for(int i=1;i<=n;i++) if(sum[i] == m) ans++; printf("%d\n",ans); }}
- 并查集:HDU5326-Work(并查集比较简单灵活的运用)
- hdu5326 Work 并查集
- 【(伪)并查集】HDU5326-Work
- 并查集的运用
- hdu1272(并查集的运用)
- poj1661 并查集简单运用
- HDOJ3635 并查集简单运用
- hdu 5326 Work(并查集)
- hdu Work 5326(并查集)
- HDU 5326 Work(并查集)
- hdu 5326 Work(并查集)
- HDU 5326 Work (并查集)
- HDU 5326 Work(并查集)
- uva 10129 并查集的运用
- 【算法】并查集的运用
- 简单并查集
- 简单并查集
- 简单并查集
- spring+springmvc+mybatis(1)--整合和原始dao开发
- __weak
- Servlet基础(二):Servlet与JSP
- 百练OJ:2728:第一个C++程序
- sql把行带有逗号的转化为列-尘缘晖
- 并查集:HDU5326-Work(并查集比较简单灵活的运用)
- Dataframe的使用方法
- AlertDialog制作圆角 背景有直角问题
- java初学者的浅谈(5)StringBuffer类和StringBuilder类
- TCP三次握手详解及释放连接过程
- poi导出excel 之API整理
- 创建只能看到自己有权限库的登录名和用户
- javascript动画特效 多透明度 源代码
- Laravel框架一:原理机制篇