1298. Communication Problems
★Input file:jdltt.in
Output file:jdltt.out
Simple comparison
Time Limit: 1 s memory limit: 128 MB
[Description]
A basketball team has n basketball players, each of which has contact information (such as phone and email ). However, not all players' contact information is disclosed. Only some of the team members know their contact information. Ask the team members how many groups they can be divided into. The group members can notify each other (including one team member and one group, indicating that they can notify themselves ).
[Input format]
The input file contains several lines.
The first line, an integer N, indicates a total of n players (2 <= n <= 100)
There are several rows below. Each row has two numbers A, B, and A and B are member numbers, indicating that a knows the communication method of B.
[Output format]
The output file contains several rows.
The first line, an integer m, indicates that the Group can be divided into M groups. There are m rows below, and each row has several integers, indicating the group member number, the output sequence is ascending by number.
[Example input]
12
1 3
2 1
2 4
3 2
3 4
3 5
4 6
5 4
6 4
7 4
7 8
7 12
8 7
8 9
10 9
11 10
[Sample output]
8
1 2 3
4 6
5
7 8
9
10
11
12
Brute force code
#include<cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;int n,m,k,x,y;#define N 110int a[N][N],b[N/2][N],vis[N],jl[N],zj=0,tot=0,t1=0,qi;void bj(int z){ for(int i=0;i<zj;i++) vis[jl[i]]=1; sort(jl,jl+zj); zj=unique(jl,jl+zj)-jl; for(int i=0;i<zj;i++) b[jl[0]][i]=jl[i]; tot++; for(int i=0;i<zj;i++) jl[i]=0; zj=0;}void dfs(int u,int v){ if(vis[u]||vis[v]||u==v) return; if(v==qi) {bj(zj); return;} if(a[u][v]&&!vis[v]) { jl[zj++]=v; for(int j=1;j<=n;j++){ if(a[v][j]&&!vis[j]) dfs(v,j); } } if(u==qi&&zj>0) zj--;}int main(){ freopen("jdltt.in","r",stdin); freopen("jdltt.out","w",stdout); scanf("%d",&n); while(scanf("%d%d",&x,&y)==2){ a[x][y]=1; } for(qi=1;qi<=n;qi++){ for(int j=1;j<=n;j++) if(qi!=j&&a[qi][j]){ if(!vis[qi]){ if(!vis[j]){ jl[zj++]=qi; dfs(qi,j); }else continue; }else break; } } for(int i=1;i<=n;i++) if(!vis[i]) t1++; printf("%d\n",t1+tot); for(int i=1;i<=n;i++){ if(b[i][0]!=0){ for(int j=0;b[i][j];j++){ printf("%d ",b[i][j]); } printf("\n"); } else if(!vis[i]) printf("%d\n",i); } return 0;}
1298. Communication Problems