题目连接:https://www.patest.cn/contests/pat-a-practise/1013
原题如下:
It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.
For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.
Input
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.
Output
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.
Sample Input3 2 3 1 2 1 3 1 2 3Sample Output
1 0 0
这道题目如果思路打开,能想到连通集,继而想到题中所说的需要修的路其实就是将分散的连通集合并在一起(注意单独的点也是一个连通集),因此,修路的
条数=连通集个数-1。我一开始并没有想到这一块,也是参考了网友的方法,大致有三种,集合,DFS,BFS。
1、集合:
1 #include<stdio.h> 2 #define MaxN 1005 3 struct node{ 4 int x; 5 int y; 6 }Node[MaxN*MaxN];//注意M最大为(N*(N-1))/2 7 8 int Find(int *city,int x) 9 { 10 if (city[x]<0)return x; 11 else city[x]=Find(city,city[x]); 12 } 13 void Union(int *city,int x,int y) 14 { 15 int Root1,Root2; 16 Root1=Find(city,x); 17 Root2=Find(city,y); 18 if (Root1!=Root2) 19 { 20 if (city[Root1]<city[Root2]) 21 { 22 city[Root1]+=city[Root2]; 23 city[Root2]=Root1; 24 } 25 else { 26 city[Root2]+=city[Root1]; 27 city[Root1]=Root2; 28 } 29 } 30 } 31 32 int main() 33 { 34 int N,K; 35 long long int M; 36 int visited[MaxN]={0},city[MaxN]; 37 scanf("%d %d %d",&N,&M,&K); 38 int v,w; 39 long long int i,j; 40 for (i=1;i<=N;i++)city[i]=-1; 41 for (i=0;i<M;i++) 42 { 43 scanf("%d %d",&v,&w); 44 Node[i].x=v;Node[i].y=w; 45 } 46 int cnt=0; 47 while (K--) 48 { 49 for (i=1;i<=N;i++)city[i]=-1; 50 scanf("%d",&v); 51 for (i=0;i<M;i++) 52 { 53 if (Node[i].x==v || Node[i].y==v)continue; 54 Union(city,Node[i].x,Node[i].y); 55 } 56 for (i=1;i<=N;i++) //统计有几个根节点 57 { 58 if(i==v)continue; 59 if (city[i]<0)cnt++; 60 } 61 printf("%d ",cnt-1); 62 cnt=0; 63 } 64 return 0; 65 }
2、DFS:
1 #include<stdio.h> 2 #define MaxN 1005 3 int G[MaxN][MaxN]={0}; 4 int visited[MaxN]={0}; 5 int N,M,K; 6 void DFS(int i) 7 { 8 int j; 9 visited[i]=1; 10 for (j=1;j<=N;j++) 11 { 12 if (!visited[j] && G[i][j]==1)DFS(j); 13 } 14 } 15 16 int main() 17 { 18 19 scanf("%d %d %d",&N,&M,&K); 20 int i,v,w,m,j,cnt=0; 21 22 for (i=0;i<M;i++) 23 { 24 scanf("%d %d",&v,&w); 25 G[v][w]=G[w][v]=1; 26 } 27 28 for (i=0;i<K;i++) 29 { 30 scanf("%d",&v); 31 visited[v]=1; 32 for (j=1;j<=N;j++) 33 { 34 if (!visited[j]) //统计有几个连通集 35 { 36 cnt++; 37 DFS(j); 38 } 39 } 40 printf("%d ",cnt-1); 41 cnt=0; 42 for (m=1;m<=N;m++)visited[m]=0; 43 } 44 return 0; 45 }
3、BFS
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<stdbool.h> 4 #define Max 1005 5 int G[Max][Max]={0}; 6 int visited[Max]={0}; 7 8 typedef struct QNode{ 9 int front; 10 int rear; 11 int Maxsize; 12 int *Data; 13 }Quenue; 14 15 Quenue *CreatQ(int n) 16 { 17 Quenue*Q; 18 Q=(Quenue*)malloc(sizeof(struct QNode)); 19 Q->front=Q->rear=0; 20 Q->Maxsize=n+1; 21 Q->Data=(int *)malloc((n+1)*sizeof(struct QNode)); 22 return Q; 23 } 24 25 bool IsEmpty(Quenue*Q) 26 { 27 return (Q->front==Q->rear); 28 } 29 30 int Pop(Quenue*Q) 31 { 32 if (!IsEmpty(Q)) 33 { 34 Q->front=(Q->front+1)%(Q->Maxsize); 35 return (Q->Data[Q->front]); 36 } 37 } 38 39 bool IsFull(Quenue *Q) 40 { 41 return (Q->rear+1)%(Q->Maxsize)==Q->front; 42 } 43 44 void AddQ(Quenue *Q,int x) 45 { 46 if (!IsFull(Q)) 47 { 48 Q->rear=(Q->rear+1)%(Q->Maxsize); 49 Q->Data[Q->rear]=x; 50 } 51 } 52 53 int main() 54 { 55 int N,M,K; 56 scanf("%d %d %d",&N,&M,&K); 57 int i,j,v,w; 58 Quenue *Q; 59 Q=(Quenue*)malloc(sizeof(struct QNode)); 60 Q=CreatQ(N); 61 62 for (i=0;i<M;i++) 63 { 64 scanf("%d %d",&v,&w); 65 G[v][w]=G[w][v]=1; 66 } 67 int cnt=0; 68 while(K--) 69 { 70 scanf("%d",&v); 71 visited[v]=1; 72 for (i=1;i<=N;i++) 73 { 74 if (!visited[i]) 75 { 76 cnt++; 77 AddQ(Q,i); 78 while (!IsEmpty(Q)) 79 { 80 w=Pop(Q); 81 visited[w]=1; 82 for (j=1;j<=N;j++) 83 { 84 if (!visited[j] && G[w][j]==1)AddQ(Q,j); 85 } 86 } 87 } 88 } 89 printf("%d ",cnt-1); 90 cnt=0; 91 for (j=1;j<=N;j++)visited[j]=0; 92 } 93 return 0; 94 }