제출 #1167228

#제출 시각아이디문제언어결과실행 시간메모리
1167228ByeWorldTorrent (COI16_torrent)C++20
31 / 100
2093 ms26444 KiB
#include <bits/stdc++.h> #define int long long #define ll long long #define pb push_back #define fi first #define se second #define lf (id<<1) #define rg ((id<<1)|1) #define md ((l+r)>>1) #define ld long double using namespace std; typedef pair<int,int> pii; typedef pair<char,char> pcc; typedef pair<pii,int> ipii; typedef pair<pii,pii> ipiii; const int MAXN = 3e5+10; const int MAXA = 1e7+10; const int INF = 1e9+10; const int SQRT = 300; const int LOG = 20; const ld EPS = 1e-6; const int MOD = 1e9+7; int sum(int a, int b){ return (a+b)%MOD; } void chsum(int &a, int b){ a = (a+b)%MOD; } void chsub(int &a, int b){ a = (a+MOD-b)%MOD; } int mul(int a, int b){ return (a*b)%MOD; } void chmul(int &a, int b){ a = (a*b)%MOD; } void chmn(int &a, int b){ a = min(a, b); } void chmx(int &a, int b){ a = max(a, b); } int expo(int a, int b){ if(b==0) return 1; int te = expo(a, b/2); te = mul(te,te); return (b%2 ? mul(te,a) : te); } int n, A, B; vector <int> adj[MAXN]; vector <int> path; bool done = 0; void precom(int nw, int par){ path.pb(nw); if(nw==B){ done = 1; return; } for(auto nx : adj[nw]){ if(nx==par) continue; precom(nx, nw); if(done) return; } path.pop_back(); } int dp[MAXN]; bool tag[MAXN]; void dfs(int nw, int par){ vector <int> vec; for(auto nx : adj[nw]){ if(nx==par || tag[nx]) continue; dfs(nx, nw); vec.pb(dp[nx]); } sort(vec.rbegin(), vec.rend()); for(int i=0; i<vec.size(); i++) chmx(dp[nw], vec[i]+i+1); } signed main(){ // ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n>>A>>B; for(int i=1; i<=n-1; i++){ int x, y; cin>>x>>y; adj[x].pb(y); adj[y].pb(x); } precom(A, -1); int ans = INF; for(int i=0; i+1<path.size(); i++){ for(int j=1; j<=n; j++) dp[j] = 0; tag[path[i+1]] = 1; dfs(A,-1); tag[path[i+1]] = 0; tag[path[i]] = 1; dfs(B, -1); tag[path[i]] = 0; chmn(ans, max(dp[A], dp[B])); // cout << path[i] <<' ' << dp[A] << ' ' << dp[B] << " dp\n"; } // for(int i=1; i<=n; i++) // cout << i << ' ' << dp[i] << " dp\n"; cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...