九度题目1325:Battle Over Cities

来源:互联网 发布:网络对大学生利的事件 编辑:程序博客网 时间:2024/05/03 10:56

题目1325:Battle Over Cities
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:331
解决:104
题目描述:
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.

输入:
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.

输出:
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

样例输入:
3 2 3
1 2
1 3
1 2 3样例输出:
1
0
0

 


/*
题意:给你若干个点和这链接这几个点的路,让你求删除一个点之后,让剩下的所有点重新组成通路所需要最少的路径。
思路:采用划分集合的思想,只要是和删除的点有关的(与其有链接的)点都不用管,和删除点无关的只要存在通路就放在一个集合内,
最后看看有几个集合(sum),就需要有几条路(sum-1)。
划分集合可以采用DFS(深入优先搜索)方法,将与一个节点有关的所有节点都划分到集合中去(说白了就是标记掉)
每次DFS之后都记录一次,下次再碰到没有划分到集合的节点(即没有被标记),继续对其DFS,以此推类
*/
AC代码:

#include<stdio.h>#include<string.h>int a[1500][1500],f[1500],n;void DFS(int p){ f[p]=1;//该点当成已经划入集合的点 for(int i=1;i<=n;i++) {        if(f[i]==0&&a[i][p]==1)//找出与p点相接的点i,把i以及i所连接的路径都划入集合   DFS(i); }}int main(){ int i,m,k,p,x,y,sum;    while(scanf("%d %d %d",&n,&m,&k)!=EOF) {   memset(a,0,sizeof(a));         for(i=0;i<m;i++)         {    scanf("%d %d",&x,&y);    a[x][y]=1;    a[y][x]=1;   }   while(k--)   {    scanf("%d",&p);    memset(f,0,sizeof(f));    sum=0;    f[p]=1;//将被敌人占据的点当成一个单独的集合    for(i=1;i<=n;i++)    {     if(f[i]==0)//开始划分集合     {      DFS(i);      sum++;//将i以及i所连接的所有节点归为一个集合     }    }    printf("%d\n",sum-1);//找出有几个单独集合,集合总数减一(sum-1)就是最小链接路径   } } return 0;}
0 0
原创粉丝点击