hdu 5326 Work【并查集】
来源:互联网 发布:hadoop java c 编辑:程序博客网 时间:2024/05/21 18:35
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5326
题意:n个点 ,给你m有向条边,表示起点是终点的父亲。问子孙节点个数是K的节点有几个?
解法:并查集。
代码:
#include <stdio.h>#include <ctime>#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>using namespace std;int is[110][110];int p[110];int num[110];int n, k;int findd(int x){ if (x == p[x]) return x; else return p[x] = findd(p[x]);}void un(int x, int y){ int a, b; a = findd(x); b = findd(y); if (a == b) return; p[b] = a;}int main(){ while (scanf("%d%d", &n, &k) != EOF) { memset(is, 0, sizeof(is)); memset(num, 0, sizeof(num)); for (int i = 1; i<=n; i++) p[i] = i; int x, y; for (int i = 1; i<n; i++) { scanf("%d%d",&x,&y); un(x,y); is[x][y] = 1; } for (int k = 1; k <= n;k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (is[i][k] == 1 && is[k][j] == 1) { is[i][j] = 1; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (p[j] == p[i] && i != j && is[i][j] == 1) num[i]++; } } int ans = 0; for (int i = 1; i <= n; i++) if (num[i] == k) ans++; printf("%d\n", ans); }}
0 0
- hdu 5326 Work【并查集】
- hdu 5326 Work(并查集)
- hdu Work 5326(并查集)
- HDU 5326 work 【并查集】
- HDU 5326 Work(并查集变种)
- HDU 5326 Work(并查集)
- hdu 5326 Work(并查集)
- HDU Problem 5326 Work 【并查集】
- HDU 5326 Work (并查集)
- HDU 5326 Work(并查集)
- hdu 5326 Work 并查集
- HDU-5326-Work【并查集(有向)】
- HDOJ 5326 Work (并查集)
- hdoj 5326 Work <半步并查集>
- hdoj 5326 Work【并查集】
- HDU 5326 Work 并查集 (2015 Multi-University Training Contest 3 2015多校联合)
- HDU 5326 Work(并查集)(2015 Multi-University Training Contest 3)
- HDU:5326 Work(并查集+小技巧统计每个人的手下个数)
- iOS 苹果官方Demo合集
- Scala GUI编程
- noip2010 乌龟棋
- linux udp 单播组播广播实现
- hdu 3635 并查集
- hdu 5326 Work【并查集】
- 2015年7月28日--博客开通
- 在Eclipse中关联源代码
- 母函数模板解释
- JS脚本无法调试
- java基础之集合函数-List
- HDU 5316 Magician
- Java学习之路0728(十二)(Class类、反射、Annotation功能注解)
- 单例类Singleton