#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifndef WAIMAI
#include "stations.h"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 1005;
vector<int> adj[SIZE];
int id[SIZE];
vector<int> label(int n, int k, vector<int> u, vector<int> v) {
vector<int> labels(n, -1);
FOR (i, 0, n - 1) {
adj[i].clear();
id[i] = -1;
}
FOR (i, 0, n - 2) {
adj[u[i]].pb(v[i]);
adj[v[i]].pb(u[i]);
}
int dfsid = 0;
auto dfs = [&](auto dfs, int pos, int fa, bool f)->void {
if (f) id[pos] = dfsid++;
for (int np : adj[pos]) if (np != fa) {
dfs(dfs, np, pos, !f);
}
if (!f) id[pos] = dfsid++;
};
dfs(dfs, 0, 0, 1);
FOR (i, 0, n - 1) labels[i] = id[i];
return labels;
}
int find_next_station(int s, int t, vector<int> c) {
sort(c.begin(), c.end());
if (s == 0) {
for (int x : c) if (t <= x) return x;
}
if (s < c[0]) {
int l = s;
for (int i = 0; i + 1 < c.size(); i++) {
if (l <= t && t <= c[i]) return c[i];
l = c[i] + 1;
}
return c.back();
}
int r = s;
for (int i = c.size() - 1; i >= 1; i--) {
if (c[i] <= t && t <= r) return c[i];
r = c[i] - 1;
}
return c[0];
}
/*
in1
1
5 10000000
0 1
1 2
1 3
2 4
2
2 0 1
1 3 3
out1
9
1
3
case1
1
5 1000000
4 2
2 3
3 0
0 1
2
2 1 3
1 4 0
case2
1
7 100000
0 1
0 2
1 3
1 4
2 5
2 6
3
4 5 1
6 0 2
1 4 4
case3
1
6 1000000
1 4
4 3
3 5
4 2
2 0
5
1 4 4
4 5 3
0 2 2
2 0 0
3 2 4
*/
#ifdef WAIMAI
static int max_label = 0;
static int r, n, k, q;
static vector<int> u, v, labels, answers;
static map<int, int> reverse_labels;
static vector<vector<int>> adjlist;
static int s, t, w;
static vector<int> c;
int main() {
assert(scanf("%d", &r) == 1);
for (int tc = 0; tc < r; tc++) {
assert(scanf("%d%d", &n, &k) == 2);
u.resize(n - 1);
v.resize(n - 1);
adjlist.clear();
adjlist.resize(n);
for (int i = 0; i < n - 1; i++) {
assert(scanf("%d%d", &u[i], &v[i]) == 2);
adjlist[u[i]].push_back(v[i]);
adjlist[v[i]].push_back(u[i]);
}
labels = label(n, k, u, v);
if ((int)labels.size() != n) {
printf("Number of labels not equal to %d\n", n);
exit(0);
}
reverse_labels.clear();
for (int i = 0; i < n; i++) {
if (labels[i] < 0 || labels[i] > k) {
printf("Label not in range 0 to %d\n", k);
exit(0);
}
if (reverse_labels.find(labels[i]) != reverse_labels.end()) {
printf("Labels not unique\n");
exit(0);
}
reverse_labels[labels[i]] = i;
if (labels[i] > max_label) {
max_label = labels[i];
}
}
assert(scanf("%d", &q) == 1);
for (int i = 0; i < q; i++) {
assert(scanf("%d%d%d", &s, &t, &w) == 3);
c.clear();
for (int v : adjlist[s]) {
c.push_back(labels[v]);
}
sort(c.begin(), c.end());
int answer = find_next_station(labels[s], labels[t], c);
if (!binary_search(c.begin(), c.end(), answer)) {
printf("Label %d returned by find_next_station not found in c\n", answer);
exit(0);
}
answers.push_back(reverse_labels[answer]);
}
}
printf("%d\n", max_label);
for (int index : answers) {
printf("%d\n", index);
}
exit(0);
}
#endif
Compilation message
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:60:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i + 1 < c.size(); i++) {
| ~~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
472 ms |
528 KB |
Output is correct |
2 |
Correct |
324 ms |
532 KB |
Output is correct |
3 |
Correct |
639 ms |
544 KB |
Output is correct |
4 |
Correct |
514 ms |
532 KB |
Output is correct |
5 |
Correct |
442 ms |
536 KB |
Output is correct |
6 |
Correct |
356 ms |
608 KB |
Output is correct |
7 |
Correct |
283 ms |
532 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
4 ms |
556 KB |
Output is correct |
10 |
Correct |
0 ms |
488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
402 ms |
596 KB |
Output is correct |
2 |
Correct |
525 ms |
532 KB |
Output is correct |
3 |
Correct |
707 ms |
524 KB |
Output is correct |
4 |
Correct |
476 ms |
536 KB |
Output is correct |
5 |
Correct |
522 ms |
416 KB |
Output is correct |
6 |
Correct |
349 ms |
536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
419 ms |
536 KB |
Output is correct |
2 |
Correct |
275 ms |
604 KB |
Output is correct |
3 |
Correct |
802 ms |
416 KB |
Output is correct |
4 |
Correct |
575 ms |
416 KB |
Output is correct |
5 |
Correct |
498 ms |
532 KB |
Output is correct |
6 |
Correct |
298 ms |
532 KB |
Output is correct |
7 |
Correct |
408 ms |
528 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
0 ms |
492 KB |
Output is correct |
11 |
Correct |
523 ms |
536 KB |
Output is correct |
12 |
Correct |
437 ms |
704 KB |
Output is correct |
13 |
Correct |
363 ms |
656 KB |
Output is correct |
14 |
Correct |
336 ms |
524 KB |
Output is correct |
15 |
Correct |
38 ms |
544 KB |
Output is correct |
16 |
Correct |
57 ms |
576 KB |
Output is correct |
17 |
Correct |
90 ms |
664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
700 ms |
512 KB |
Output is correct |
2 |
Correct |
552 ms |
528 KB |
Output is correct |
3 |
Correct |
427 ms |
536 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
4 ms |
420 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
570 ms |
532 KB |
Output is correct |
8 |
Correct |
571 ms |
536 KB |
Output is correct |
9 |
Correct |
482 ms |
416 KB |
Output is correct |
10 |
Correct |
502 ms |
548 KB |
Output is correct |
11 |
Correct |
3 ms |
488 KB |
Output is correct |
12 |
Correct |
3 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
500 KB |
Output is correct |
15 |
Correct |
0 ms |
492 KB |
Output is correct |
16 |
Correct |
401 ms |
560 KB |
Output is correct |
17 |
Correct |
483 ms |
416 KB |
Output is correct |
18 |
Correct |
287 ms |
416 KB |
Output is correct |
19 |
Correct |
389 ms |
416 KB |
Output is correct |
20 |
Correct |
448 ms |
420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
402 ms |
536 KB |
Output is correct |
2 |
Correct |
360 ms |
612 KB |
Output is correct |
3 |
Correct |
714 ms |
416 KB |
Output is correct |
4 |
Correct |
522 ms |
416 KB |
Output is correct |
5 |
Correct |
545 ms |
420 KB |
Output is correct |
6 |
Correct |
376 ms |
544 KB |
Output is correct |
7 |
Correct |
360 ms |
548 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
3 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
500 KB |
Output is correct |
11 |
Correct |
281 ms |
540 KB |
Output is correct |
12 |
Correct |
439 ms |
544 KB |
Output is correct |
13 |
Correct |
631 ms |
536 KB |
Output is correct |
14 |
Correct |
620 ms |
420 KB |
Output is correct |
15 |
Correct |
363 ms |
532 KB |
Output is correct |
16 |
Correct |
386 ms |
544 KB |
Output is correct |
17 |
Correct |
475 ms |
424 KB |
Output is correct |
18 |
Correct |
385 ms |
548 KB |
Output is correct |
19 |
Correct |
385 ms |
548 KB |
Output is correct |
20 |
Correct |
303 ms |
552 KB |
Output is correct |
21 |
Correct |
35 ms |
592 KB |
Output is correct |
22 |
Correct |
60 ms |
544 KB |
Output is correct |
23 |
Correct |
70 ms |
656 KB |
Output is correct |
24 |
Correct |
3 ms |
492 KB |
Output is correct |
25 |
Correct |
5 ms |
496 KB |
Output is correct |
26 |
Correct |
2 ms |
500 KB |
Output is correct |
27 |
Correct |
3 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
500 KB |
Output is correct |
29 |
Correct |
422 ms |
536 KB |
Output is correct |
30 |
Correct |
397 ms |
532 KB |
Output is correct |
31 |
Correct |
462 ms |
420 KB |
Output is correct |
32 |
Correct |
346 ms |
424 KB |
Output is correct |
33 |
Correct |
339 ms |
480 KB |
Output is correct |
34 |
Correct |
246 ms |
548 KB |
Output is correct |
35 |
Correct |
289 ms |
548 KB |
Output is correct |
36 |
Correct |
309 ms |
768 KB |
Output is correct |
37 |
Correct |
428 ms |
656 KB |
Output is correct |
38 |
Correct |
332 ms |
660 KB |
Output is correct |
39 |
Correct |
356 ms |
664 KB |
Output is correct |
40 |
Correct |
428 ms |
636 KB |
Output is correct |
41 |
Correct |
355 ms |
704 KB |
Output is correct |
42 |
Correct |
43 ms |
724 KB |
Output is correct |
43 |
Correct |
100 ms |
660 KB |
Output is correct |
44 |
Correct |
115 ms |
604 KB |
Output is correct |
45 |
Correct |
144 ms |
628 KB |
Output is correct |
46 |
Correct |
279 ms |
548 KB |
Output is correct |
47 |
Correct |
178 ms |
544 KB |
Output is correct |
48 |
Correct |
54 ms |
756 KB |
Output is correct |
49 |
Correct |
46 ms |
700 KB |
Output is correct |