#include <vector>
#include "incursion.h"
#include <cstdio>
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
#define sz(x) ((int)(x).size())
using pii = pair<int, int>;
using tii = tuple<int, int, int>;
const int nmax = 45e3 + 5;
vector<int> g[nmax];
int area[nmax];
int father[nmax];
#define N uhasfuhafs
int N;
void initarea(int node, int f) {
area[node] = 1;
father[node] = f;
for(auto x : g[node]) {
if(x == f) continue;
initarea(x, node);
area[node] += area[x];
}
return;
}
pii findcentr(int node, int f) {
//cerr << node << ' ' << area[node] << '\n';
for(auto x : g[node]) {
if(x == f) continue;
//cerr << '\t' << x << ' ' << area[x] << '\n';
if(area[x] * 2 == N) { return pii{node, x}; }
if(area[x] * 2 > N) { return findcentr(x, node); }
}
return pii{node, node};
}
int make_graph(vector<pii> F) {
for(auto &[a, b] : F) --a, --b;
N = sz(F) + 1;
for(int i = 0; i <= N + 1; i++) g[i].clear();
for(auto [a, b] : F) {
g[a].emplace_back(b);
g[b].emplace_back(a);
}
int cnt = 0;
for(int i = 0; i < N; i++) {
if(sz(g[i]) == 2) cnt++;
}
if(cnt == 1) {
for(int i = 0; i < N; i++)
if(sz(g[i]) == 2) return i;
}
initarea(0, 0);
auto [u_, v_] = findcentr(0, 0);
int root;
if(u_ != v_) {
for(int i = 0; i < sz(g[u_]); i++) {
if(g[u_][i] == v_) {
swap(g[u_][i], g[u_].back());
g[u_].pop_back();
break;
}
}
for(int i = 0; i < sz(g[v_]); i++) {
if(g[v_][i] == u_) {
swap(g[v_][i], g[v_].back());
g[v_].pop_back();
break;
}
}
g[u_].emplace_back(N);
g[v_].emplace_back(N);
g[N].emplace_back(u_);
g[N].emplace_back(v_);
N++;
root = N - 1;
}
else { root = u_; }
return root;
}
vector<int> known;
bool dfs(int node, int f, int where) {
bool znayu = node == where;
for(auto x : g[node]) {
if(x == f) continue;
znayu |= dfs(x, node, where);
}
known[node] = znayu;
return known[node];
}
std::vector<int> mark(std::vector<std::pair<int, int>> F, int safe) {
--safe;
int root = make_graph(F);
known.resize(N);
dfs(root, root, safe);
//cerr << root + 1 << '\n';
known.resize(sz(F) + 1);
return known;
}
int go(int x) {
return visit(x +1);
}
void locate(std::vector<std::pair<int, int>> F, int curr, int t) {
--curr;
int root = make_graph(F);
//for(int i = 0; i< N; i++) {
//cerr << i << ": ";
//for(auto x : g[i]) cerr << x << ' ';
//cerr << '\n';
//}
known.assign(N, -1);
if(N != sz(F) + 1)
known.back() = 1;
initarea(root, root);
while(1) {
if(t == 0) {
int u = father[curr];
if(u == sz(F) + 1) {
curr = g[u][0] ^ g[u][1] ^ curr;
t = go(curr);
}
else {
curr = father[curr];
t = go(curr);
}
}
else {
int a = -1, b = -1;
for(auto x : g[curr]) {
if(x == father[curr]) continue;
if(a == -1) a = x;
else b = x;
}
//cerr << curr << ": " << a << ' ' << b << '\n';
if(a == -1 && b == -1) return;
if(b == -1) {
int h = go(a);
if(h == 1) {
curr = a;
t = 1;
continue;
}
go(curr);
return;
}
else {
if(area[a] < area[b]) swap(a, b);
int h = go(a);
if(h == 1) {
curr = a;
t = 1;
continue;
}
go(curr);
h = go(b);
if(h == 1) {
curr = b;
t = 1;
continue;
}
go(curr);
return;
}
}
}
}
#undef N
Compilation message
interface.cpp: In function 'int main()':
interface.cpp:44:55: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | if(fread(T.data(), sizeof(int), 2 * N - 2, stdin) != 2 * N - 2) exit(0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
interface.cpp:50:33: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
50 | int l = (numbers.size() == N ? N : 0);
| ~~~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2820 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
224 ms |
12944 KB |
Correct |
2 |
Correct |
221 ms |
13080 KB |
Correct |
3 |
Correct |
125 ms |
14116 KB |
Correct |
4 |
Correct |
97 ms |
14700 KB |
Correct |
5 |
Correct |
203 ms |
14372 KB |
Correct |
6 |
Correct |
90 ms |
14608 KB |
Correct |
7 |
Correct |
83 ms |
12968 KB |
Correct |
8 |
Correct |
201 ms |
13296 KB |
Correct |
9 |
Correct |
233 ms |
13724 KB |
Correct |
10 |
Correct |
163 ms |
13136 KB |
Correct |
11 |
Correct |
110 ms |
14940 KB |
Correct |
12 |
Correct |
297 ms |
13344 KB |
Correct |
13 |
Correct |
83 ms |
12836 KB |
Correct |
14 |
Correct |
83 ms |
13368 KB |
Correct |
15 |
Correct |
230 ms |
13904 KB |
Correct |
16 |
Correct |
234 ms |
15164 KB |
Correct |
17 |
Correct |
123 ms |
12188 KB |
Correct |
18 |
Correct |
105 ms |
13072 KB |
Correct |
19 |
Correct |
159 ms |
12156 KB |
Correct |
20 |
Correct |
86 ms |
14392 KB |
Correct |
21 |
Correct |
89 ms |
13304 KB |
Correct |
22 |
Correct |
225 ms |
13372 KB |
Correct |
23 |
Correct |
214 ms |
13484 KB |
Correct |
24 |
Correct |
98 ms |
13196 KB |
Correct |
25 |
Correct |
91 ms |
13980 KB |
Correct |
26 |
Correct |
88 ms |
12612 KB |
Correct |
27 |
Correct |
82 ms |
13176 KB |
Correct |
28 |
Correct |
82 ms |
13688 KB |
Correct |
29 |
Correct |
217 ms |
13612 KB |
Correct |
30 |
Correct |
217 ms |
12800 KB |
Correct |
31 |
Correct |
93 ms |
14472 KB |
Correct |
32 |
Correct |
218 ms |
13460 KB |
Correct |
33 |
Correct |
245 ms |
13788 KB |
Correct |
34 |
Correct |
83 ms |
12704 KB |
Correct |
35 |
Correct |
78 ms |
12412 KB |
Correct |
36 |
Correct |
224 ms |
13732 KB |
Correct |
37 |
Correct |
198 ms |
13592 KB |
Correct |
38 |
Correct |
243 ms |
14500 KB |
Correct |
39 |
Correct |
147 ms |
14636 KB |
Correct |
40 |
Correct |
181 ms |
14148 KB |
Correct |
41 |
Correct |
84 ms |
14096 KB |
Correct |
42 |
Correct |
86 ms |
13700 KB |
Correct |
43 |
Correct |
219 ms |
13608 KB |
Correct |
44 |
Correct |
202 ms |
12240 KB |
Correct |
45 |
Correct |
97 ms |
13056 KB |
Correct |
46 |
Correct |
86 ms |
13124 KB |
Correct |
47 |
Correct |
93 ms |
13848 KB |
Correct |
48 |
Correct |
83 ms |
12412 KB |
Correct |
49 |
Correct |
75 ms |
14208 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
7948 KB |
Correct |
2 |
Correct |
71 ms |
7980 KB |
Correct |
3 |
Correct |
77 ms |
8004 KB |
Correct |
4 |
Correct |
83 ms |
10468 KB |
Correct |
5 |
Correct |
137 ms |
10592 KB |
Correct |
6 |
Correct |
170 ms |
10480 KB |
Correct |
7 |
Correct |
75 ms |
7916 KB |
Correct |
8 |
Correct |
74 ms |
7700 KB |
Correct |
9 |
Correct |
74 ms |
7916 KB |
Correct |
10 |
Correct |
71 ms |
8076 KB |
Correct |
11 |
Correct |
76 ms |
7964 KB |
Correct |
12 |
Correct |
77 ms |
7656 KB |
Correct |
13 |
Correct |
76 ms |
7888 KB |
Correct |
14 |
Correct |
60 ms |
6464 KB |
Correct |
15 |
Correct |
56 ms |
6456 KB |
Correct |
16 |
Correct |
74 ms |
7716 KB |
Correct |
17 |
Correct |
82 ms |
7760 KB |
Correct |
18 |
Correct |
72 ms |
7716 KB |
Correct |
19 |
Correct |
75 ms |
7980 KB |
Correct |
20 |
Correct |
52 ms |
6472 KB |
Correct |
21 |
Correct |
54 ms |
6564 KB |
Correct |
22 |
Correct |
52 ms |
6576 KB |
Correct |
23 |
Correct |
60 ms |
6308 KB |
Correct |
24 |
Correct |
56 ms |
6700 KB |
Correct |
25 |
Correct |
51 ms |
6448 KB |
Correct |
26 |
Correct |
81 ms |
7960 KB |
Correct |
27 |
Correct |
78 ms |
7908 KB |
Correct |
28 |
Correct |
74 ms |
7976 KB |
Correct |
29 |
Correct |
84 ms |
7844 KB |
Correct |
30 |
Correct |
79 ms |
7972 KB |
Correct |
31 |
Correct |
79 ms |
7960 KB |
Correct |
32 |
Correct |
79 ms |
7948 KB |
Correct |
33 |
Correct |
80 ms |
7940 KB |
Correct |
34 |
Correct |
76 ms |
7996 KB |
Correct |
35 |
Correct |
68 ms |
7940 KB |
Correct |
36 |
Correct |
89 ms |
7976 KB |
Correct |
37 |
Correct |
78 ms |
7868 KB |
Correct |
38 |
Correct |
79 ms |
7896 KB |
Correct |
39 |
Correct |
76 ms |
8004 KB |
Correct |
40 |
Correct |
78 ms |
7984 KB |
Correct |
41 |
Correct |
75 ms |
8012 KB |
Correct |
42 |
Correct |
68 ms |
7912 KB |
Correct |
43 |
Correct |
74 ms |
7996 KB |
Correct |
44 |
Correct |
76 ms |
7976 KB |
Correct |
45 |
Correct |
77 ms |
7912 KB |
Correct |
46 |
Correct |
77 ms |
7936 KB |
Correct |
47 |
Correct |
81 ms |
7972 KB |
Correct |
48 |
Correct |
74 ms |
8000 KB |
Correct |
49 |
Correct |
76 ms |
7980 KB |
Correct |
50 |
Correct |
75 ms |
8004 KB |
Correct |
51 |
Correct |
75 ms |
7912 KB |
Correct |
52 |
Correct |
77 ms |
7984 KB |
Correct |
53 |
Correct |
77 ms |
7972 KB |
Correct |
54 |
Correct |
81 ms |
8048 KB |
Correct |
55 |
Correct |
74 ms |
8008 KB |
Correct |
56 |
Correct |
75 ms |
7908 KB |
Correct |
57 |
Correct |
74 ms |
7828 KB |
Correct |
58 |
Correct |
74 ms |
7908 KB |
Correct |
59 |
Correct |
80 ms |
8120 KB |
Correct |
60 |
Correct |
78 ms |
7844 KB |
Correct |
61 |
Correct |
82 ms |
7980 KB |
Correct |
62 |
Correct |
75 ms |
7972 KB |
Correct |
63 |
Correct |
75 ms |
8000 KB |
Correct |
64 |
Correct |
73 ms |
7960 KB |
Correct |
65 |
Correct |
79 ms |
7972 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2820 KB |
Correct |
2 |
Correct |
224 ms |
12944 KB |
Correct |
3 |
Correct |
221 ms |
13080 KB |
Correct |
4 |
Correct |
125 ms |
14116 KB |
Correct |
5 |
Correct |
97 ms |
14700 KB |
Correct |
6 |
Correct |
203 ms |
14372 KB |
Correct |
7 |
Correct |
90 ms |
14608 KB |
Correct |
8 |
Correct |
83 ms |
12968 KB |
Correct |
9 |
Correct |
201 ms |
13296 KB |
Correct |
10 |
Correct |
233 ms |
13724 KB |
Correct |
11 |
Correct |
163 ms |
13136 KB |
Correct |
12 |
Correct |
110 ms |
14940 KB |
Correct |
13 |
Correct |
297 ms |
13344 KB |
Correct |
14 |
Correct |
83 ms |
12836 KB |
Correct |
15 |
Correct |
83 ms |
13368 KB |
Correct |
16 |
Correct |
230 ms |
13904 KB |
Correct |
17 |
Correct |
234 ms |
15164 KB |
Correct |
18 |
Correct |
123 ms |
12188 KB |
Correct |
19 |
Correct |
105 ms |
13072 KB |
Correct |
20 |
Correct |
159 ms |
12156 KB |
Correct |
21 |
Correct |
86 ms |
14392 KB |
Correct |
22 |
Correct |
89 ms |
13304 KB |
Correct |
23 |
Correct |
225 ms |
13372 KB |
Correct |
24 |
Correct |
214 ms |
13484 KB |
Correct |
25 |
Correct |
98 ms |
13196 KB |
Correct |
26 |
Correct |
91 ms |
13980 KB |
Correct |
27 |
Correct |
88 ms |
12612 KB |
Correct |
28 |
Correct |
82 ms |
13176 KB |
Correct |
29 |
Correct |
82 ms |
13688 KB |
Correct |
30 |
Correct |
217 ms |
13612 KB |
Correct |
31 |
Correct |
217 ms |
12800 KB |
Correct |
32 |
Correct |
93 ms |
14472 KB |
Correct |
33 |
Correct |
218 ms |
13460 KB |
Correct |
34 |
Correct |
245 ms |
13788 KB |
Correct |
35 |
Correct |
83 ms |
12704 KB |
Correct |
36 |
Correct |
78 ms |
12412 KB |
Correct |
37 |
Correct |
224 ms |
13732 KB |
Correct |
38 |
Correct |
198 ms |
13592 KB |
Correct |
39 |
Correct |
243 ms |
14500 KB |
Correct |
40 |
Correct |
147 ms |
14636 KB |
Correct |
41 |
Correct |
181 ms |
14148 KB |
Correct |
42 |
Correct |
84 ms |
14096 KB |
Correct |
43 |
Correct |
86 ms |
13700 KB |
Correct |
44 |
Correct |
219 ms |
13608 KB |
Correct |
45 |
Correct |
202 ms |
12240 KB |
Correct |
46 |
Correct |
97 ms |
13056 KB |
Correct |
47 |
Correct |
86 ms |
13124 KB |
Correct |
48 |
Correct |
93 ms |
13848 KB |
Correct |
49 |
Correct |
83 ms |
12412 KB |
Correct |
50 |
Correct |
75 ms |
14208 KB |
Correct |
51 |
Correct |
74 ms |
7948 KB |
Correct |
52 |
Correct |
71 ms |
7980 KB |
Correct |
53 |
Correct |
77 ms |
8004 KB |
Correct |
54 |
Correct |
83 ms |
10468 KB |
Correct |
55 |
Correct |
137 ms |
10592 KB |
Correct |
56 |
Correct |
170 ms |
10480 KB |
Correct |
57 |
Correct |
75 ms |
7916 KB |
Correct |
58 |
Correct |
74 ms |
7700 KB |
Correct |
59 |
Correct |
74 ms |
7916 KB |
Correct |
60 |
Correct |
71 ms |
8076 KB |
Correct |
61 |
Correct |
76 ms |
7964 KB |
Correct |
62 |
Correct |
77 ms |
7656 KB |
Correct |
63 |
Correct |
76 ms |
7888 KB |
Correct |
64 |
Correct |
60 ms |
6464 KB |
Correct |
65 |
Correct |
56 ms |
6456 KB |
Correct |
66 |
Correct |
74 ms |
7716 KB |
Correct |
67 |
Correct |
82 ms |
7760 KB |
Correct |
68 |
Correct |
72 ms |
7716 KB |
Correct |
69 |
Correct |
75 ms |
7980 KB |
Correct |
70 |
Correct |
52 ms |
6472 KB |
Correct |
71 |
Correct |
54 ms |
6564 KB |
Correct |
72 |
Correct |
52 ms |
6576 KB |
Correct |
73 |
Correct |
60 ms |
6308 KB |
Correct |
74 |
Correct |
56 ms |
6700 KB |
Correct |
75 |
Correct |
51 ms |
6448 KB |
Correct |
76 |
Correct |
81 ms |
7960 KB |
Correct |
77 |
Correct |
78 ms |
7908 KB |
Correct |
78 |
Correct |
74 ms |
7976 KB |
Correct |
79 |
Correct |
84 ms |
7844 KB |
Correct |
80 |
Correct |
79 ms |
7972 KB |
Correct |
81 |
Correct |
79 ms |
7960 KB |
Correct |
82 |
Correct |
79 ms |
7948 KB |
Correct |
83 |
Correct |
80 ms |
7940 KB |
Correct |
84 |
Correct |
76 ms |
7996 KB |
Correct |
85 |
Correct |
68 ms |
7940 KB |
Correct |
86 |
Correct |
89 ms |
7976 KB |
Correct |
87 |
Correct |
78 ms |
7868 KB |
Correct |
88 |
Correct |
79 ms |
7896 KB |
Correct |
89 |
Correct |
76 ms |
8004 KB |
Correct |
90 |
Correct |
78 ms |
7984 KB |
Correct |
91 |
Correct |
75 ms |
8012 KB |
Correct |
92 |
Correct |
68 ms |
7912 KB |
Correct |
93 |
Correct |
74 ms |
7996 KB |
Correct |
94 |
Correct |
76 ms |
7976 KB |
Correct |
95 |
Correct |
77 ms |
7912 KB |
Correct |
96 |
Correct |
77 ms |
7936 KB |
Correct |
97 |
Correct |
81 ms |
7972 KB |
Correct |
98 |
Correct |
74 ms |
8000 KB |
Correct |
99 |
Correct |
76 ms |
7980 KB |
Correct |
100 |
Correct |
75 ms |
8004 KB |
Correct |
101 |
Correct |
75 ms |
7912 KB |
Correct |
102 |
Correct |
77 ms |
7984 KB |
Correct |
103 |
Correct |
77 ms |
7972 KB |
Correct |
104 |
Correct |
81 ms |
8048 KB |
Correct |
105 |
Correct |
74 ms |
8008 KB |
Correct |
106 |
Correct |
75 ms |
7908 KB |
Correct |
107 |
Correct |
74 ms |
7828 KB |
Correct |
108 |
Correct |
74 ms |
7908 KB |
Correct |
109 |
Correct |
80 ms |
8120 KB |
Correct |
110 |
Correct |
78 ms |
7844 KB |
Correct |
111 |
Correct |
82 ms |
7980 KB |
Correct |
112 |
Correct |
75 ms |
7972 KB |
Correct |
113 |
Correct |
75 ms |
8000 KB |
Correct |
114 |
Correct |
73 ms |
7960 KB |
Correct |
115 |
Correct |
79 ms |
7972 KB |
Correct |
116 |
Correct |
88 ms |
8496 KB |
Correct |
117 |
Incorrect |
83 ms |
8472 KB |
Not correct |
118 |
Halted |
0 ms |
0 KB |
- |