Submission #531521

#TimeUsernameProblemLanguageResultExecution timeMemory
531521zlxFTHTorrent (COI16_torrent)C++11
0 / 100
2071 ms7244 KiB
#include<bits/stdc++.h> #define LL long long #define Rep(i,x,y) for(int i = (x), stOxrj = (y); i <= stOxrj; ++i) #define Irep(i,x,y) for(int i = (x), stOxrj = (y); i >= stOxrj; --i) #define il inline #define let const auto #define ci const int& #define VEC vector<int> #define pb emplace_back #define MP make_pair #define PA pair<int, int> #define fi first #define se second #define IT iterator using namespace std; il int read(){ int res = 0, flag = 1; char c = getchar(); while( c < '0' || c > '9' ){ if( c == '-' ) flag = -1; c = getchar(); } while( c >= '0' && c <= '9' ) res = res * 10 + c - '0', c = getchar(); return res * flag; } const int N = 3e5 + 20; VEC e[N]; int ba, bb, n, s, t; bool same(ci u, ci v){ return u + v == ba + bb && abs(u - v) == abs(ba - bb); } int fa[N], f[N]; int nb[N]; void dfs(int u, int fat){ fa[u] = fat; for(int v : e[u]) if(v != fat && !same(u, v)) dfs(v, u); int res = 0, cnt = 0; for(int v : e[u]) if(v != fat && !same(u, v)) nb[++cnt] = f[v]; sort(nb + 1, nb + cnt + 1, [&](ci A, ci B){ return A > B; }); Rep(i, 1, cnt) res = max(res, i + nb[i]); f[u] = res; } int link[N], ln; PA Solve(int x){ ba = link[x], bb = link[x + 1]; dfs(s, 0), dfs(t, 0); return MP(f[s], f[t]); } signed main(){ freopen("torrent.in", "r", stdin); freopen("torrent.out", "w", stdout); n = read(), s = read(), t = read(); Rep(i, 2, n){ int u = read(), v = read(); e[u].pb(v), e[v].pb(u); } dfs(t, 0); int ss = s; link[++ln] = s; while(ss != t) link[++ln] = ss = fa[ss]; PA cur; int l = 1, r = ln - 1; while(l < r){ int mid = l + r >> 1; cur = Solve(mid); if(cur.fi > cur.se) l = mid + 1; else r = mid; } cur = Solve(l); int ans = max(cur.fi, cur.se); if(l != 1){ cur = Solve(l - 1); ans = min(ans, max(cur.fi, cur.se)); } cout<<ans<<'\n'; return 0; }

Compilation message (stderr)

torrent.cpp: In function 'int main()':
torrent.cpp:61:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   61 |   int mid = l + r >> 1;
      |             ~~^~~
torrent.cpp:46:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |  freopen("torrent.in", "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:47:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |  freopen("torrent.out", "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...