[并查集]HOJ 2745 Indirect Know
来源:互联网 发布:黑龙江东北网络台 v 编辑:程序博客网 时间:2024/05/21 21:47
传送门:Indirect Know
Indirect Know
Submitted : 299, Accepted : 165
Background
New term comes. And the new students of HIT@CS have already been in school. Every new one has known several other new ones. Because of the memory power, they can't know everyone others, and every one at most know fifteen others. As we known, bin3 is a very very gossipy boy, he wants to count how many pairs of them is indirect known each other.
We define indirect known below:
If bin3 know wywcgs, then bin3 indirect know wywcgs;
If bin3 indirect know wywcgs, and wywcgs know L, then bin3 indirect know L;
bin3 indirect know L, that means L also indirect know bin3;
Input
There are several cases.In every case, the first line is a positive integer n(n ≤ 600), standing for the new students'number.Next comes n lines, and in line i, first integer ki is the students'number that i know, next ki integers stand for the student i know. Here i from 0 to n-1.
Output
Only one integer stands for the number of pairs with indirect-know relation.
Sample Input
52 1 21 01 01 41 3
Sample Output
4
解题报告:
基础并查集。代码如下:
#include<iostream>#include<cstdio>using namespace std;int father[605],num[605];int n,m;void makeset(int n){ for(int j=0;j<n;j++){ father[j]=j; num[j]=1; }}int findset(int x){ return father[x]==x?x:father[x]=findset(father[x]);}void Union(int a,int b){ int x=findset(a); int y=findset(b); if(x==y) return; if(num[x]<=num[y]){ father[x]=y; num[y]+=num[x]; } else{ father[y]=x; num[x]+=num[y]; }}int main(){ int ans; while(scanf("%d",&n)==1){ ans=0; makeset(n); int a; for(int i=0;i<n;i++){ scanf("%d",&m); for(int j=0;j<m;j++){ scanf("%d",&a); Union(i,a); } } for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++) if(findset(i)==findset(j)) ans++; printf("%d\n",ans); } return 0;}
- [并查集]HOJ 2745 Indirect Know
- HOJ 1225 Suptermarket(并查集)
- [并查集]HOJ 2676 Relation
- 并查集 HOJ 2064 Journey to Tibet
- HOJ C - Portal——并查集+离线
- [并查集]HDU 1829/POJ 2492/HOJ 2319 A Bug's Life
- ICPCCamp 2016 Day 2 - ZhejiangU Contest(Easy When You Know How-并查集+rmq)
- bfs hoj Going to know him
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- error:求平方数时,求数的位数时,没考虑仔细flag,漏了一种情况
- udev的介绍
- [Leetcode] Longest Consecutive Sequence (Java)
- linux bash bc awk 浮点 计算 比较
- java中的反射
- [并查集]HOJ 2745 Indirect Know
- 中文计算机核心期刊排名及投稿信息
- 黑马程序员--异常
- Linux-CD挂载
- 很棒的node.js
- Fedora 10中开启root用户登录
- Fedora 10中开启root用户登录
- android:TableLayout表格布局详解
- web-front-编写高质量代码--Web前端开发修炼之道