标签:acm for case bottom 实现 信息 put panel iostream
1 /* 2 Time: 2017/8/8 3 */ 4 #include<stdio.h> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 int parent[105]; 9 struct node{ 10 int a,b,v; 11 }casei[999]; 12 13 //初始化parent数组 14 void init() 15 { 16 for(int i = 0; i < 105;i++) 17 parent[i] = i; 18 } 19 20 //找根节点 21 int find(int n) 22 { 23 if(parent[n] == n) 24 return n; 25 else 26 return find(parent[n]); 27 } 28 29 // 30 int merge(int a,int b) 31 { 32 a = find(a); 33 b = find(b); 34 if(a != b) 35 { 36 parent[b] = a; 37 return 1; 38 } 39 else 40 return 0; 41 42 43 } 44 45 // 46 bool cmp(node a,node b) 47 { 48 return a.v < b.v; 49 } 50 51 int main() 52 { 53 int n,m; 54 while(cin>>n>>m&&n!=0) 55 { 56 init(); 57 for(int i = 0; i < n; i++) 58 { 59 cin>>casei[i].a>>casei[i].b>>casei[i].v; 60 61 } 62 sort(casei,casei+n,cmp); 63 int sum = 0; 64 int count = 0; 65 int ans = 1; 66 for(int i = 0; i < n; i++) 67 { 68 int p = merge(casei[i].a,casei[i].b); 69 if(p) 70 { 71 sum += casei[i].v; 72 73 } 74 75 } 76 for(int i = 1; i <= m;i++) 77 { 78 if(parent[i] == i) count++; 79 if(count > 1) 80 { 81 ans = 0; 82 break; 83 } 84 } 85 86 if(ans) cout<<sum<<endl; 87 else cout<<"?"<<endl; 88 } 89 90 return 0; 91 }
标签:acm for case bottom 实现 信息 put panel iostream
原文地址:http://www.cnblogs.com/jj81/p/7305171.html