7-1 File Transfer(25 分)
We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?
Input Specification:
Each input file contains one test case. For each test case, the first line contains N (2≤N≤104), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format:
I c1 c2
where I
stands for inputting a connection between c1
and c2
; or
C c1 c2
where C
stands for checking if it is possible to transfer files between c1
and c2
; or
S
where S
stands for stopping this case.
Output Specification:
For each C
case, print in one line the word "yes" or "no" if it is possible or impossible to transfer files between c1
and c2
, respectively. At the end of each case, print in one line "The network is connected." if there is a path between any pair of computers; or "There are k
components." where k
is the number of connected components in this network.
Sample Input 1:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S
Sample Output 1:
no
no
yes
There are 2 components.
Sample Input 2:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
I 1 3
C 1 5
S
Sample Output 2:
no
no
yes
yes
The network is connected.
题目大意:输入为c时查询两个数是否相同,为s时输出最大联通数。
思路:裸并查集
代码如下:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
using namespace std;
int p[1000000];
void int1(int n)
{
for(int i=1;i<=2*n;i++)
p[i]=i;
}
int find(int x)
{
int y=x,t;
while(y!=p[y]) y=p[y];
while(x!=y)
{
t=p[x];p[x]=y;x=t;
}
return x;
}
void bin(int x,int y)
{
int a=find(x);
int b=find(y);
if(a!=b)
{
if(p[a]<p[b])
{
p[b]=a;
}else
{
p[a]=b;
}
}
}
int main()
{
int t,i,j,a,b;
char ch;
scanf("%d",&t);
int1(t);
while(scanf("%c",&ch)!=EOF&&ch!='S')
{
if(ch=='I')
{
scanf("%d%d",&a,&b);
bin(a,b);//注意此处若改为p[a]=b会错,原因是未进行路径压缩
}
if(ch=='C')
{
scanf("%d%d",&a,&b);
if(find(a)==find(b))
printf("yes\n");
else printf("no\n");
}
}
int s=0;
for(i=1;i<=t;i++)
{
if(p[i]==i)
s++;
}
if(s==1) printf("The network is connected.\n");
else printf("There are %d components.\n",s);
return 0;
}
7-2 朋友圈(25 分)
某学校有N个学生,形成M个俱乐部。每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈。一个学生可以同时属于若干个不同的俱乐部。根据“我的朋友的朋友也是我的朋友”这个推论可以得出,如果A和B是朋友,且B和C是朋友,则A和C也是朋友。请编写程序计算最大朋友圈中有多少人。
输入格式:
输入的第一行包含两个正整数N(≤30000)和M(≤1000),分别代表学校的学生总数和俱乐部的个数。后面的M行每行按以下格式给出1个俱乐部的信息,其中学生从1~N编号:
第i个俱乐部的人数Mi(空格)学生1(空格)学生2 … 学生Mi
输出格式:
输出给出一个整数,表示在最大朋友圈中有多少人。
输入样例:
7 4
3 1 2 3
2 1 4
3 5 6 7
1 6
输出样例:
4
思路:还是裸的并查集
代码如下:
#include<iostream>
#include<string.h>
using namespace std;
int par[30001],num[30001];
int fi(int x)
{
int r=x;
while(r!=par[r])
r=par[r];
int i=x,j;
while(i!=r)
{
j=par[i];
par[i]=r;
i=j;
}
return r;
}
void Join(int x,int y)
{
int fx=fi(x);
int fy=fi(y);
if(fx!=fy)
par[fx]=fy;
}
int main()
{
std::ios::sync_with_stdio(false);
int n,m,a,b,k;
cin>>n>>m;
for(int i=1;i<=n;i++)
par[i]=i;
while(m--)
{
cin>>k;
cin>>a;
k--;
while(k--)
{
cin>>b;
Join(a,b);
}
}
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
{
num[fi(i)]++;
}
int ans=0;
for(int i=1;i<=n;i++)
{
if(num[i]>ans)
ans=num[i];
}
cout<<ans<<endl;
return 0;
}