-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathd.cpp
More file actions
96 lines (91 loc) · 1.81 KB
/
d.cpp
File metadata and controls
96 lines (91 loc) · 1.81 KB
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
94
95
96
#include <bits/stdc++.h>
#define endl '\n'
#define eat cin
#define moo cout
#define int long long
using namespace std;
int T, N, M;
vector<pair<int, int>> G[200005];
pair<int, int> E[200005];
int ans[200005], D[200005];
bool vis[200005];
bool altv[200005];
void dfs(int v, int d){
vis[v] = 1;
D[v] = d;
for(pair<int, int> e : G[v]){
if(vis[e.first]) continue;
ans[e.second] = 1;
dfs(e.first, d+1);
}
}
void sanity(){
int cnt1 = accumulate(ans, ans+M, 0);
assert(cnt1 == N-1);
fill(altv, altv+N, 0);
for(int i = 0; i < M; i++){
if(!ans[i]){
altv[E[i].first] = altv[E[i].second] = 1;
}
}
int cnt0 = accumulate(altv, altv+N, 0);
if(M >= N) assert(cnt0 > M-N+1);
fill(altv, altv+N, 0);
for(int i = 0; i < M; i++){
if(ans[i]){
altv[E[i].first] = altv[E[i].second] = 1;
}
}
int cntx = accumulate(altv, altv+N, 0);
//moo << N << ' ' << cntx << endl; moo.flush();
assert(cntx == N);
}
int32_t main(){
eat.tie(0) -> sync_with_stdio(0);
eat >> T;
while(T--){
eat >> N >> M;
fill(ans, ans+M, 0);
for(int i = 0; i < N; i++){
G[i].clear();
}
for(int i = 0; i < M; i++){
int u, v; eat >> u >> v;
u--, v--;
G[u].push_back({v, i});
G[v].push_back({u, i});
E[i] = {u, v};
}
fill(vis, vis+N, 0);
dfs(0, 0);
if(M == N+2){
fill(altv, altv+N, 0);
int targ = 0;
for(int i = 0; i < M; i++){
if(!ans[i]){
altv[E[i].first] = altv[E[i].second] = 1;
targ = i;
}
}
int cnt = accumulate(altv, altv+N, 0);
if(cnt == 3){
int u = E[targ].first;
int v = E[targ].second;
if(D[u] < D[v]) swap(u, v);
for(pair<int, int> i : G[u]){
int w = i.first;
if(D[w] < D[u] && ans[i.second]){
ans[i.second] = 0;
break;
}
}
ans[targ] = 1;
}
}
sanity();
for(int i = 0; i < M; i++){
moo << ans[i];
}
moo << endl;
}
}