Submission #305348

#TimeUsernameProblemLanguageResultExecution timeMemory
305348HynDufTorrent (COI16_torrent)C++11
31 / 100
2097 ms20344 KiB
#include <bits/stdc++.h> #define task "T" #define all(v) (v).begin(), (v).end() #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define Rep(i, r, l) for (int i = (r); i >= (l); --i) #define DB(X) { cerr << #X << " = " << (X) << '\n'; } #define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; } #define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; } #define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; } #define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';} #define SZ(x) ((int)(x).size()) #define pb push_back #define eb emplace_back #define pf push_front #define F first #define S second #define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a)); #define next ___next #define prev ___prev #define y1 ___y1 #define left ___left #define right ___right #define y0 ___y0 #define div ___div #define j0 ___j0 #define jn ___jn using ll = long long; using ld = long double; using ull = unsigned long long; using namespace std; typedef pair<int, int> ii; typedef pair<ii, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<ll> vl; const int N = 3e5 + 2; int n, A, B, par[N]; vi g[N]; void dfs(int u) { for (int v : g[u]) if (v != par[u]) { par[v] = u; dfs(v); } } int dfs1(int u, int p, int b) { int res = 0; vi mn; for (int v : g[u]) if (v != p && v != b) { int t = dfs1(v, u, b); mn.eb(t); } if (!mn.empty()) sort(all(mn), greater<int> ()); rep(i, 0, SZ(mn) - 1) res = max(res, mn[i] + i + 1); return res; } int check(int ofb, int ofa) { return max(dfs1(B, 0, ofa), dfs1(A, 0, ofb)); } int main() { #ifdef HynDuf freopen(task".in", "r", stdin); //freopen(task".out", "w", stdout); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif cin >> n >> A >> B; rep(i, 2, n) { int u, v; cin >> u >> v; g[u].eb(v); g[v].eb(u); } dfs(A); int u = B; vi path; while (u) { path.eb(u); u = par[u]; } // int l = 0, r = SZ(path) - 3, ansu = 0; // while (l <= r) // { // int m = (l + r) >> 1; // if (check(path[m], path[m + 1]) > check(path[m + 1], path[m + 2])) l = m + 1, ansu = l; // else r = m - 1, ansu = m; // } int ans = 1e9; rep(i, 0, SZ(path) - 2) ans = min(ans, check(path[i], path[i + 1])); cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...