이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int INF = (int) 1e9 + 1e6 + 123;
const ll LINF = (ll) 1e18 + 1e9 + 123;
#define rep(i, s, t) for (auto i = (s); i < (t); ++(i))
#define per(i, s, t) for (auto i = (s); i >= (t); --(i))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
bool mini(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
bool maxi(auto &x, const auto &y) {
if (y > x) {
x = y;
return 1;
}
return 0;
}
void run();
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
return 0;
}
const int N = (int) 1e6 + 123;
int n, t, m;
vector<int> g[N];
int dp[N], out[N];
bool dfs(int v, int f = -1) {
dp[v] = INF;
out[v] = INF;
if (v == t) {
dp[v] = 0;
return 1;
}
bool has_t = 0;
int pos = -1;
int ptr = 0;
for (int u : g[v]) {
if (u == f) {
pos = ptr;
ptr++;
continue;
}
has_t |= dfs(u, v);
ptr++;
}
if (pos != -1) {
g[v].erase(g[v].begin() + pos);
}
if (has_t) {
vector<int> outs;
int to = -1;
for (int u : g[v]) {
if (dp[u] == INF) {
outs.pb(out[u]);
} else {
to = u;
}
}
assert(to != -1);
if (sz(outs) == 0) {
dp[v] = dp[to];
} else if (sz(outs) == 1) {
dp[v] = dp[to] + 1;
} else {
sort(all(outs));
int k = sz(outs);
dp[v] = dp[to] + outs[k - 2] + k;
}
} else {
vector<int> outs;
for (int u : g[v]) {
outs.pb(out[u]);
}
if (sz(outs) == 0) {
out[v] = 0;
} else if (sz(outs) == 1) {
out[v] = 1;
} else {
sort(all(outs));
int k = sz(outs);
out[v] = outs[k - 2] + k;
}
}
return has_t;
}
void run() {
cin >> n >> t >> m;
rep(i, 0, n - 1) {
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(m);
cout << dp[m] << "\n";
}
# | 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... |