hdu 1213 How many Tables (UFS 간단 하고 집합 찾기)
Description
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
Sample Input
2
5 3
1 2
2 3
4 5
5 1
2 5
Sample Output
2
4
학습 및 집합 연습 의 첫 번 째 문제 (간단 할 수록 좋 습 니 다 ~)
질문
N 개의 요소 가 있 는 집합 응용 문제 에서 우 리 는 보통 시작 할 때 모든 요소 로 하여 금 하나의 단일 요소 의 집합 을 구성 하 게 한 다음 에 일정한 순서에 따라 같은 그룹 에 속 하 는 요소 가 있 는 집합 을 합병 시 키 고 그 사이 에 하나의 요소 가 어느 집합 에 있 는 지 반복 적 으로 찾 아야 한다.
예 를 들 어 어느 도시 에 n 명 이 살 고 있 습 니 다. 아 는 사람 은 친구 가 아니면 적 입 니 다. 그리고 만족 합 니 다. 제 친구 의 친 구 는 제 친구 입 니 다.n 개인 에 관 한 m 조 정보 (즉, 두 사람 은 친구) 를 알 고 있 습 니 다. 친구 인 모든 사람 이 반드시 같은 단체 에 속한다 고 가정 하면 이 도시 에 최대 몇 개의 단체 가 있 는 지 계산 하 십시오.
헤헤, 그 용 도 를 알 겠 군.그리고 수집 한 에센스: 경로 압축.
시간 과 공간 자원 을 크게 절약 하 다.그것 은 아래 코드 에 있다.
드러나다그리고 집합 을 찾 는 쓰기 방법 은 아래 의 한 가지 뿐만 아니 라 광범 위 하 게 응용 되 는 '배열 + 함수 모듈' 도 있다.
f[x]=ff(f[x]);
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
hdu4671(다교리그 7--수 시뮬레이션)클릭하여 링크 열기 제목: n과 서버, m개의 데이터베이스가 있고 모든 데이터베이스는 서버를 연결해야 하지만 모든 데이터베이스는 서버를 연결하는 우선순위가 있습니다.모든 데이터베이스의 서버 우선순위를 구하다.또한 한...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.