寻找关键车站 2006-08-11

  天气太热,这两天没写东西了,也没写程序, 无心学习.不过看书相对多了,因为电视也看不下去...

  这是一个前一星期做的题目,想了半天,仍然没什么效果,也暂时做一个将就一下吧.

In the city named ACM, there aren't any network stations by now, what's a pity! So the government plans to establish a complete network, and decides to set up a team in charge of this project.

There are totally N districts. And the team plans to build one network station in each district. Also in order to make sure the communication of the city, there exists at least one path between any two stations, directly or indirectly. For the sack of safety, they hire some engineers working in stations so that if there are any problems they can fix them in no time. Considering the cost, they don't want to hire enough engineers for each station, instead only for the key stations. The key station is the one that if it halts down, the network left isn't connected any more. For example, in the graph given, station 1 is a key station, while the others are not.

So the problem is how to find all of the key stations in the city. The leader of the team, Jack, needs help! As a program expert, can you help him?

Input

The input consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of districts N ≤ 1000. Each of the next at most N lines contains the number of a station followed by the numbers of some stations to which there is a direct connection from this station.

These at most N lines completely describe the network, i.e., each direct connection of two stations in the network is contained at least in one row. All numbers in one line are separated by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0.

Output

For each block, first output the number of the key stations m, then m ascending numbers are followed. The numbers are separated by exactly one blank.

  这题目和另一个我在一个夜晚里想了很久,也没出个结果.转天看了看数据结构教材.这题目图的表达不太可能用邻接矩阵,可能这让我更加坚定用邻接链表.

  道理比较简单,首先排除一个点,然后试着遍历图,如果能中间不间断,则判断其点不为key station.这里我加了记数器,稍微节省点时间.

  问题是因为好象是无向图,所以在存储空间加倍了.在建立的时候每次都是成对建立的.

 #include <stdio.h>
#include <stdlib.h>


struct Anode{
 int num;
 Anode * next;
};
bool b[1000];
Anode * arr[1000];

void addNode(int a,int b){
 Anode * p=(Anode *)malloc(sizeof(Anode));
 Anode * q=(Anode *)malloc(sizeof(Anode));
 Anode * s;
 p->num=b;p->next=NULL;
 q->num=a;q->next=NULL;
 
 if(!arr[a]) arr[a]=p;
 else{
  s=arr[a];
  while(s->next){s=s->next;}
  s->next=p;
 }
 if(!arr[b]) arr[b]=q;
 else{
  s=arr[b];
  while(s->next){s=s->next;}
  s->next=q;
 }
}


//图的遍历
bool DFS(int num,int &sum,int n){
 bool flag=false;
 sum++;
 if(sum==n-1) return true;
 else{
  b[num]=true;
     Anode * p=arr[num];
     while(p){
   if(b[p->num]==false){
    if((flag=DFS(p->num,sum,n))==true) break;
    
   }
   p=p->next;
  }
  return flag;
 }
}
//求关键车站
void findAllKey(int n){
 int sum;
 for(int i=0;i<n;i++){
  sum=0;
  b[i]=true;
  if(DFS((i+1)%n,sum,n)==false)
  { printf("%d/n",i);}
  for(int j=0;j<n;j++){b[j]=false;}
 }

}
int main(){
 addNode(0,1);
 addNode(0,2);
 addNode(1,3);
 addNode(1,4);
 addNode(2,4);
 addNode(4,5);
 addNode(4,6);
 addNode(5,7);
 addNode(6,7);
 findAllKey(8);
 return 0;
}

书上说使用邻接链表可以使此深度优先遍历的时间在O(n+e) e应该为边.故此算法应该为O(n^2)吧?我想应该比普通的邻接矩阵强些.

因为测试起来需要很多数据,而其结果也不是人算的...所以我就用了很小量的数据(同样没有符合输入输出规格).如此就不知道其正确性和时间空间的耗费了.毛病太多了.

这到题目到底要怎么解,你能告诉我吗?

 

 

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值