Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include <iostream> #include <string> #include <string.h> #include <algorithm> #include <stdio.h> using namespace std; const int Max_N = 100008 ; struct Edge{ int u ; int v ; int w ; } ; Edge edge[Max_N] ; int N , M; bool cmp1(Edge A ,Edge B){ return A.w < B.w ; } bool cmp2(Edge A ,Edge B){ return A.w > B.w ; } int father[Max_N] ; int find_father(int x){ if(x == father[x]) return x ; else return father[x] = find_father(father[x]) ; } int gao(){ int sum = 0 ,brige = 0 ; for(int i = 1 ; i <= N ; i++) father[i] = i ; for(int i = 1 ; i <= M ; i++){ int f_u = find_father(edge[i].u) ; int f_v = find_father(edge[i].v) ; if(f_u != f_v){ brige ++ ; sum += edge[i].w ; father[f_u] = f_v ; } if(brige == N-1) break ; } return brige == N-1 ? sum : -1 ; } int fibo[31] ; void init_fibo(){ fibo[1] = 1 ; fibo[2] = 2 ; for(int i =3 ; i <= 30 ; i++) fibo[i] = fibo[i-1] + fibo[i-2] ; } int judge(){ int L , R ; sort(edge+1 ,edge+1+M, cmp1) ; L = gao() ; sort(edge+1 ,edge+1+M ,cmp2) ; R = gao() ; if(L == -1) return 0 ; for(int i =1 ;i < 30 ;i++){ if(L <= fibo[i] && fibo[i] <= R) return 1 ; } return 0 ; } int main(){ init_fibo() ; int T ; scanf("%d",&T) ; for(int cas =1 ;cas <= T; cas++){ scanf("%d%d",&N,&M) ; for(int i = 1 ;i <= M ;i++) scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w) ; printf("Case #%d: %s\n",cas,judge()? "Yes" : "No") ; } return 0; }