1013. Battle Over Cities (25)
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一开始以为是求割点。。写了个程序还自我感觉良好,哭#include <iostream> int min(int x,int y){ return x>y?y:x; } using namespace std; int duan[1001]; int mark[1001]={0}; int n,m,k; int visit[1001]={0}; int goback[1001]={0}; int p=1; int parent[1001]={0}; int child[1001]={0}; int edge[1001][1001]; void DFS(int x) { mark[x]=1; visit[x]=goback[x]=p; p++; for(int i=1;i<=n;i++) if(edge[x][i]==1) { if(mark[i]==0) { DFS(i); parent[i]=x; goback[x]=min(goback[x],goback[i]); child[x]++; if(parent[x]==0&&child[x]>1) duan[x]=1; if(visit[x]<=goback[i]) duan[x]=1; } else { if(i!=parent[x]) goback[x]=min(goback[x],visit[i]); } } } int main() { cin>>n>>m>>k; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { edge[i][j]=-1; } for(int i=1;i<=n;i++) edge[i][i]=0; for(int i=1;i<=m;i++) { int x,y; cin>>x>>y; edge[x][y]=edge[y][x]=1; } DFS(1); for(int i=0;i<k;i++) { int tmp; cin>>tmp; cout<<duan[tmp]<<endl;} return 0; }
后来怎么也调不出错误,看了看别人的代码才发现读错题了,是用并查集,呜呜呜#include <iostream> using namespace std; int mark[1001]={0}; int n,m,k; int parent[1001]={0}; int edge[1001][1001]; int edgetmp[1001][1001]; int findpapa(int x) { if(parent[x]==x) return x; else { int z=findpapa(parent[x]); parent[x]=z; return z; } } int check() { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(edgetmp[i][j]==1&&findpapa(i)!=findpapa(j)) parent[findpapa(j)]=findpapa(i); } for(int i=1;i<=n;i++) mark[findpapa(i)]++; int cnt=0; for(int i=1;i<=n;i++) if(mark[i]>0) cnt++; return cnt-2; } int main() { cin>>n>>m>>k; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { edge[i][j]=-1; } for(int i=1;i<=n;i++) edge[i][i]=0; for(int i=1;i<=m;i++) { int x,y; cin>>x>>y; edge[x][y]=edge[y][x]=1; } for(int i=0;i<k;i++) { int tmp; cin>>tmp; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) edgetmp[i][j]=edge[i][j]; for(int i=1;i<=n;i++) { mark[i]=0; parent[i]=i; edgetmp[i][tmp]=edgetmp[tmp][i]=-1; } cout<<check()<<endl;} return 0; }
PS:做到1034回头看这道题,发现用DFS判断有几个独立堆更简单