[并查集]HOJ 2745 Indirect Know

传送门:Indirect Know

Indirect Know

My Tags   (Edit)
  Source : xiaoE
  Time limit : 3 sec   Memory limit : 64 M

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

5
2 1 2
1 0
1 0
1 4
1 3

Sample Output

4

解题报告:

基础并查集。代码如下:

#include
#include
using namespace std;
int father[605],num[605];
int n,m;
void makeset(int n){
    for(int j=0;j


你可能感兴趣的:(并查集,2014寒假)