标签:des style blog http io ar color os 使用
2 4 4 1 2 1 2 3 1 3 4 1 1 4 0 5 6 1 2 1 1 3 1 1 4 1 1 5 1 3 5 1 4 2 1
Case #1: Yes Case #2: No
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; int n,m; int fibo[50]; int f[100010]; struct node { int u,v,c; } s[100010]; bool cmp1(node x , node y) { return x.c < y.c; } bool cmp2(node x, node y) { return x.c > y.c; } int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); } void Union(int x ,int y) { int fx = find(x); int fy = find(y); if(fx != fy) { f[fx] = fy; } } int main() { #ifdef xxz freopen("in.txt","r",stdin); #endif fibo[1] = 1; fibo[2] = 2; for(int i = 3; ; i++) { fibo[i] = fibo[i-1] + fibo[i-2]; if(fibo[i] >= 100000) break; } int T,Case = 1;; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++) f[i] = i; for(int i = 0; i < m; i++) { scanf("%d%d%d",&s[i].u,&s[i].v,&s[i].c); Union(s[i].u,s[i].v); } int cent = 0; int bl = 0, bh = 0; int root = 0,size = 0; for(int i = 1; i <= n; i++) { if(f[i] == i) { cent++; root = i; } } printf("Case #%d: ",Case++); if(cent >= 2) cout<<"No"<<endl;//首先要判断是否能构成一个生成树,判断根节点个数是否为1就行 else { sort(s,s+m,cmp1); for(int i = 1; i <= n; i++) f[i] = i; for(int i = 0; i < m; i++) { int fu = find(s[i].u); int fv = find(s[i].v); if(fu == fv) continue; bl += s[i].c; size++; Union(s[i].u,s[i].v); if(size == n-1) break; } size = 0; sort(s,s+m,cmp2); for(int i = 1; i <= n; i++) f[i] = i; for(int i = 0; i < m; i++) { int fu = find(s[i].u); int fv = find(s[i].v); if(fu == fv) continue; bh += s[i].c; size++; Union(s[i].u,s[i].v); if(size == n-1) break; } int flag = 0; for(int i =1; fibo[i] <= 100000 ; i++ ) { if(fibo[i] >= bl && fibo[i] <= bh) { flag = 1; break; } } if(flag) printf("Yes\n"); else printf("No\n"); } } }
标签:des style blog http io ar color os 使用
原文地址:http://blog.csdn.net/u013445530/article/details/41257905