标签:
这道题就是个坑,是个坑。
如果有N个点,那你就for(1 to N){假装没有i,去遍历图} 就可以了。
坑就坑在不看评论能有几个人过去??
题目中说“what was previously a fully connected network”,这句话是搞笑的么,数据中居然有不连通的图。
用空行分开数据,最后一行居然不能有空行。。。什么鬼
1 #include<cstdio> 2 #include<cstring> 3 4 #define MAXN 1010 5 6 struct point{ 7 int ref, nex; 8 }; 9 10 point a[MAXN]; 11 int head[MAXN]; 12 bool vis[MAXN]; 13 14 void runn(int k){ 15 vis[k] = true; 16 int i = head[k]; 17 while(i > 0){ 18 if(!vis[a[i].ref]) runn(a[i].ref); 19 i = a[i].nex; 20 } 21 } 22 23 int main(void) 24 { 25 int x, y; 26 int k; 27 int n, m; 28 int N = 1; 29 bool p, q; 30 q = false; 31 scanf("%d", &x); 32 while(x > 0){ 33 if(q) printf("\n"); 34 else q = true; 35 memset(head, 0, sizeof(head)); 36 printf("Network #%d\n", N++); 37 p = true; 38 k = 1; 39 n = 0; 40 while(x > 0){ 41 scanf("%d", &y); 42 a[k].ref = y; 43 a[k].nex = head[x]; 44 head[x] = k++; 45 a[k].ref = x; 46 a[k].nex = head[y]; 47 head[y] = k++; 48 if(x > n) n = x; 49 if(y > n) n = y; 50 scanf("%d", &x); 51 } //printf("** n = %d", n); 52 for(int i = 0; i <= n; ++i){ //这里出现了0,以及下方出现了if(i == 0) break;是为了判断图是否连通,仅仅是后来加的补丁,所以比较非主流 53 memset(vis, false, sizeof(vis)); 54 vis[i] = true; 55 m = 0; 56 for(int j = 1; j <= n; ++j) 57 if(!vis[j]){ 58 runn(j); 59 m++; 60 } 61 if(m > 1) { 62 if(i == 0) break;//←←←←这里 63 printf(" SPF node %d leaves %d subnets\n", i, m); 64 p = false; 65 } 66 } 67 if(p) printf(" No SPF nodes\n"); 68 //printf("\n"); 69 scanf("%d", &x); 70 } 71 return 0; 72 }
标签:
原文地址:http://www.cnblogs.com/xuezhonghao/p/4869896.html