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 Specification:
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 Specification:
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.
Sample Input:
3 2 3
1 2
1 3
1 2 3
Sample Output:
1
0
0
此道題目看起來比較簡單,思路也比較簡單,本質上只要求出連通圖的個數,那麼問題就迎刃而解,但是最終解決問題花費了大量的時間 。
Vision 1:
建立一個鄰接矩陣,再用DFS算法求解連通圖個數
#include <iostream>
#include<string.h>
using namespace std;
int G[1000][1000]={0};
int visit[1000]={0};
int GC[1000][1000]={0};
int N,M,K;
void DFS(int u ){
visit[u]=1;
for(int i=1;i<=N;i++){
if (GC[u][i]==1&&visit[i]==0)
DFS(i);
}
}
int main()
{
cin>>N>>M>>K;
for(int i=0;i<M;i++){
int m,n;
cin>>m>>n;
G[m][n]=G[n][m]=1;
}
for(int k=0;k<K;k++){
int l,gnum=0;
cin>>l;
memset(visit,0,sizeof(visit));
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
GC[i][j]=G[i][j];
}
}
for(int i=1;i<=N;i++){
if (GC[l][i]==1) GC[l][i]=GC[i][l]=0;
}
for(int i=1;i<=N;i++){
if (!visit[i]) DFS(i),gnum++;
}
cout<<gnum-2<<endl;
}
}
結果出現了超時問題。
Version 2:
分析數據結構對其改良,鄰接表可能無關的遍歷次數過多,造成時間浪費,改用連接表:
#include <iostream>
#include<string.h>
#include<vector>
using namespace std;
//DFS計算連通圖,最後一個用例超時
vector<int> V[1000];
int visit[1000]={0};
int N,M,K;
void DFS(int u ){
visit[u]=1;
for(int i=0;i<V[u].size();i++){
int c=V[u][i];
if (visit[c]==0)
DFS(c);
}
}
int main()
{
cin>>N>>M>>K;
for(int i=0;i<M;i++){
int m,n;
cin>>m>>n;
V[m].push_back(n);
V[n].push_back(m);
}
for(int k=0;k<K;k++){
int l,gnum=0;
cin>>l;
memset(visit,0,sizeof(int)*(N+1));
visit[l]=1;
for(int i=1;i<=N;i++){
if (!visit[i]) DFS(i),gnum++;
}
if(gnum==0) cout<<0<<endl;
else cout<<gnum-1<<endl;
}
}
結果還是超時
Version 3:
改用並查集來解決問題:
#include <iostream>
#include<vector>
#include<stdio.h>
using namespace std;
struct Edge{
int u,v;
};
vector<Edge> E;
int tribe[1000];
int find_tribe(int r){
int p=r;
while(tribe[p]!=p)
p=tribe[p];
while(tribe[r]!=p){
r=tribe[r];
tribe[r]=p;
}
return p;
}
void merge_tribe(int u,int v){
int fx=find_tribe(u);
int fy=find_tribe(v);
tribe[fx]=fy;
}
int main()
{
int N,M,K;
cin>>N>>M>>K;
for(int i=0;i<M;i++){
int m,n;
cin>>m>>n;
Edge e;
e.u=m,e.v=n;
E.push_back(e);
}
for(int k=0;k<K;k++){
for(int i=1;i<=N;i++)
tribe[i]=i;
int lost,gnum=0;
cin>>lost;
for(int i=0;i<M;i++){
if(E[i].u!=lost&&E[i].v!=lost)
merge_tribe(E[i].u,E[i].v);
}
for(int i=1;i<=N;i++)
if (tribe[i]==i)
gnum++;
if (gnum>2)
cout<<gnum-2;
else
cout<<0;
}
}
爲什麼還是超時?已經想不出來改進的方法了。
後來發現上面的3個版本只要輸入輸出都換用scanf,prinf就能通過。
scanf輸入和cin輸入的時間差別: https://blog.csdn.net/soul_97/article/details/79416985
通過代碼:
#include <iostream>
#include<vector>
#include<stdio.h>
using namespace std;
struct Edge{
int u,v;
};
vector<Edge> E;
int tribe[1000];
int find_tribe(int r){
int p=r;
while(tribe[p]!=p)
p=tribe[p];
while(tribe[r]!=p){
r=tribe[r];
tribe[r]=p;
}
return p;
}
void merge_tribe(int u,int v){
int fx=find_tribe(u);
int fy=find_tribe(v);
tribe[fx]=fy;
}
int main()
{
int N,M,K;
scanf("%d%d%d",&N,&M,&K);
for(int i=0;i<M;i++){
int m,n;
scanf("%d%d",&m,&n);
Edge e;
e.u=m,e.v=n;
E.push_back(e);
}
for(int k=0;k<K;k++){
for(int i=1;i<=N;i++)
tribe[i]=i;
int lost,gnum=0;
scanf("%d",&lost);
for(int i=0;i<M;i++){
if(E[i].u!=lost&&E[i].v!=lost)
merge_tribe(E[i].u,E[i].v);
}
for(int i=1;i<=N;i++)
if (tribe[i]==i)
gnum++;
if (gnum>2)
printf("%d\n",gnum-2);
else
printf("0\n");
}
}