Submission #563954

#TimeUsernameProblemLanguageResultExecution timeMemory
563954baokhue232005Mousetrap (CEOI17_mousetrap)C++17
100 / 100
764 ms241332 KiB
/* #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimize("unroll-loops") */ // lethal option #include<bits/stdc++.h> using namespace std; #define all(flg) flg.begin(), flg.end() #define pb push_back #define fi first #define se second #define endl "\n" #define eb emplace_back #define ii pair<int, int> #define PI 3.141592653589793238462643383279502884 #define ll long long #define for1(i, ff, gg) for(int i = ff; i <= gg; ++i) #define for2(i, ff, gg) for(int i = ff; i >= gg; --i) const int mod = 998244353; const int maxN = 2000005; int n, a[maxN]; int x, y, z, k; int s, t; bool sacred[maxN]; int godlist[maxN]; int cn = 0; vector<int> road[maxN]; int szi[maxN]; vector<int> burden[maxN]; vector<int> vc[maxN]; bool dfs_sacred(int node, int par){ if(node == t){ sacred[node] = 1; return 1; } for(int cc : vc[node]){ if(dfs_sacred(cc, node)){ sacred[node] = 1; godlist[++cn] = node; return 1; } } return 0; } int dfs_calc(int node, int par){ for(int i = 0; i < (int)vc[node].size(); ++i){ if(vc[node][i] == par){ vc[node].erase(vc[node].begin() + i); break; } } for(int &cc : vc[node]) cc = dfs_calc(cc, node); sort(all(vc[node]), greater<int>()); int res = 0; if(vc[node].size() >= 2) res = vc[node][1]; return res + vc[node].size(); } bool solve(int node, int lim, int turn){ ++turn; if(node > n) return (lim >= 0); int fail = 0; for(int cc : road[node]) if(cc + szi[node] > lim) ++fail; if(turn < fail) return 0; turn -= fail; lim -= fail; return solve(node + 1, lim, turn); } signed main(){ // freopen("mousetrap.inp", "r", stdin); // freopen("mousetrap.out", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> t >> s; if(s == t){ cout << 0 << endl; exit(0); } memset(sacred, 0, sizeof(sacred)); for1(i, 2, n){ cin >> x >> y; vc[x].pb(y); vc[y].pb(x); } // if(n > 10) exit(0); dfs_sacred(s, s); reverse(godlist + 1, godlist + cn + 1); z = 0; vector<ii> dawut; for1(i, 1, n) if(!sacred[i]){ int saved = 0; for(int cc : vc[i]) if(sacred[cc] && cc != t) dawut.pb({i, cc}); } for(ii pr : dawut){ burden[pr.se].pb(dfs_calc(pr.fi, pr.se)); } // if(n > 10) exit(0); n = cn; for1(i, 1, n){ swap(road[i], burden[godlist[i]]); szi[i] = road[i].size(); // cout << szi[i] << endl; // for(int cc : road[i]) cout << cc << " "; // cout << endl; } z = 0; for2(i, n, 1){ z += szi[i]; szi[i] = z; } int le = 0, ri = 1e7; while(le != ri){ int mid = (le + ri) / 2; if(solve(1, mid, 0)) ri = mid; else le = mid + 1; } cout << le << endl; }

Compilation message (stderr)

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:95:13: warning: unused variable 'saved' [-Wunused-variable]
   95 |         int saved = 0;
      |             ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...