항저우 전신에 대한 병렬 검색, 최단거리 제목 코드(학습ing)
7117 단어 최단로
hdu 1213
How Many Tables
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
이 문제는 주인이 밥을 살 때 서로 아는 (간접적으로) 한 테이블에 배치해야 한다는 뜻이다. 모두 몇 개의 테이블을 차려야 하는지 물어보는 것이 가장 간단한 병문집에 속한다. 1 #include<stdio.h>
2 #include<string.h>
3 int bin[200001];
4 int find(int x)
5 {
6 int r=x;
7 while(bin[r]!=r)
8 r=bin[r];
9 return r;
10 }
11 void merge(int x,int y)
12 {
13 int fx=find(x);
14 int fy=find(y);
15 if(fx!=fy)
16 bin[fx]=fy;
17 }
18 int main()
19 {
20 int n,i,m,h,k,count,j,a,b;
21 scanf("%d",&h);
22 for(k=1;k<=h;k++)
23 {
24 scanf("%d",&n);
25 for(i=1;i<=n;i++)
26 {
27 bin[i]=i;
28 }
29 for(scanf("%d",&m);m>0;m--)
30 {
31 scanf("%d %d",&a,&b);
32 merge(a,b);
33 }
34 for(count=0,i=1;i<=n;i++)
35 {
36 if(bin[i]==i)
37 count++;
38 }
39 printf("%d
",count);
40 }
41 return 0;
42 }
43
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
HDU 1860 - Currency Exchange(최단거리)
Several currency exchange points are working in our city.
Let us suppose that each point specializes in two particular c...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
1 #include<stdio.h>
2 #include<string.h>
3 int bin[200001];
4 int find(int x)
5 {
6 int r=x;
7 while(bin[r]!=r)
8 r=bin[r];
9 return r;
10 }
11 void merge(int x,int y)
12 {
13 int fx=find(x);
14 int fy=find(y);
15 if(fx!=fy)
16 bin[fx]=fy;
17 }
18 int main()
19 {
20 int n,i,m,h,k,count,j,a,b;
21 scanf("%d",&h);
22 for(k=1;k<=h;k++)
23 {
24 scanf("%d",&n);
25 for(i=1;i<=n;i++)
26 {
27 bin[i]=i;
28 }
29 for(scanf("%d",&m);m>0;m--)
30 {
31 scanf("%d %d",&a,&b);
32 merge(a,b);
33 }
34 for(count=0,i=1;i<=n;i++)
35 {
36 if(bin[i]==i)
37 count++;
38 }
39 printf("%d
",count);
40 }
41 return 0;
42 }
43
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
HDU 1860 - Currency Exchange(최단거리)Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular c...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.