이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, K, cent, tot_sz, sz[240005], A[240005], B[240005], inc[240005], down[240005], dep[240005], par[240005], anc[25][240005], wei[25][240005];
bool vis[240005];
char C[240005];
vector<ii> adj[240005], vec[240005];
int get_sizes(int n, int e = -1) {
sz[n] = 1;
for (auto [u, w] : adj[n]) if (u != e && !vis[u])
sz[n] += get_sizes(u, n);
return sz[n];
}
void get_centroid(int n, int e = -1) {
int m = tot_sz - sz[n];
for (auto [u, w] : adj[n]) if (u != e && !vis[u]) {
get_centroid(u, n);
m = max(m, sz[u]);
}
if (m <= tot_sz / 2) cent = n;
}
void dfs(int n, int e = -1, int prv = -1) {
anc[0][n] = e;
wei[0][n] = prv;
for (int k = 1; k < 20; k++)
if (anc[k - 1][n] != -1) {
anc[k][n] = anc[k - 1][anc[k - 1][n]];
wei[k][n] = max(wei[k - 1][n], wei[k - 1][anc[k - 1][n]]);
}
for (auto [u, w] : adj[n]) if (u != e) {
dep[u] = dep[n] + 1;
if (w < prv) inc[u] = inc[n], down[u] = n;
else down[u] = down[n], inc[u] = n;
dfs(u, n, w);
}
}
bool qry(int u, int v, int t) {
int ou = u, ov = v, m_w = 0;
bool has_swap = 0;
if (dep[u] > dep[v]) {
swap(u, v);
has_swap = 1;
}
for (int k = 19; k >= 0; k--)
if (dep[v] - (1 << k) >= dep[u]) {
m_w = max(m_w, wei[k][v]);
v = anc[k][v];
}
if (u == v) {
if (m_w > t) return 0;
if (dep[ou] <= dep[ov]) return dep[down[ov]] <= dep[ou];
else return dep[inc[ou]] <= dep[ov];
}
if (has_swap) swap(u, v);
for (int k = 19; k >= 0; k--)
if (anc[k][u] != anc[k][v]) {
m_w = max({m_w, wei[k][u], wei[k][v]});
u = anc[k][u];
v = anc[k][v];
}
int lca = anc[0][u];
m_w = max({m_w, wei[0][u], wei[0][v]});
return m_w < t && dep[inc[ou]] <= dep[lca] && dep[down[ov]] <= dep[lca] && wei[0][u] < wei[0][v];
}
ii get_ends(int u, int v) {
int ou = u, ov = v;
bool has_swap = 0;
assert(u != v);
if (dep[u] > dep[v]) {
swap(u, v);
has_swap = 1;
}
for (int k = 19; k >= 0; k--)
if (dep[v] - (1 << k) >= dep[u] + 1) v = anc[k][v];
if (u == anc[0][v]) {
auto ret = mp(wei[0][v], wei[0][has_swap ? ou : ov]);
if (has_swap) swap(ret.first, ret.second);
return ret;
}
auto ret = mp(wei[0][ou], wei[0][ov]);
return ret;
}
void decomp(int n, int e = -1) {
get_sizes(n);
tot_sz = sz[n];
cent = -1;
get_centroid(n);
assert(cent != -1);
vis[cent] = 1;
par[cent] = e;
int orig_cent = cent;
for (auto [u, w] : adj[cent]) if (!vis[u])
decomp(u, orig_cent);
}
main() {
memset(anc, -1, sizeof anc);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
for (int i = 1; i <= N + K - 1; i++) {
cin >> C[i];
if (C[i] == 'S' || C[i] == 'Q') cin >> A[i] >> B[i];
else cin >> A[i];
if (C[i] == 'S') {
adj[A[i]].eb(B[i], i);
adj[B[i]].eb(A[i], i);
}
}
inc[1] = down[1] = 1;
dfs(1);
decomp(1);
for (int i = 1; i <= N; i++) {
for (int u = par[i]; u != -1; u = par[u]) {
if (qry(u, i, (int)1e9)) {
auto curr = get_ends(u, i);
vec[u].eb(curr.first, curr.second);
}
}
}
for (int i = 1; i <= N + K - 1; i++) {
if (C[i] == 'Q') {
if (qry(B[i], A[i], i)) cout << "yes\n";
else cout << "no\n";
} else if (C[i] == 'C') {
int ans = 1;
for (auto [x, y] : vec[A[i]])
if (y < i) ans++;
for (int u = par[A[i]]; u != -1; u = par[u]) {
if (qry(A[i], u, i)) {
auto curr = get_ends(A[i], u);
ans++;
for (auto [x, y] : vec[u])
if (curr.second < x && y < i) ans++;
}
}
cout << ans << '\n';
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
servers.cpp:127:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
127 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |