#ifndef LOCAL
#include "meetings.h"
#endif // LOCAL
#ifdef LOCAL
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <utility>
#include <vector>
namespace {
const int MAX_N = 2000;
const int MAX_CALLS = 100000;
void WrongAnswer(int code) {
printf("Wrong Answer [%d]\n", code);
exit(0);
}
int N, num_calls;
std::vector<int> graph[MAX_N];
std::set<std::pair<int, int>> edges, edges_reported;
int weight[MAX_N];
bool Visit(int p, int e, int rt = -1) {
if (p == e) {
++weight[p];
return true;
}
for (int q : graph[p]) {
if (q != rt) {
if (Visit(q, e, p)) {
++weight[p];
return true;
}
}
}
return false;
}
} // namespace
int Query(int u, int v, int w) {
if (!(0 <= u && u <= N - 1 && 0 <= v && v <= N - 1 && 0 <= w && w <= N - 1 &&
u != v && u != w && v != w)) {
WrongAnswer(1);
}
if (++num_calls > MAX_CALLS) {
WrongAnswer(2);
}
std::fill(weight, weight + N, 0);
Visit(u, v);
Visit(u, w);
Visit(v, w);
for (int x = 0; x < N; ++x) {
if (weight[x] == 3) {
return x;
}
}
printf("Error: the input may be invalid\n");
exit(0);
}
void Bridge(int u, int v) {
if (!(0 <= u && u < v && v <= N - 1)) {
WrongAnswer(3);
}
if (!(edges.count(std::make_pair(u, v)) >= 1)) {
WrongAnswer(4);
}
if (!(edges_reported.count(std::make_pair(u, v)) == 0)) {
WrongAnswer(5);
}
edges_reported.insert(std::make_pair(u, v));
}
void Solve(int N);
int main() {
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
if (scanf("%d", &N) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
for (int i = 0; i < N - 1; ++i) {
int u, v;
if (scanf("%d%d", &u, &v) != 2) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
graph[u].push_back(v);
graph[v].push_back(u);
edges.insert(std::make_pair(u, v));
}
num_calls = 0;
Solve(N);
if (edges_reported.size() != static_cast<size_t>(N - 1)) {
WrongAnswer(6);
}
printf("Accepted: %d\n", num_calls);
return 0;
}
#endif // LOCAL
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 2005;
vector<int> sub[maxn];
int n;
int query(int x, int y, int z) {
if (x == y || z == x) return x;
if (y == z) return y;
return Query(x, y, z);
}
void dfs(int root) {
if (sub[root].empty()) return;
int v = sub[root][rng() % sub[root].size()];
vector<int> path;
vector<int> tmp;
swap(tmp, sub[root]);
path.push_back(root);
path.push_back(v);
for (int u : tmp) {
if (u == v) continue;
int lca = query(root, u, v);
path.push_back(lca);
if (lca != u) {
sub[lca].push_back(u);
}
}
sort(path.begin(), path.end());
path.erase(unique(path.begin(), path.end()), path.end());
sort(path.begin(), path.end(), [&](int x, int y) {
return query(root, x, y) == x;
});
auto answer = [&](int u, int v) {
if (u > v) swap(u, v);
Bridge(u, v);
};
for (int i = 0; i + 1 < path.size(); ++i) {
answer(path[i], path[i + 1]);
}
for (int u : path)
dfs(u);
}
void Solve(int _N) {
n = _N;
for (int i = 1; i < n; ++i)
sub[0].push_back(i);
dfs(0);
}
Compilation message
meetings.cpp: In function 'void dfs(int)':
meetings.cpp:155:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
155 | for (int i = 0; i + 1 < path.size(); ++i) {
| ~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
7 ms |
492 KB |
Output is correct |
28 |
Correct |
7 ms |
492 KB |
Output is correct |
29 |
Correct |
7 ms |
492 KB |
Output is correct |
30 |
Correct |
7 ms |
492 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
9 ms |
492 KB |
Output is correct |
33 |
Correct |
16 ms |
492 KB |
Output is correct |
34 |
Correct |
18 ms |
492 KB |
Output is correct |
35 |
Correct |
17 ms |
492 KB |
Output is correct |
36 |
Correct |
7 ms |
492 KB |
Output is correct |
37 |
Correct |
16 ms |
492 KB |
Output is correct |
38 |
Correct |
14 ms |
492 KB |
Output is correct |
39 |
Correct |
9 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
562 ms |
852 KB |
Output is correct |
2 |
Correct |
677 ms |
748 KB |
Output is correct |
3 |
Correct |
606 ms |
748 KB |
Output is correct |
4 |
Correct |
573 ms |
1004 KB |
Output is correct |
5 |
Correct |
473 ms |
748 KB |
Output is correct |
6 |
Correct |
390 ms |
748 KB |
Output is correct |
7 |
Correct |
755 ms |
748 KB |
Output is correct |
8 |
Correct |
873 ms |
812 KB |
Output is correct |
9 |
Correct |
871 ms |
876 KB |
Output is correct |
10 |
Correct |
824 ms |
876 KB |
Output is correct |
11 |
Correct |
1034 ms |
876 KB |
Output is correct |
12 |
Correct |
533 ms |
876 KB |
Output is correct |
13 |
Correct |
294 ms |
748 KB |
Output is correct |
14 |
Correct |
589 ms |
748 KB |
Output is correct |
15 |
Correct |
601 ms |
876 KB |
Output is correct |
16 |
Correct |
680 ms |
876 KB |
Output is correct |
17 |
Correct |
855 ms |
876 KB |
Output is correct |
18 |
Correct |
447 ms |
748 KB |
Output is correct |
19 |
Correct |
555 ms |
876 KB |
Output is correct |
20 |
Correct |
633 ms |
748 KB |
Output is correct |
21 |
Correct |
724 ms |
876 KB |
Output is correct |
22 |
Correct |
626 ms |
872 KB |
Output is correct |
23 |
Correct |
582 ms |
932 KB |
Output is correct |
24 |
Correct |
606 ms |
876 KB |
Output is correct |
25 |
Correct |
568 ms |
776 KB |
Output is correct |
26 |
Correct |
498 ms |
748 KB |
Output is correct |
27 |
Correct |
537 ms |
748 KB |
Output is correct |
28 |
Correct |
992 ms |
1004 KB |
Output is correct |
29 |
Correct |
844 ms |
828 KB |
Output is correct |
30 |
Correct |
809 ms |
748 KB |
Output is correct |
31 |
Correct |
903 ms |
1088 KB |
Output is correct |
32 |
Correct |
1200 ms |
1004 KB |
Output is correct |
33 |
Correct |
970 ms |
1004 KB |
Output is correct |
34 |
Correct |
6 ms |
492 KB |
Output is correct |
35 |
Correct |
7 ms |
492 KB |
Output is correct |
36 |
Correct |
7 ms |
492 KB |
Output is correct |
37 |
Correct |
6 ms |
492 KB |
Output is correct |
38 |
Correct |
8 ms |
512 KB |
Output is correct |
39 |
Correct |
8 ms |
492 KB |
Output is correct |
40 |
Correct |
14 ms |
492 KB |
Output is correct |
41 |
Correct |
18 ms |
492 KB |
Output is correct |
42 |
Correct |
17 ms |
492 KB |
Output is correct |
43 |
Correct |
8 ms |
492 KB |
Output is correct |
44 |
Correct |
12 ms |
492 KB |
Output is correct |
45 |
Correct |
15 ms |
492 KB |
Output is correct |
46 |
Correct |
8 ms |
492 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
1 ms |
364 KB |
Output is correct |
52 |
Correct |
1 ms |
364 KB |
Output is correct |
53 |
Correct |
1 ms |
364 KB |
Output is correct |
54 |
Correct |
1 ms |
364 KB |
Output is correct |
55 |
Correct |
1 ms |
364 KB |
Output is correct |
56 |
Correct |
1 ms |
364 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
1 ms |
364 KB |
Output is correct |
62 |
Correct |
1 ms |
492 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
0 ms |
364 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
1 ms |
364 KB |
Output is correct |
67 |
Correct |
1 ms |
364 KB |
Output is correct |
68 |
Correct |
1 ms |
364 KB |
Output is correct |
69 |
Correct |
1 ms |
364 KB |
Output is correct |
70 |
Correct |
1 ms |
364 KB |
Output is correct |
71 |
Correct |
0 ms |
364 KB |
Output is correct |
72 |
Correct |
1 ms |
364 KB |
Output is correct |