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>
#include<cstring>
using namespace std;
int map[1010][1010];
bool flag[1010];
int N,M,K;
void dfs(int a)
{
flag[a] = true;
int i;
for(i = 1;i <= N;i++)
{
if(map[a][i] == 1 && flag[i] == false) dfs(i);
}
}
int main()
{
int i,j;
int a,b;
cin>>N>>M>>K;
memset(map,0,sizeof(map));
for(i = 0;i < M;i++)
{
cin>>a>>b;
map[a][b] = 1;
map[b][a] = 1;
}
int temp;
for(i = 0;i < K;i++)
{
memset(flag,0,sizeof(flag));
cin>>temp;
flag[temp] = true;
int count = 0;
for(j = 1;j <= N;j++)
{
if(flag[j] == false)
{
count++;
dfs(j);
}
}
printf("%d\n",count - 1);
}
return 0;
}
用并查集进行求解:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int N,M,K;
int map[1010][1010];
int count;
int father[1010];
int rank1[1010];
int Find_father(int i)
{
if(father[i] != i)father[i] = Find_father(father[i]);
return father[i];
}
void unio(int a,int b)
{
int temp1 = Find_father(a);
int temp2 = Find_father(b);
if(temp1 != temp2)
{
count++;
if(rank1[temp1] >rank1[temp2])
father[temp2] = temp1;
else
{
father[temp1] = temp2;
if(rank1[temp1] == rank1[temp2])++rank1[temp1];
}
}
}
int main()
{
int i,j,k1;
int a,b;
//cin>>N>>M>>K;
scanf("%d%d%d",&N,&M,&K);
memset(map,0,sizeof(map));
for(i = 0;i < M;i++)
{
//cin>>a>>b;
scanf("%d%d",&a,&b);
map[a][b] = 1;
map[b][a] = 1;
}
if(N == 1)
{
for(i = 1;i <= K;i++)
{
int temp;
//cin>>temp;
//cout<<"0"<<endl;
scanf("%d",&temp);
printf("0\n");
}
}
else
{
for(i = 1;i <= K;i++)
{
int temp;
count = 0;
//cin>>temp;
scanf("%d",&temp);
for(j = 1;j <= N;j++)
{
father[j] = j;
rank1[j] = 0;
}
for(j = 1;j <= N;j++)
{
for(k1 = j + 1;k1 <= N;k1++)
{
if(j != temp && k1 != temp && map[j][k1] == 1)unio(j,k1);
}
}
//cout<<N - count - 2<<endl;
printf("%d\n",N - count - 2);
}
}
return 0;
}
开始的时候运行超时了,后来用到rank后就好了!