Submission #146186

#TimeUsernameProblemLanguageResultExecution timeMemory
146186BartolMTorrent (COI16_torrent)C++17
100 / 100
666 ms27124 KiB
#include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <vector> #include <cstring> using namespace std; #define X first #define Y second #define mp make_pair #define pb push_back typedef long long ll; typedef pair <int, int> pii; typedef pair <pii, int> ppi; typedef pair <int, pii> pip; const int INF=0x3f3f3f3f; const int N=3e5+5; int n, A, B; vector <int> ls[N]; vector <int> v; int bio[N], dp[N]; int cmp(int a, int b) { if (dp[a]==dp[b]) return a<b; return dp[a]>dp[b]; } int rek(int node, int par, int ne) { if (dp[node]!=-1) return dp[node]; dp[node]=0; for (int sus:ls[node]) { if (sus==ne || sus==par) continue; rek(sus, node, ne); } sort(ls[node].begin(), ls[node].end(), cmp); int cnt=1; for (int sus:ls[node]) { if (sus==ne || sus==par) continue; dp[node]=max(dp[node], dp[sus]+cnt); cnt++; } return dp[node]; } int dfs(int node) { if (node==A) return 1; if (bio[node]) return 0; bio[node]=1; int res=0; for (int sus:ls[node]) { res|=dfs(sus); } if (res) v.pb(node); return res; } void load() { scanf("%d %d %d", &n, &A, &B); A--; B--; for (int i=0; i<n-1; ++i) { int a, b; scanf("%d %d", &a, &b); a--; b--; ls[a].pb(b); ls[b].pb(a); } } void solve() { dfs(B); v.insert(v.begin(), A); /*printf("v: "); for (int x:v) printf("%d ", x); printf("\n");*/ int lo=0, hi=v.size()-2, mid; while (lo<hi) { memset(dp, -1, sizeof dp); mid=(lo+hi)/2; int x=v[mid], y=v[mid+1]; int fa=rek(A, -1, y), fb=rek(B, -1, x); if (fa>fb) hi=mid; else lo=mid+1; } //printf("lo == %d\n", lo); memset(dp, -1, sizeof dp); int x=v[lo], y=v[lo+1]; int sol=rek(A, -1, y); memset(dp, -1, sizeof dp); if (lo!=0) sol=min(sol, rek(B, -1, v[lo-1])); printf("%d\n", sol); } int main() { load(); solve(); return 0; }

Compilation message (stderr)

torrent.cpp: In function 'void solve()':
torrent.cpp:88:6: warning: unused variable 'x' [-Wunused-variable]
  int x=v[lo], y=v[lo+1];
      ^
torrent.cpp: In function 'void load()':
torrent.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &n, &A, &B); A--; B--;
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &a, &b); a--; b--;
   ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...