-
Notifications
You must be signed in to change notification settings - Fork 0
/
4786.cpp
94 lines (85 loc) · 1.8 KB
/
4786.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//+base
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SIZE(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
//+kruskal
template<typename T>
int kruskal(vector<int>& ret, vector<T>& es)
{
int N = ret.size();
rep(i, 0, N) ret[i] = i;
function<int(int)> find = [&](int x){
return x == ret[x] ? x : ret[x] = find(ret[x]);
};
sort(all(es));
int cnt = 0;
for(auto& e : es){
int f1 = find(e.u);
int f2 = find(e.v);
if(f1 != f2){
ret[f1] = f2;
cnt += 1;
}
}
return cnt;
}
//+main
bool solve(){
int N, M;
cin >> N >> M;
struct EDGE {
int u, v, c;
bool operator<(const EDGE& t){
return c < t.c;
}
};
vector<EDGE> es;
rep(m, 0, M){
EDGE e;
cin >> e.u >> e.v >> e.c;
e.u -= 1;
e.v -= 1;
es.pb(e);
}
auto cal = [&](int c) {
vector<EDGE> nes;
for(auto& e: es) if(e.c != c) {
nes.pb(e);
}
vector<int> ret(N);
return kruskal(ret, nes);
};
int k = cal(2);
if(k != N-1) return false; // determin spanning tree existance
int mx = cal(0);
int mn = N-1-cal(1);
int a = 0;
int b = 1;
while(true){
int c = a + b;
if(c > mx) break;
if(c >= mn) return true;
a = b;
b = c;
}
return false;
}
int main(){
int T;
cin >> T;
cout.precision(12);
rep(t, 0, T) {
cout << "Case #" << (t+1) << ": " << (solve() ? "Yes" : "No") << endl;
}
return 0;
}