[PAT] Grade 1013 Battle Over Cities (25 minutes) (disjoint-set, simply connected graph)

Meaning of the questions:

Enter three integers N, M, K (N <= 1000, the fourth data 1e5 <= M <= 1e6). There are 1 ~ N cities, M highways, K times asked Always ask enter a city occupied by the enemy, all the highways and the city can not use all connected, how many highway rest can increase demand N-1 cities in China Unicom. (Otherwise might not be communication between the city, the original assume that only through China Unicom 0, 4 data).

trick:

The same code to pay a lot of times, a few times would be the first four points out. (Doubtful)

Code:

#include<bits/stdc++.h>
using namespace std;
int a[1000007],b[1000007];
int fa[1007];
int find_(int x){
if(fa[x]==x)
return x;
else
return fa[x]=find_(fa[x]);
}
int main(){
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=m;++i)
cin>>a[i]>>b[i];
for(int i=1;i<=k;++i){
int x;
cin>>x;
for(int j=1;j<=n;++j)
fa[j]=j;
for(int j=1;j<=m;++j){
if(a[j]==x||b[j]==x)
continue;
int t=find_(a[j]);
int tt=find_(b[j]);
if(t!=tt)
fa[t]=tt;
}
int cnt=-1;//联通块的数量-1
for(int j=1;j<=n;++j){
IF (J == X)
Continue;
IF (FA [J] == J)
CNT ++;
}
COUT CNT << << "\ n-"; // Number of blocks Unicom -1
}
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11243535.html