#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using vi = vector<int>;
#define sz(x) int(x.size())
const int mx = 120'000;
struct edge
{
int v;
int t;
};
vector<edge> S[1+mx];
struct query
{
int a;
int t;
};
vector<query> Q[1+mx]; //stored by d
vector<int> C[1+mx]; //stored by d
int N, K;
vi res(2*mx + 5);
vi banned(1+mx, 0);
vi st_count(1+mx, 0);
vi lst;
void dfs(int u, int p)
{
lst.push_back(u);
st_count[u] = 1;
for(auto e: S[u])
{
int v = e.v;
if(v == p) continue;
if(banned[v]) continue;
dfs(v, u);
st_count[u] += st_count[v];
}
}
vi out_good(1+mx); //going out from centroid
vi in_good(1+mx); //coming in to centroid
vi root_time(1+mx);
vi par_time(1+mx);
vi st_index(1+mx);
vi exists(1+mx, 0);
void dfs2(int u, int p)
{
for(auto e: S[u])
{
int v = e.v;
int t = e.t;
if(v == p) continue;
if(banned[v]) continue;
root_time[v] = root_time[u];
par_time[v] = t;
out_good[v] = out_good[u] && (par_time[v] > par_time[u]);
in_good[v] = in_good[u] && (par_time[v] < par_time[u]);
st_index[v] = st_index[u];
dfs2(v, u);
}
}
void solve(int src)
{
lst.clear();
dfs(src, src);
int cent = -1;
for(int u: lst)
{
exists[u] = 1;
if(2 * st_count[u] < st_count[src]) continue;
if(cent == -1) cent = u;
else if(st_count[u] < st_count[cent]) cent = u;
}
// cerr << "\n\n\n\n";
// for(int l: lst) cerr << l << ' ';
// cerr << ": centroid = " << cent << '\n';
// for(int l: lst) cerr << l << " : " << out_good[l] << ' ' << in_good[l] << ' ' << root_time[l] << ' ' << par_time[l] << '\n';
int stid = 0;
st_index[cent] = 0;
for(auto e: S[cent])
{
int v = e.v;
int t = e.t;
if(banned[v]) continue;
st_index[v] = ++stid;
in_good[v] = out_good[v] = 1;
root_time[v] = par_time[v] = t;
dfs2(v, cent);
}
for(int d: lst)
{
for(query qr: Q[d])
{
int a = qr.a;
int t = qr.t;
if(!exists[a]) continue;
if(d == cent)
{
if(a == cent)
res[t] = 1;
else
res[t] |= (out_good[a] && par_time[a] < t);
}
else
{
if(a == cent)
res[t] |= (in_good[d] && root_time[d] < t);
else
res[t] |= (in_good[d] && out_good[a] && root_time[d] < root_time[a] && par_time[a] < t);
}
}
}
for(int u: lst) exists[u] = 0;
banned[cent] = 1;
for(auto e: S[cent])
{
if(banned[e.v]) continue;
solve(e.v);
}
banned[cent] = 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> K;
char c[N+K];
for(int q = 1; q <= N+K-1; q++)
{
cin >> c[q];
if(c[q] == 'S')
{
int a, b;
cin >> a >> b;
S[a].push_back({b, q});
S[b].push_back({a, q});
}
else if(c[q] == 'Q')
{
int a, d;
cin >> a >> d;
Q[d].push_back({a, q});
}
else
{
int d;
cin >> d;
C[d].push_back(q);
}
}
solve(1);
for(int q = 1; q <= N+K-1; q++)
{
if(c[q] == 'S') continue;
else if(c[q] == 'Q')
{
if(res[q] == 1) cout << "yes\n";
else cout << "no\n";
}
else cout << res[q] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
15564 KB |
Output is correct |
2 |
Correct |
44 ms |
17096 KB |
Output is correct |
3 |
Correct |
34 ms |
16964 KB |
Output is correct |
4 |
Correct |
44 ms |
17236 KB |
Output is correct |
5 |
Correct |
38 ms |
17464 KB |
Output is correct |
6 |
Correct |
36 ms |
17124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
15564 KB |
Output is correct |
2 |
Correct |
44 ms |
17096 KB |
Output is correct |
3 |
Correct |
34 ms |
16964 KB |
Output is correct |
4 |
Correct |
44 ms |
17236 KB |
Output is correct |
5 |
Correct |
38 ms |
17464 KB |
Output is correct |
6 |
Correct |
36 ms |
17124 KB |
Output is correct |
7 |
Incorrect |
26 ms |
16340 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
15600 KB |
Output is correct |
2 |
Correct |
126 ms |
24888 KB |
Output is correct |
3 |
Correct |
148 ms |
24912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
15600 KB |
Output is correct |
2 |
Correct |
126 ms |
24888 KB |
Output is correct |
3 |
Correct |
148 ms |
24912 KB |
Output is correct |
4 |
Incorrect |
26 ms |
16380 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
15532 KB |
Output is correct |
2 |
Correct |
304 ms |
31696 KB |
Output is correct |
3 |
Correct |
348 ms |
31728 KB |
Output is correct |
4 |
Correct |
187 ms |
31480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
15532 KB |
Output is correct |
2 |
Correct |
304 ms |
31696 KB |
Output is correct |
3 |
Correct |
348 ms |
31728 KB |
Output is correct |
4 |
Correct |
187 ms |
31480 KB |
Output is correct |
5 |
Incorrect |
26 ms |
16384 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
15524 KB |
Output is correct |
2 |
Correct |
237 ms |
25208 KB |
Output is correct |
3 |
Correct |
265 ms |
25116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
15524 KB |
Output is correct |
2 |
Correct |
237 ms |
25208 KB |
Output is correct |
3 |
Correct |
265 ms |
25116 KB |
Output is correct |
4 |
Incorrect |
27 ms |
16384 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
15516 KB |
Output is correct |
2 |
Correct |
327 ms |
31768 KB |
Output is correct |
3 |
Correct |
338 ms |
31780 KB |
Output is correct |
4 |
Correct |
184 ms |
31480 KB |
Output is correct |
5 |
Correct |
28 ms |
16400 KB |
Output is correct |
6 |
Correct |
194 ms |
25140 KB |
Output is correct |
7 |
Correct |
282 ms |
25172 KB |
Output is correct |
8 |
Correct |
254 ms |
25908 KB |
Output is correct |
9 |
Correct |
302 ms |
25896 KB |
Output is correct |
10 |
Correct |
404 ms |
28856 KB |
Output is correct |
11 |
Correct |
345 ms |
27728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
15516 KB |
Output is correct |
2 |
Correct |
327 ms |
31768 KB |
Output is correct |
3 |
Correct |
338 ms |
31780 KB |
Output is correct |
4 |
Correct |
184 ms |
31480 KB |
Output is correct |
5 |
Correct |
28 ms |
16400 KB |
Output is correct |
6 |
Correct |
194 ms |
25140 KB |
Output is correct |
7 |
Correct |
282 ms |
25172 KB |
Output is correct |
8 |
Correct |
254 ms |
25908 KB |
Output is correct |
9 |
Correct |
302 ms |
25896 KB |
Output is correct |
10 |
Correct |
404 ms |
28856 KB |
Output is correct |
11 |
Correct |
345 ms |
27728 KB |
Output is correct |
12 |
Incorrect |
33 ms |
16324 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
15456 KB |
Output is correct |
2 |
Correct |
44 ms |
17172 KB |
Output is correct |
3 |
Correct |
35 ms |
16964 KB |
Output is correct |
4 |
Correct |
40 ms |
17236 KB |
Output is correct |
5 |
Correct |
41 ms |
17412 KB |
Output is correct |
6 |
Correct |
38 ms |
17136 KB |
Output is correct |
7 |
Correct |
28 ms |
16372 KB |
Output is correct |
8 |
Correct |
124 ms |
24840 KB |
Output is correct |
9 |
Correct |
115 ms |
24828 KB |
Output is correct |
10 |
Correct |
27 ms |
16376 KB |
Output is correct |
11 |
Correct |
326 ms |
31716 KB |
Output is correct |
12 |
Correct |
301 ms |
31708 KB |
Output is correct |
13 |
Correct |
185 ms |
31436 KB |
Output is correct |
14 |
Correct |
30 ms |
16340 KB |
Output is correct |
15 |
Correct |
188 ms |
25164 KB |
Output is correct |
16 |
Correct |
274 ms |
25196 KB |
Output is correct |
17 |
Correct |
267 ms |
25932 KB |
Output is correct |
18 |
Correct |
332 ms |
25768 KB |
Output is correct |
19 |
Correct |
358 ms |
28828 KB |
Output is correct |
20 |
Correct |
327 ms |
27732 KB |
Output is correct |
21 |
Correct |
142 ms |
24964 KB |
Output is correct |
22 |
Correct |
155 ms |
25004 KB |
Output is correct |
23 |
Correct |
237 ms |
25280 KB |
Output is correct |
24 |
Correct |
239 ms |
25444 KB |
Output is correct |
25 |
Correct |
367 ms |
28344 KB |
Output is correct |
26 |
Correct |
277 ms |
24388 KB |
Output is correct |
27 |
Correct |
212 ms |
24232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
15456 KB |
Output is correct |
2 |
Correct |
44 ms |
17172 KB |
Output is correct |
3 |
Correct |
35 ms |
16964 KB |
Output is correct |
4 |
Correct |
40 ms |
17236 KB |
Output is correct |
5 |
Correct |
41 ms |
17412 KB |
Output is correct |
6 |
Correct |
38 ms |
17136 KB |
Output is correct |
7 |
Correct |
28 ms |
16372 KB |
Output is correct |
8 |
Correct |
124 ms |
24840 KB |
Output is correct |
9 |
Correct |
115 ms |
24828 KB |
Output is correct |
10 |
Correct |
27 ms |
16376 KB |
Output is correct |
11 |
Correct |
326 ms |
31716 KB |
Output is correct |
12 |
Correct |
301 ms |
31708 KB |
Output is correct |
13 |
Correct |
185 ms |
31436 KB |
Output is correct |
14 |
Correct |
30 ms |
16340 KB |
Output is correct |
15 |
Correct |
188 ms |
25164 KB |
Output is correct |
16 |
Correct |
274 ms |
25196 KB |
Output is correct |
17 |
Correct |
267 ms |
25932 KB |
Output is correct |
18 |
Correct |
332 ms |
25768 KB |
Output is correct |
19 |
Correct |
358 ms |
28828 KB |
Output is correct |
20 |
Correct |
327 ms |
27732 KB |
Output is correct |
21 |
Correct |
142 ms |
24964 KB |
Output is correct |
22 |
Correct |
155 ms |
25004 KB |
Output is correct |
23 |
Correct |
237 ms |
25280 KB |
Output is correct |
24 |
Correct |
239 ms |
25444 KB |
Output is correct |
25 |
Correct |
367 ms |
28344 KB |
Output is correct |
26 |
Correct |
277 ms |
24388 KB |
Output is correct |
27 |
Correct |
212 ms |
24232 KB |
Output is correct |
28 |
Incorrect |
28 ms |
16332 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |