#include "highway.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(931621953);
int get_rand(int l, int r){
return uniform_int_distribution<int>(l, r)(rng);
}
int A, B, C, M, n;
ll initial;
int path_len;
int ed;
vector<vector<pair<int, int> > > g;
map<pair<int, int>, int> deco;
vector<char> state;
vector<int> gord;
bool rec(int u, int p, int pared){
assert(state[u] == 0);
state[u] = 1;
int pool = -1;
auto add_cand = [&](int e){
if(pool == -1){
pool = e;
} else {
gord.push_back(pool);
gord.push_back(e);
pool = -1;
}
};
for(auto const&e:g[u]){
if(e.first == p) continue;
if(state[e.first] == 0){
bool did = rec(e.first, u, e.second);
if(!did){
add_cand(e.second);
}
} else if(state[e.first] == 1){
add_cand(e.second);
}
}
state[u] = 2;
if(pool != -1){
gord.push_back(pool);
gord.push_back(pared);
return true;
}
return false;
}
vector<int> line_graph_allign(){
state.assign(n, 0);
gord.clear();
rec(get_rand(0, n-1), -1, -1);
if(M%2){
assert(gord.back() == -1);
gord.pop_back();
gord.push_back(gord.back());
}
return gord;
}
int find_vertex(int M, vector<int> const&U, vector<int> const&V){
vector<int> ord = line_graph_allign();
for(int i=0;i<ord.size();i+=2){
int j = 2*get_rand(0, i/2);
swap(ord[i], ord[i-j]);
swap(ord[i+1], ord[i-j+1]);
}
/*for(int i=0;i<ord.size();i+=2){
cerr << U[ord[i]] << " " << V[i] << " | " << U[i+1] << " " << V[i+1] << "\n";
}*/
int l = -1, r = (M+1)/2-1;
while(l+1 < r){
int m = l + (r-l)/2;
vector<int> v(M, 1);
for(int i=0;i/2<=m;++i) v[ord[i]] = 0;
ll mid = ask(v);
if(mid == path_len*(ll)A){
r = m;
} else {
l = m;
}
}
map<int, int> cnt;
for(auto const&e:{U, V}){
for(auto const&i:{ord[2*r], ord[2*r+1]}){
++cnt[e[i]];
}
}
for(auto const&e:cnt){
if(e.second == 2){
return e.first;
}
}
assert(0);
return -1;
}
int far_search(int u, vector<int> const&U, vector<int> const&V, bool fix_d){
vector<int> ord, vis(n, 0);
vector<int> inv_ord(n);
vector<int> d;
ord.push_back(u);
d.push_back(0);
vis[u] = 1;
for(int i=0;i<(int)ord.size();++i){
int v = ord[i];
inv_ord[v] = i;
for(auto const&f:g[v]){
auto const e = f.first;
if(!vis[e]){
vis[e] = 1;
ord.push_back(e);
d.push_back(d[i]+1);
}
}
}
assert((int)ord.size() == n);
int l = 0, r = n;
if(fix_d){
l = lower_bound(d.begin(), d.end(), path_len) - d.begin();
r = upper_bound(d.begin(), d.end(), path_len) - d.begin();
}
while(l+1 < r){
int m = l + (r-l)/2;
vector<int> v(M, 1);
for(int i=0;i<M;++i){
if(inv_ord[U[i]] < m && inv_ord[V[i]] < m){
v[i] = 0;
}
}
ll dist = ask(v);
if(dist == path_len * (ll) A){
r = m;
} else {
l = m;
}
}
return ord[l];
}
void find_pair(int N, vector<int> U, vector<int> V, int A_, int B_) {
n=N; A=A_; B=B_;
C = B-A;
M = U.size();
for(int i=0;i<M;++i){
deco[make_pair(U[i], V[i])] = i;
deco[make_pair(V[i], U[i])] = i;
}
initial = ask(vector<int>(M, 1));
assert(initial % B == 0);
path_len = initial / B;
//if(path_len <= 2) ask(vector<int>(M, 1));
g.assign(N, vector<pair<int, int> >());
for(int i=0;i<M;++i){
g[U[i]].emplace_back(V[i], i);
g[V[i]].emplace_back(U[i], i);
}
for(auto &e:g) shuffle(e.begin(), e.end(), rng);
int start = find_vertex(M, U, V);
//cerr << "ed " << ed << " : " << U[ed] << " " << V[ed] << "\n";
const int S = far_search(start, U, V, false);
const int T = far_search(S, U, V, true);
answer(S, T);
}
Compilation message
highway.cpp: In function 'int find_vertex(int, const std::vector<int>&, const std::vector<int>&)':
highway.cpp:78:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<ord.size();i+=2){
~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
248 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
420 KB |
Output is correct |
5 |
Correct |
2 ms |
420 KB |
Output is correct |
6 |
Correct |
2 ms |
416 KB |
Output is correct |
7 |
Correct |
2 ms |
328 KB |
Output is correct |
8 |
Correct |
3 ms |
344 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
456 KB |
Output is correct |
11 |
Correct |
2 ms |
248 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
540 KB |
Output is correct |
2 |
Correct |
33 ms |
2632 KB |
Output is correct |
3 |
Correct |
397 ms |
21208 KB |
Output is correct |
4 |
Correct |
385 ms |
21208 KB |
Output is correct |
5 |
Correct |
414 ms |
21208 KB |
Output is correct |
6 |
Correct |
366 ms |
21216 KB |
Output is correct |
7 |
Correct |
367 ms |
21228 KB |
Output is correct |
8 |
Correct |
359 ms |
21216 KB |
Output is correct |
9 |
Correct |
397 ms |
21236 KB |
Output is correct |
10 |
Correct |
369 ms |
21212 KB |
Output is correct |
11 |
Correct |
369 ms |
22580 KB |
Output is correct |
12 |
Correct |
399 ms |
23604 KB |
Output is correct |
13 |
Correct |
389 ms |
23288 KB |
Output is correct |
14 |
Correct |
369 ms |
22916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3288 KB |
Output is correct |
2 |
Correct |
47 ms |
6488 KB |
Output is correct |
3 |
Correct |
65 ms |
8536 KB |
Output is correct |
4 |
Correct |
232 ms |
28416 KB |
Output is correct |
5 |
Correct |
211 ms |
28400 KB |
Output is correct |
6 |
Correct |
207 ms |
28064 KB |
Output is correct |
7 |
Correct |
218 ms |
28328 KB |
Output is correct |
8 |
Correct |
225 ms |
28076 KB |
Output is correct |
9 |
Correct |
367 ms |
28324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
536 KB |
Output is correct |
2 |
Correct |
50 ms |
2632 KB |
Output is correct |
3 |
Correct |
303 ms |
16508 KB |
Output is correct |
4 |
Correct |
363 ms |
21228 KB |
Output is correct |
5 |
Correct |
421 ms |
21220 KB |
Output is correct |
6 |
Correct |
358 ms |
21228 KB |
Output is correct |
7 |
Correct |
350 ms |
21216 KB |
Output is correct |
8 |
Correct |
363 ms |
21200 KB |
Output is correct |
9 |
Correct |
348 ms |
21220 KB |
Output is correct |
10 |
Correct |
392 ms |
21216 KB |
Output is correct |
11 |
Correct |
367 ms |
22512 KB |
Output is correct |
12 |
Correct |
364 ms |
22684 KB |
Output is correct |
13 |
Correct |
479 ms |
23628 KB |
Output is correct |
14 |
Correct |
383 ms |
22936 KB |
Output is correct |
15 |
Correct |
352 ms |
21208 KB |
Output is correct |
16 |
Correct |
356 ms |
21212 KB |
Output is correct |
17 |
Correct |
482 ms |
22452 KB |
Output is correct |
18 |
Correct |
512 ms |
22588 KB |
Output is correct |
19 |
Correct |
337 ms |
21216 KB |
Output is correct |
20 |
Correct |
356 ms |
23480 KB |
Output is correct |
21 |
Correct |
357 ms |
21392 KB |
Output is correct |
22 |
Correct |
464 ms |
21400 KB |
Output is correct |
23 |
Correct |
347 ms |
21456 KB |
Output is correct |
24 |
Correct |
335 ms |
22132 KB |
Output is correct |
25 |
Correct |
409 ms |
26580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
2980 KB |
Output is correct |
2 |
Correct |
35 ms |
3244 KB |
Output is correct |
3 |
Correct |
442 ms |
24544 KB |
Output is correct |
4 |
Correct |
481 ms |
27124 KB |
Output is correct |
5 |
Correct |
590 ms |
31732 KB |
Output is correct |
6 |
Correct |
643 ms |
31752 KB |
Output is correct |
7 |
Correct |
601 ms |
31556 KB |
Output is correct |
8 |
Correct |
619 ms |
31736 KB |
Output is correct |
9 |
Correct |
496 ms |
24200 KB |
Output is correct |
10 |
Correct |
490 ms |
24848 KB |
Output is correct |
11 |
Correct |
566 ms |
25344 KB |
Output is correct |
12 |
Correct |
539 ms |
30696 KB |
Output is correct |
13 |
Correct |
662 ms |
31216 KB |
Output is correct |
14 |
Correct |
594 ms |
33084 KB |
Output is correct |
15 |
Correct |
582 ms |
33080 KB |
Output is correct |
16 |
Correct |
528 ms |
28340 KB |
Output is correct |
17 |
Correct |
342 ms |
21468 KB |
Output is correct |
18 |
Correct |
394 ms |
21720 KB |
Output is correct |
19 |
Correct |
412 ms |
21720 KB |
Output is correct |
20 |
Correct |
507 ms |
21788 KB |
Output is correct |
21 |
Correct |
676 ms |
30356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
2984 KB |
Output is correct |
2 |
Correct |
67 ms |
3228 KB |
Output is correct |
3 |
Correct |
456 ms |
24844 KB |
Output is correct |
4 |
Correct |
412 ms |
25860 KB |
Output is correct |
5 |
Correct |
495 ms |
27100 KB |
Output is correct |
6 |
Correct |
561 ms |
31708 KB |
Output is correct |
7 |
Correct |
438 ms |
24844 KB |
Output is correct |
8 |
Correct |
426 ms |
25896 KB |
Output is correct |
9 |
Correct |
485 ms |
27136 KB |
Output is correct |
10 |
Correct |
551 ms |
31732 KB |
Output is correct |
11 |
Correct |
692 ms |
31704 KB |
Output is correct |
12 |
Correct |
625 ms |
31740 KB |
Output is correct |
13 |
Correct |
558 ms |
25328 KB |
Output is correct |
14 |
Correct |
488 ms |
24784 KB |
Output is correct |
15 |
Correct |
577 ms |
25276 KB |
Output is correct |
16 |
Correct |
599 ms |
24708 KB |
Output is correct |
17 |
Correct |
536 ms |
25344 KB |
Output is correct |
18 |
Correct |
530 ms |
24720 KB |
Output is correct |
19 |
Correct |
640 ms |
31412 KB |
Output is correct |
20 |
Correct |
574 ms |
32304 KB |
Output is correct |
21 |
Correct |
603 ms |
33224 KB |
Output is correct |
22 |
Correct |
554 ms |
33216 KB |
Output is correct |
23 |
Correct |
582 ms |
33064 KB |
Output is correct |
24 |
Correct |
594 ms |
33244 KB |
Output is correct |
25 |
Correct |
584 ms |
33304 KB |
Output is correct |
26 |
Correct |
576 ms |
33040 KB |
Output is correct |
27 |
Correct |
381 ms |
21708 KB |
Output is correct |
28 |
Correct |
381 ms |
21648 KB |
Output is correct |
29 |
Partially correct |
390 ms |
21868 KB |
Output is partially correct |
30 |
Correct |
503 ms |
21924 KB |
Output is correct |
31 |
Correct |
402 ms |
21652 KB |
Output is correct |
32 |
Correct |
458 ms |
21584 KB |
Output is correct |
33 |
Correct |
403 ms |
21956 KB |
Output is correct |
34 |
Correct |
513 ms |
21720 KB |
Output is correct |
35 |
Correct |
360 ms |
21604 KB |
Output is correct |
36 |
Correct |
525 ms |
21576 KB |
Output is correct |
37 |
Correct |
399 ms |
21996 KB |
Output is correct |
38 |
Correct |
415 ms |
22292 KB |
Output is correct |
39 |
Correct |
527 ms |
31008 KB |
Output is correct |
40 |
Correct |
549 ms |
30192 KB |
Output is correct |
41 |
Correct |
556 ms |
30136 KB |
Output is correct |
42 |
Correct |
604 ms |
30396 KB |
Output is correct |