// Oh damn! Suddenly you're free to fly...
#include<bits/stdc++.h>
#include "simurgh.h"
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 510, maxM = 3e5 + 10, mod = 1e9 + 7, inf = 1e9 + 10;
vector<pii> v[maxn];
set<int> TREE;
int h[maxn];
pii up[maxn];
bool mark[maxn];
int know[maxM];
int parid[maxn], par[maxn];
int ASKED = 0;
int ask(){
// assert(++ASKED <= 10000);//////////////
vector<int> tmp;
for(int x : TREE)
tmp.PB(x);
return count_common_roads(tmp);
}
template<int N> struct DSU{
int pr[N];
DSU(){
memset(pr, -1, sizeof pr);
}
void clear(){
memset(pr, -1, sizeof pr);
}
int fnd(int u){
return pr[u] < 0 ? u : pr[u] = fnd(pr[u]);
}
bool mrg(int a, int b){
if( (a = fnd(a)) == (b = fnd(b)) )
return 0;
if(pr[a] > pr[b])
swap(a, b);
pr[a]+= pr[b];
pr[b] = a;
return 1;
}
void mrg_know(int a, int b){
a = fnd(a), b = fnd(b);
if(know[b] != -1)
know[a] = know[b];
else
know[b] = know[a];
}
void put_know(int a, int x){
a = fnd(a);
know[a] = x;
}
};DSU<maxM> dsu;
void prep(int u, int pr = -1){
par[u] = pr;
mark[u] = 1;
h[u] = (pr == -1 ? 0 : (h[pr] + 1));
up[u] = {h[u], -1};
for(auto [y, c] : v[u]){
if(!mark[y]){
parid[y] = c;
prep(y, u);
TREE.insert(c);
up[u] = min(up[u], up[y]);
}
else if(y != pr){
up[u] = min(up[u], (pii){h[y], c});
}
}
}
int tree_ask;
vector<int> A, B;
int n, m;
void check(int in, int out){
TREE.insert(out);
TREE.erase(in);
int x = ask();
if(x == tree_ask){
dsu.mrg_know(in, out);
dsu.mrg(in, out);
}
if(x < tree_ask){
dsu.put_know(in, 1);
dsu.put_know(out, 0);
}
if(x > tree_ask){
dsu.put_know(in, 0);
dsu.put_know(out, 1);
}
TREE.insert(in);
TREE.erase(out);
}
bool mark_calc[maxn];
void dfs(int u){
if(par[u] != -1 && up[u].F == h[u]){
dsu.put_know(parid[u], 1);
}
mark[u] = 1;
pair<int, pii> mnh = {h[u], {-1, -1}};
for(auto [y, c] : v[u]){
mnh = min(mnh, (pair<int, pii>){h[y], {y, c}});
}
if(mnh.F < h[u] - 1){
int tmp = u;
while(tmp != mnh.S.F && !mark_calc[tmp]){
mark_calc[tmp] = 1;
check(parid[tmp], mnh.S.S);
tmp = par[tmp];
}
if(tmp != mnh.S.F){
check(parid[tmp], mnh.S.S);
}
if(know[ dsu.fnd(parid[u]) ] == -1){
dsu.put_know(parid[u], 0);
}
}
for(auto [y, c] : v[u]){
if(!mark[y])
dfs(y);
}
}
bool bad_edge[maxM];
vector<int> wave;
void dfs2(int u){
mark[u] = 1;
for(auto [y, c] : v[u]){
if(bad_edge[c] || mark[y])
continue;
wave.PB(c);
bad_edge[c] = 1;
dfs2(y);
}
}
DSU<maxn> dsu2;
vector<int> dfs_tree;
int jungle(vector<int> vec){
dsu2.clear();
TREE.clear();
for(int id : vec){
dsu2.mrg(A[id], B[id]);
TREE.insert(id);
}
int lz = 0;
for(int i : dfs_tree){
if(dsu2.mrg(A[i], B[i]))
lz+= know[i], TREE.insert(i);
}
// assert(sz(TREE) == n-1);
return ask() - lz;
}
void solve(vector<int> ed, int cnt = -1){
if(ed.empty())
return;
if(cnt == -1)
cnt = jungle(ed);
if(cnt == 0){
for(int id : ed)
know[id] = 0;
return;
}
if(cnt == sz(ed)){
for(int id : ed)
know[id] = 1;
return;
}
int mid = sz(ed) / 2;
vector<int> v1, v2;
for(int i = 0; i < sz(ed); i++){
if(i < mid)
v1.PB(ed[i]);
else
v2.PB(ed[i]);
}
int c = jungle(v1);
solve(v1, c);
solve(v2, cnt - c);
}
void ASSERT(bool x, int sig){
if(!x)
exit(sig);
}
vector<int> find_roads(int n, vector<int> A, vector<int> B){
::A = A, ::B = B, ::n = n;
m = sz(A);
memset(know, -1, sizeof know);
for(int i = 0; i < m; i++){
v[A[i]].PB({B[i], i});
v[B[i]].PB({A[i], i});
}
prep(0);
tree_ask = ask();
for(int id = 0; id < m; id++){
if(abs(h[A[id]] - h[B[id]]) > 1){
if(h[A[id]] < h[B[id]])
swap(A[id], B[id]);
int tmp = A[id];
int ONE = -1;
while(tmp != B[id]){
if(know[ dsu.fnd(parid[tmp]) ] == -1){
check(parid[tmp], id);
}
else{
ONE = parid[tmp];
}
tmp = par[tmp];
}
bool is = 0;
tmp = A[id];
while(tmp != B[id]){
if(know[ dsu.fnd(parid[tmp]) ] == -1){
is = 1;
}
tmp = par[tmp];
}
if(is){
if(ONE != -1)
check(ONE, id);
tmp = A[id];
if(know[ dsu.fnd(id) ] == -1)
dsu.put_know(id, 0);
}
}
}
for(int i : TREE){
if(know[ dsu.fnd(i) ] == -1)// boreshi
dsu.put_know(i, 1);
}
for(int i : TREE)
dfs_tree.PB(i);
int TOT = 0;
for(int id : TREE){
know[id] = know[ dsu.fnd(id) ];
assert(know[id] != -1);
bad_edge[id] = 1;
TOT+= know[id];
}
// assert(TOT == ask());
// ASSERT(TOT == tree_ask, 1);
int ROUND = 0;
while(true){
memset(mark, 0, sizeof mark);
wave.clear();
for(int i = 0; i < n; i++){
if(!mark[i])
dfs2(i);
}
if(wave.empty())
break;
solve(wave);
// ASSERT(sz(wave) < n, 2);
// ASSERT(++ROUND <= n, 3);
}
vector<int> ret;
for(int i = 0; i < m; i++){
// ASSERT(know[i] != -1, 4);
if(know[i])
ret.PB(i);
}
// ASSERT(sz(ret) == n-1, 5);
return ret;
}
Compilation message
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:288:9: warning: unused variable 'ROUND' [-Wunused-variable]
288 | int ROUND = 0;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
correct |
2 |
Correct |
2 ms |
2688 KB |
correct |
3 |
Correct |
2 ms |
2688 KB |
correct |
4 |
Correct |
2 ms |
2688 KB |
correct |
5 |
Correct |
2 ms |
2688 KB |
correct |
6 |
Correct |
2 ms |
2688 KB |
correct |
7 |
Correct |
2 ms |
2688 KB |
correct |
8 |
Correct |
2 ms |
2688 KB |
correct |
9 |
Correct |
2 ms |
2688 KB |
correct |
10 |
Correct |
2 ms |
2688 KB |
correct |
11 |
Correct |
2 ms |
2688 KB |
correct |
12 |
Correct |
2 ms |
2688 KB |
correct |
13 |
Correct |
2 ms |
2688 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
correct |
2 |
Correct |
2 ms |
2688 KB |
correct |
3 |
Correct |
2 ms |
2688 KB |
correct |
4 |
Correct |
2 ms |
2688 KB |
correct |
5 |
Correct |
2 ms |
2688 KB |
correct |
6 |
Correct |
2 ms |
2688 KB |
correct |
7 |
Correct |
2 ms |
2688 KB |
correct |
8 |
Correct |
2 ms |
2688 KB |
correct |
9 |
Correct |
2 ms |
2688 KB |
correct |
10 |
Correct |
2 ms |
2688 KB |
correct |
11 |
Correct |
2 ms |
2688 KB |
correct |
12 |
Correct |
2 ms |
2688 KB |
correct |
13 |
Correct |
2 ms |
2688 KB |
correct |
14 |
Correct |
5 ms |
2688 KB |
correct |
15 |
Correct |
4 ms |
2720 KB |
correct |
16 |
Correct |
4 ms |
2688 KB |
correct |
17 |
Correct |
5 ms |
2688 KB |
correct |
18 |
Correct |
5 ms |
2688 KB |
correct |
19 |
Correct |
5 ms |
2688 KB |
correct |
20 |
Correct |
4 ms |
2688 KB |
correct |
21 |
Correct |
4 ms |
2688 KB |
correct |
22 |
Correct |
3 ms |
2688 KB |
correct |
23 |
Correct |
3 ms |
2688 KB |
correct |
24 |
Correct |
3 ms |
2688 KB |
correct |
25 |
Correct |
3 ms |
2688 KB |
correct |
26 |
Correct |
3 ms |
2688 KB |
correct |
27 |
Correct |
3 ms |
2688 KB |
correct |
28 |
Correct |
2 ms |
2688 KB |
correct |
29 |
Correct |
3 ms |
2688 KB |
correct |
30 |
Correct |
3 ms |
2688 KB |
correct |
31 |
Correct |
3 ms |
2688 KB |
correct |
32 |
Correct |
3 ms |
2688 KB |
correct |
33 |
Correct |
4 ms |
2688 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
correct |
2 |
Correct |
2 ms |
2688 KB |
correct |
3 |
Correct |
2 ms |
2688 KB |
correct |
4 |
Correct |
2 ms |
2688 KB |
correct |
5 |
Correct |
2 ms |
2688 KB |
correct |
6 |
Correct |
2 ms |
2688 KB |
correct |
7 |
Correct |
2 ms |
2688 KB |
correct |
8 |
Correct |
2 ms |
2688 KB |
correct |
9 |
Correct |
2 ms |
2688 KB |
correct |
10 |
Correct |
2 ms |
2688 KB |
correct |
11 |
Correct |
2 ms |
2688 KB |
correct |
12 |
Correct |
2 ms |
2688 KB |
correct |
13 |
Correct |
2 ms |
2688 KB |
correct |
14 |
Correct |
5 ms |
2688 KB |
correct |
15 |
Correct |
4 ms |
2720 KB |
correct |
16 |
Correct |
4 ms |
2688 KB |
correct |
17 |
Correct |
5 ms |
2688 KB |
correct |
18 |
Correct |
5 ms |
2688 KB |
correct |
19 |
Correct |
5 ms |
2688 KB |
correct |
20 |
Correct |
4 ms |
2688 KB |
correct |
21 |
Correct |
4 ms |
2688 KB |
correct |
22 |
Correct |
3 ms |
2688 KB |
correct |
23 |
Correct |
3 ms |
2688 KB |
correct |
24 |
Correct |
3 ms |
2688 KB |
correct |
25 |
Correct |
3 ms |
2688 KB |
correct |
26 |
Correct |
3 ms |
2688 KB |
correct |
27 |
Correct |
3 ms |
2688 KB |
correct |
28 |
Correct |
2 ms |
2688 KB |
correct |
29 |
Correct |
3 ms |
2688 KB |
correct |
30 |
Correct |
3 ms |
2688 KB |
correct |
31 |
Correct |
3 ms |
2688 KB |
correct |
32 |
Correct |
3 ms |
2688 KB |
correct |
33 |
Correct |
4 ms |
2688 KB |
correct |
34 |
Correct |
126 ms |
3996 KB |
correct |
35 |
Correct |
118 ms |
3960 KB |
correct |
36 |
Correct |
96 ms |
3712 KB |
correct |
37 |
Correct |
24 ms |
2816 KB |
correct |
38 |
Correct |
123 ms |
4088 KB |
correct |
39 |
Correct |
109 ms |
3960 KB |
correct |
40 |
Correct |
93 ms |
3712 KB |
correct |
41 |
Correct |
100 ms |
3968 KB |
correct |
42 |
Correct |
102 ms |
4088 KB |
correct |
43 |
Correct |
35 ms |
3456 KB |
correct |
44 |
Correct |
30 ms |
3212 KB |
correct |
45 |
Correct |
33 ms |
3328 KB |
correct |
46 |
Correct |
27 ms |
3256 KB |
correct |
47 |
Correct |
18 ms |
2944 KB |
correct |
48 |
Correct |
8 ms |
2688 KB |
correct |
49 |
Correct |
14 ms |
2816 KB |
correct |
50 |
Correct |
18 ms |
2944 KB |
correct |
51 |
Correct |
34 ms |
3456 KB |
correct |
52 |
Correct |
28 ms |
3200 KB |
correct |
53 |
Correct |
28 ms |
3200 KB |
correct |
54 |
Correct |
36 ms |
3456 KB |
correct |
55 |
Correct |
35 ms |
3328 KB |
correct |
56 |
Correct |
36 ms |
3328 KB |
correct |
57 |
Correct |
36 ms |
3328 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
correct |
2 |
Correct |
2 ms |
2688 KB |
correct |
3 |
Correct |
446 ms |
6520 KB |
correct |
4 |
Correct |
777 ms |
8080 KB |
correct |
5 |
Correct |
775 ms |
7928 KB |
correct |
6 |
Correct |
663 ms |
7928 KB |
correct |
7 |
Correct |
656 ms |
7928 KB |
correct |
8 |
Correct |
654 ms |
7932 KB |
correct |
9 |
Correct |
790 ms |
8056 KB |
correct |
10 |
Correct |
767 ms |
8056 KB |
correct |
11 |
Correct |
778 ms |
8188 KB |
correct |
12 |
Correct |
773 ms |
8056 KB |
correct |
13 |
Correct |
2 ms |
2688 KB |
correct |
14 |
Correct |
762 ms |
8052 KB |
correct |
15 |
Correct |
769 ms |
8056 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
correct |
2 |
Correct |
2 ms |
2688 KB |
correct |
3 |
Correct |
2 ms |
2688 KB |
correct |
4 |
Correct |
2 ms |
2688 KB |
correct |
5 |
Correct |
2 ms |
2688 KB |
correct |
6 |
Correct |
2 ms |
2688 KB |
correct |
7 |
Correct |
2 ms |
2688 KB |
correct |
8 |
Correct |
2 ms |
2688 KB |
correct |
9 |
Correct |
2 ms |
2688 KB |
correct |
10 |
Correct |
2 ms |
2688 KB |
correct |
11 |
Correct |
2 ms |
2688 KB |
correct |
12 |
Correct |
2 ms |
2688 KB |
correct |
13 |
Correct |
2 ms |
2688 KB |
correct |
14 |
Correct |
5 ms |
2688 KB |
correct |
15 |
Correct |
4 ms |
2720 KB |
correct |
16 |
Correct |
4 ms |
2688 KB |
correct |
17 |
Correct |
5 ms |
2688 KB |
correct |
18 |
Correct |
5 ms |
2688 KB |
correct |
19 |
Correct |
5 ms |
2688 KB |
correct |
20 |
Correct |
4 ms |
2688 KB |
correct |
21 |
Correct |
4 ms |
2688 KB |
correct |
22 |
Correct |
3 ms |
2688 KB |
correct |
23 |
Correct |
3 ms |
2688 KB |
correct |
24 |
Correct |
3 ms |
2688 KB |
correct |
25 |
Correct |
3 ms |
2688 KB |
correct |
26 |
Correct |
3 ms |
2688 KB |
correct |
27 |
Correct |
3 ms |
2688 KB |
correct |
28 |
Correct |
2 ms |
2688 KB |
correct |
29 |
Correct |
3 ms |
2688 KB |
correct |
30 |
Correct |
3 ms |
2688 KB |
correct |
31 |
Correct |
3 ms |
2688 KB |
correct |
32 |
Correct |
3 ms |
2688 KB |
correct |
33 |
Correct |
4 ms |
2688 KB |
correct |
34 |
Correct |
126 ms |
3996 KB |
correct |
35 |
Correct |
118 ms |
3960 KB |
correct |
36 |
Correct |
96 ms |
3712 KB |
correct |
37 |
Correct |
24 ms |
2816 KB |
correct |
38 |
Correct |
123 ms |
4088 KB |
correct |
39 |
Correct |
109 ms |
3960 KB |
correct |
40 |
Correct |
93 ms |
3712 KB |
correct |
41 |
Correct |
100 ms |
3968 KB |
correct |
42 |
Correct |
102 ms |
4088 KB |
correct |
43 |
Correct |
35 ms |
3456 KB |
correct |
44 |
Correct |
30 ms |
3212 KB |
correct |
45 |
Correct |
33 ms |
3328 KB |
correct |
46 |
Correct |
27 ms |
3256 KB |
correct |
47 |
Correct |
18 ms |
2944 KB |
correct |
48 |
Correct |
8 ms |
2688 KB |
correct |
49 |
Correct |
14 ms |
2816 KB |
correct |
50 |
Correct |
18 ms |
2944 KB |
correct |
51 |
Correct |
34 ms |
3456 KB |
correct |
52 |
Correct |
28 ms |
3200 KB |
correct |
53 |
Correct |
28 ms |
3200 KB |
correct |
54 |
Correct |
36 ms |
3456 KB |
correct |
55 |
Correct |
35 ms |
3328 KB |
correct |
56 |
Correct |
36 ms |
3328 KB |
correct |
57 |
Correct |
36 ms |
3328 KB |
correct |
58 |
Correct |
2 ms |
2688 KB |
correct |
59 |
Correct |
2 ms |
2688 KB |
correct |
60 |
Correct |
446 ms |
6520 KB |
correct |
61 |
Correct |
777 ms |
8080 KB |
correct |
62 |
Correct |
775 ms |
7928 KB |
correct |
63 |
Correct |
663 ms |
7928 KB |
correct |
64 |
Correct |
656 ms |
7928 KB |
correct |
65 |
Correct |
654 ms |
7932 KB |
correct |
66 |
Correct |
790 ms |
8056 KB |
correct |
67 |
Correct |
767 ms |
8056 KB |
correct |
68 |
Correct |
778 ms |
8188 KB |
correct |
69 |
Correct |
773 ms |
8056 KB |
correct |
70 |
Correct |
2 ms |
2688 KB |
correct |
71 |
Correct |
762 ms |
8052 KB |
correct |
72 |
Correct |
769 ms |
8056 KB |
correct |
73 |
Correct |
2 ms |
2688 KB |
correct |
74 |
Correct |
782 ms |
8076 KB |
correct |
75 |
Correct |
753 ms |
8952 KB |
correct |
76 |
Correct |
306 ms |
4992 KB |
correct |
77 |
Correct |
770 ms |
8876 KB |
correct |
78 |
Correct |
776 ms |
8952 KB |
correct |
79 |
Correct |
783 ms |
8864 KB |
correct |
80 |
Correct |
804 ms |
8844 KB |
correct |
81 |
Correct |
542 ms |
8184 KB |
correct |
82 |
Correct |
769 ms |
9056 KB |
correct |
83 |
Correct |
483 ms |
6264 KB |
correct |
84 |
Correct |
240 ms |
6812 KB |
correct |
85 |
Correct |
198 ms |
6648 KB |
correct |
86 |
Correct |
140 ms |
5268 KB |
correct |
87 |
Correct |
101 ms |
4736 KB |
correct |
88 |
Correct |
87 ms |
4096 KB |
correct |
89 |
Correct |
84 ms |
3968 KB |
correct |
90 |
Correct |
74 ms |
3960 KB |
correct |
91 |
Correct |
46 ms |
2944 KB |
correct |
92 |
Correct |
28 ms |
2816 KB |
correct |
93 |
Correct |
197 ms |
6648 KB |
correct |
94 |
Correct |
132 ms |
5120 KB |
correct |
95 |
Correct |
128 ms |
5240 KB |
correct |
96 |
Correct |
80 ms |
3968 KB |
correct |
97 |
Correct |
87 ms |
4096 KB |
correct |
98 |
Correct |
103 ms |
4736 KB |
correct |
99 |
Correct |
86 ms |
4096 KB |
correct |
100 |
Correct |
56 ms |
3072 KB |
correct |
101 |
Correct |
29 ms |
2816 KB |
correct |
102 |
Correct |
225 ms |
5864 KB |
correct |
103 |
Correct |
223 ms |
5880 KB |
correct |
104 |
Correct |
222 ms |
5884 KB |
correct |
105 |
Correct |
225 ms |
5760 KB |
correct |