#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
#define all(v) v.begin(),v.end()
#define sort(v) sort(all(v))
#define endl '\n'
#define forn(i,n) for(int i = 0; i < n; ++i)
#define forbe(i,b,e) for(int i = b; i < e; ++i)
#define forr(i,n) for(int i = n-1; i >= 0; --i)
#define sz(v) ((int)v.size())
#define pb push_back
#define f first
#define s second
const int N = 1e3;
vi adj[N];
bitset<N> bip;
vi ret;
int cnt;
void dfs(int x, int p) {
if (!bip[x])
ret[x] = cnt++;
for(int u : adj[x]) {
if (u == p) continue;
bip[u] = !bip[x];
dfs(u, x);
}
if (bip[x])
ret[x] = cnt++;
}
vi label(int n, int k, vi u, vi v) {
cnt = 0;
forn(i ,n) adj[i].clear();
forn(i ,n) bip[i] = 0;
forn(i ,n-1) {
adj[u[i]].pb(v[i]);
adj[v[i]].pb(u[i]);
}
ret.assign(n,0);
dfs(0,0);
return ret;
}
int find_next_station(int s, int t, vi c) {
if (s == t) return s;
if (sz(c) == 1) return c[0];
int mn, mx;
mn = N;
mx = 0;
int p;
for (int u : c)
{
mn = min(u, mn);
mx = max(u, mx);
}
sort(c);
if (mx > s) {
p = mx;
if (t < s) return p;
if (t >= p) return p;
reverse(all(c));
forn(i, sz(c)) {
if (c[i] < t)
return c[i-1];
}
return c[sz(c)-1];
} else {
p = mn;
if (t > s) return p;
if (t <= p) return p;
forn(i, sz(c)) {
if (c[i] > t)
return c[max(i-1,0)];
}
return c[sz(c)-1];
}
}
#ifdef ONPC
void solve() {
int n,k;
cin >> n >> k;
vi u(n-1), v(n-1);
forn(i,n-1)
cin >> u[i] >> v[i];
vi l = label(n, k, u, v);
forn(i, n)
cout << l[i] << ' ';
cout << endl;
//return;
int q;
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
vi c;
for(int u : adj[a])
c.pb(l[u]);
cout << find_next_station(l[a], l[b], c) << endl;
}
}
int main() {
freopen("in", "r", stdin);
// ios_base::sync_with_stdio(0);cin.tie(0);
solve();
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
453 ms |
648 KB |
Output is correct |
2 |
Correct |
411 ms |
660 KB |
Output is correct |
3 |
Correct |
865 ms |
532 KB |
Output is correct |
4 |
Correct |
677 ms |
524 KB |
Output is correct |
5 |
Correct |
531 ms |
416 KB |
Output is correct |
6 |
Correct |
513 ms |
548 KB |
Output is correct |
7 |
Correct |
441 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
4 ms |
492 KB |
Output is correct |
10 |
Correct |
0 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
427 ms |
548 KB |
Output is correct |
2 |
Correct |
511 ms |
524 KB |
Output is correct |
3 |
Correct |
804 ms |
420 KB |
Output is correct |
4 |
Correct |
626 ms |
420 KB |
Output is correct |
5 |
Correct |
541 ms |
444 KB |
Output is correct |
6 |
Correct |
516 ms |
548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
688 ms |
656 KB |
Output is correct |
2 |
Correct |
416 ms |
648 KB |
Output is correct |
3 |
Correct |
1160 ms |
484 KB |
Output is correct |
4 |
Correct |
676 ms |
420 KB |
Output is correct |
5 |
Correct |
731 ms |
528 KB |
Output is correct |
6 |
Correct |
471 ms |
656 KB |
Output is correct |
7 |
Correct |
525 ms |
536 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
3 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
489 ms |
528 KB |
Output is correct |
12 |
Correct |
548 ms |
764 KB |
Output is correct |
13 |
Correct |
496 ms |
616 KB |
Output is correct |
14 |
Correct |
494 ms |
532 KB |
Output is correct |
15 |
Correct |
56 ms |
416 KB |
Output is correct |
16 |
Correct |
62 ms |
544 KB |
Output is correct |
17 |
Correct |
129 ms |
612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
906 ms |
484 KB |
Output is correct |
2 |
Correct |
619 ms |
524 KB |
Output is correct |
3 |
Correct |
492 ms |
420 KB |
Output is correct |
4 |
Correct |
3 ms |
492 KB |
Output is correct |
5 |
Correct |
7 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
484 KB |
Output is correct |
7 |
Correct |
657 ms |
532 KB |
Output is correct |
8 |
Correct |
954 ms |
532 KB |
Output is correct |
9 |
Correct |
795 ms |
524 KB |
Output is correct |
10 |
Correct |
570 ms |
532 KB |
Output is correct |
11 |
Correct |
5 ms |
492 KB |
Output is correct |
12 |
Correct |
4 ms |
432 KB |
Output is correct |
13 |
Correct |
3 ms |
500 KB |
Output is correct |
14 |
Correct |
3 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
567 ms |
416 KB |
Output is correct |
17 |
Correct |
564 ms |
528 KB |
Output is correct |
18 |
Correct |
378 ms |
536 KB |
Output is correct |
19 |
Correct |
475 ms |
532 KB |
Output is correct |
20 |
Correct |
485 ms |
528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
673 ms |
644 KB |
Output is correct |
2 |
Correct |
439 ms |
516 KB |
Output is correct |
3 |
Correct |
763 ms |
508 KB |
Output is correct |
4 |
Correct |
632 ms |
528 KB |
Output is correct |
5 |
Correct |
649 ms |
416 KB |
Output is correct |
6 |
Correct |
551 ms |
520 KB |
Output is correct |
7 |
Correct |
479 ms |
544 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
5 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
488 KB |
Output is correct |
11 |
Correct |
501 ms |
524 KB |
Output is correct |
12 |
Correct |
584 ms |
508 KB |
Output is correct |
13 |
Correct |
821 ms |
416 KB |
Output is correct |
14 |
Correct |
725 ms |
544 KB |
Output is correct |
15 |
Correct |
610 ms |
552 KB |
Output is correct |
16 |
Correct |
456 ms |
544 KB |
Output is correct |
17 |
Correct |
600 ms |
512 KB |
Output is correct |
18 |
Correct |
371 ms |
544 KB |
Output is correct |
19 |
Correct |
500 ms |
520 KB |
Output is correct |
20 |
Correct |
395 ms |
548 KB |
Output is correct |
21 |
Correct |
54 ms |
616 KB |
Output is correct |
22 |
Correct |
51 ms |
568 KB |
Output is correct |
23 |
Correct |
112 ms |
676 KB |
Output is correct |
24 |
Correct |
6 ms |
496 KB |
Output is correct |
25 |
Correct |
5 ms |
500 KB |
Output is correct |
26 |
Correct |
4 ms |
500 KB |
Output is correct |
27 |
Correct |
2 ms |
500 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
585 ms |
524 KB |
Output is correct |
30 |
Correct |
500 ms |
448 KB |
Output is correct |
31 |
Correct |
572 ms |
528 KB |
Output is correct |
32 |
Correct |
463 ms |
420 KB |
Output is correct |
33 |
Correct |
558 ms |
592 KB |
Output is correct |
34 |
Correct |
278 ms |
632 KB |
Output is correct |
35 |
Correct |
436 ms |
652 KB |
Output is correct |
36 |
Correct |
618 ms |
772 KB |
Output is correct |
37 |
Correct |
553 ms |
544 KB |
Output is correct |
38 |
Correct |
538 ms |
720 KB |
Output is correct |
39 |
Correct |
481 ms |
728 KB |
Output is correct |
40 |
Correct |
389 ms |
640 KB |
Output is correct |
41 |
Correct |
575 ms |
624 KB |
Output is correct |
42 |
Correct |
84 ms |
552 KB |
Output is correct |
43 |
Correct |
109 ms |
544 KB |
Output is correct |
44 |
Correct |
152 ms |
528 KB |
Output is correct |
45 |
Correct |
185 ms |
548 KB |
Output is correct |
46 |
Correct |
282 ms |
528 KB |
Output is correct |
47 |
Correct |
247 ms |
520 KB |
Output is correct |
48 |
Correct |
59 ms |
740 KB |
Output is correct |
49 |
Correct |
62 ms |
780 KB |
Output is correct |