1013. Battle Over Cities (25)

来源:互联网 发布:网易公开课优缺点知乎 编辑:程序博客网 时间:2024/04/29 09:29


时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 Input
3 2 31 21 31 2 3
Sample Output
100
n为顶点数,m为边数,k为查询次数。

m条边。

k次查询。


连通分量问题。

可以用dfs,bfs,并查集。


#include<stdio.h>//dfs 邻接表 #include<vector>#include<string.h>int cou,now;using namespace std;vector<int>adj[1000];int visit[1000];void init(int n){int i;for(i=1;i<=n;i++){visit[i]=0;}}void dfs(int s){if(s==now)return;visit[s]=1;int i;for(i=0;i<adj[s].size();i++){if(visit[adj[s][i]]==0){dfs(adj[s][i]);}}}void dfstravel(int n){int i;for(i=1;i<=n;i++){if(visit[i]==0&&i!=now){dfs(i);cou++;}}}int main(){int n,m,k,i,c1,c2;scanf("%d %d %d",&n,&m,&k);for(i=0;i<m;i++){scanf("%d %d",&c1,&c2);adj[c1].push_back(c2);adj[c2].push_back(c1); }for(i=0;i<k;i++){init(n);scanf("%d",&now);cou=0;dfstravel(n);printf("%d\n",cou-1);}} 


#include<stdio.h>//dfs#include<vector>using namespace std;vector<int>adj[1010];int visit[1010];int cou;void init(int n){int i;cou=0;for(i=1;i<=n;i++){visit[i]=0;}}void dfs(int x){visit[x]=1;int i;for(i=0;i<adj[x].size();i++){if(visit[adj[x][i]]==0){dfs(adj[x][i]);}//dfs(adj[x][i]);}}void dfstravel(int n){int i;for(i=1;i<=n;i++){if(visit[i]==0){cou++;dfs(i);}}}int main(){int n,m,k,i,c1,c2,c;scanf("%d %d %d",&n,&m,&k);for(i=0;i<m;i++){scanf("%d %d",&c1,&c2);adj[c1].push_back(c2);adj[c2].push_back(c1);}for(i=0;i<k;i++){init(n);scanf("%d",&c);visit[c]=1;dfstravel(n);printf("%d\n",cou-1);}}


bfs最后一点超时

#include<stdio.h>//bfs#include<vector>#include<queue>using namespace std;vector<int>adj[1010];int visit[1010];int cou;void init(int n){int i;cou=0;for(i=1;i<=n;i++){visit[i]=0;}}void bfs(int x){queue<int>q;q.push(x);int i;while(!q.empty()){int head=q.front();visit[head]=1;q.pop();for(i=0;i<adj[head].size();i++){if(visit[adj[head][i]]==0){//q.push(adj[head][i]);}}}}void bfstravel(int n){int i;for(i=1;i<=n;i++){if(visit[i]==0){cou++;bfs(i);}}}int main(){int n,m,k,i,c1,c2,c;scanf("%d %d %d",&n,&m,&k);for(i=0;i<m;i++){scanf("%d %d",&c1,&c2);adj[c1].push_back(c2);adj[c2].push_back(c1);}for(i=0;i<k;i++){init(n);scanf("%d",&c);visit[c]=1;bfstravel(n);printf("%d\n",cou-1);}}


好吧,最后一点超时是因为我写错了。。。

#include<stdio.h>//bfs(2)修改 #include<vector>#include<queue>using namespace std;vector<int>adj[1010];int visit[1010];int cou;void init(int n){int i;cou=0;for(i=1;i<=n;i++){visit[i]=0;}}void bfs(int x){visit[x]=1;queue<int>q;q.push(x);int i;while(!q.empty()){int head=q.front();q.pop();for(i=0;i<adj[head].size();i++){if(visit[adj[head][i]]==0){q.push(adj[head][i]);visit[adj[head][i]]=1;//!!!!}}}}void bfstravel(int n){int i;for(i=1;i<=n;i++){if(visit[i]==0){cou++;bfs(i);}}}int main(){int n,m,k,i,c1,c2,c;scanf("%d %d %d",&n,&m,&k);for(i=0;i<m;i++){scanf("%d %d",&c1,&c2);adj[c1].push_back(c2);adj[c2].push_back(c1);}for(i=0;i<k;i++){init(n);scanf("%d",&c);visit[c]=1;bfstravel(n);printf("%d\n",cou-1);}}




#include<stdio.h>//并查集 #include<vector>using namespace std;int tree[1010];struct node{int c1;int c2;}adj[500000];int cou;void init(int n){int i;cou=0;for(i=1;i<=n;i++){tree[i]=-1;}}int findroot(int x){if(tree[x]==-1)return x;return tree[x]=findroot(tree[x]);} int main(){int n,m,k,i,c1,c2,c,j;scanf("%d %d %d",&n,&m,&k);for(i=0;i<m;i++){scanf("%d %d",&c1,&c2);adj[i].c1=c1;adj[i].c2=c2;}for(i=0;i<k;i++){init(n);scanf("%d",&c);for(j=0;j<m;j++){if(adj[j].c1!=c&&adj[j].c2!=c){int a=findroot(adj[j].c1);int b=findroot(adj[j].c2);if(a!=b){tree[a]=b;}}}for(j=1;j<=n;j++){if(j!=c&&tree[j]==-1){cou++;}}printf("%d\n",cou-1);}}


0 0
原创粉丝点击