Submission #1266639

#TimeUsernameProblemLanguageResultExecution timeMemory
1266639SoMotThanhXuanTorrent (COI16_torrent)C++20
100 / 100
209 ms25368 KiB
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i) #define REP(i, a, b) for(int i = (a), _b = (b); i >= _b; --i) #define mp make_pair #define all(v) v.begin(), v.end() #define uni(v) v.erase(unique(all(v)), v.end()) #define Bit(x, i) ((x >> (i)) & 1) #define Mask(i) (1 << (i)) #define Cnt(x) __builtin_popcount(x) #define Cntll(x) __builtin_popcountll(x) #define Ctz(x) __builtin_ctz(x) #define Ctzll(x) __builtin_ctzll(x) #define Clz(x) __builtin_clz(x) #define Clzll(x) __builtin_clzll(x) inline bool maximize(int &u, int v){ return v > u ? u = v, true : false; } inline bool minimize(int &u, int v){ return v < u ? u = v, true : false; } inline bool maximizell(long long &u, long long v){ return v > u ? u = v, true : false; } inline bool minimizell(long long &u, long long v){ return v < u ? u = v, true : false; } const int mod = 1e9 + 7; inline int fastPow(int a, int n){ if(n == 0) return 1; int t = fastPow(a, n >> 1); t = 1ll * t * t % mod; if(n & 1) t = 1ll * t * a % mod; return t; } inline void add(int &u, int v){ u += v; if(u >= mod) u -= mod; } inline void sub(int &u, int v){ u -= v; if(u < 0) u += mod; } const int maxN = 3e5 + 5; const int inf = 1e9; const long long infll = 1e18; int n; vector<int> adj[maxN]; int cen1, cen2, f[maxN]; int p[maxN], d[maxN]; bool cmp(const int &a, const int &b){ return f[a] > f[b]; } void pre(int u){ for(int v : adj[u]){ if(v == p[u]) continue; p[v] = u; d[v] = d[u] + 1; pre(v); } } vector<int> path(int u, int v){ vector<int> node1, node2; while(u != v){ if(d[u] < d[v]){ node2.emplace_back(v); v = p[v]; }else{ node1.emplace_back(u); u = p[u]; } } node1.emplace_back(u); reverse(all(node2)); for(int u : node2)node1.emplace_back(u); return node1; } int ban1, ban2; void dfs1(int u, int p){ for(int v : adj[u]){ if(v == p || v == ban2) continue; dfs1(v, u); } f[u] = 0; sort(all(adj[u]), cmp); int cnt = 1; for(int v : adj[u]){ if(v == p || v == ban2) continue; maximize(f[u], f[v] + cnt); ++cnt; } } void dfs2(int u, int p){ for(int v : adj[u]){ if(v == p || v == ban1) continue; dfs2(v, u); } f[u] = 0; sort(all(adj[u]), cmp); int cnt = 1; for(int v : adj[u]){ if(v == p || v == ban1) continue; maximize(f[u], f[v] + cnt); ++cnt; } } void solve(){ pre(1); vector<int> node = path(cen1, cen2); int l = 0, r = node.size() - 2; int ans = inf; while(l <= r){ int mid = (l + r) >> 1; ban1 = node[mid]; ban2 = node[mid + 1]; dfs1(cen1, 0); dfs2(cen2, 0); // cout << f[cen1] << ' ' << f[cen2] << '\n'; // cout << l << ' ' if(f[cen1] == f[cen2]){ cout << f[cen1]; return; } if(f[cen1] < f[cen2]){ l = mid + 1; minimize(ans, f[cen2]); }else{ r = mid - 1; minimize(ans, f[cen1]); } } cout << ans; } void process(){ cin >> n >> cen1 >> cen2; FOR(i, 1, n - 1){ int u, v; cin >> u >> v; adj[u].emplace_back(v); adj[v].emplace_back(u); } solve(); } #define LOVE "torrent" int main(){ if(fopen(LOVE".inp", "r")){ freopen(LOVE".inp", "r", stdin); freopen(LOVE".out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while(t--) process(); // cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ; return 0; }

Compilation message (stderr)

torrent.cpp: In function 'int main()':
torrent.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |         freopen(LOVE".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |         freopen(LOVE".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...