[dfs]多校联合第三场 K Work
2015-07-29 09:47
330 查看
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
签到题,直接用简单的dfs即可
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; int n,k; vector<int> g[111]; //== g[111][k] int used[111]; int ans; int dfs(int x) { if (used[x] >=0) return used[x]; int sum = 0; for(int i = 0;i<g[x].size();i++) { sum += dfs(g[x][i]); } if (sum == k) ans++; used[x] = sum + 1; return used[x]; } int main() { while(scanf("%d%d",&n,&k) != EOF) { for(int i=0; i<=n; i++) g[i].clear(); for(int i=1; i<n; i++) { int u,v; scanf("%d%d",&u,&v); g[u].push_back(v); } ans=0; memset(used,-1,sizeof used); for(int i=1; i<=n; i++) { if(used[i]==-1) dfs(i); } printf("%d\n",ans); } return 0; }
相关文章推荐
- jQuery插件之ajaxFileUpload
- 优化数据页面(23)——添加链接
- UVA 10112 Myacm Triangles
- 学习重要,还是经营人脉重要?
- counter-reset属性和counter-increment属性
- 假如生活欺骗了你
- 配置内存中OLTP文件组提高性能
- 使用Eclipse设定Android开发环境
- LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)
- HDU 5317 RGCDQ
- HDU 2571 命运
- 优化数据页面(22)——n:n的数据关系
- HDU1828 Picture 周长并 线扫描
- WPF显示常用的几个显示文字控件TextBox, TextBlock, Lable
- 秋暮遣怀
- SQL Server里的闩锁介绍
- 从AD域获取用户AD信息
- 极客标签前端特效资源精品大荟萃#002
- Google Interview - Check String Order
- 10个加速Table Views开发的Tips