Submission #855900

#TimeUsernameProblemLanguageResultExecution timeMemory
855900mychecksedadMousetrap (CEOI17_mousetrap)C++17
0 / 100
605 ms75336 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> using namespace std; #define ll long long int #define MOD (1000000000+7) #define MOD1 (998244353) #define pb push_back #define all(x) x.begin(), x.end() #define en cout << '\n' const int N = 1e6+100, M = 1e5+10, K = 22; struct Fenwick{ int n; vector<int> t; Fenwick(int _n){ n = _n; t.resize(n+1, 0); } void add(int v, int x){ while(v <= n){ t[v] += x; v += (v&-v); } } int get(int v){ int res = 0; while(v > 0){ res += t[v]; v -= (v&-v); } return res; } }; int n, s, t, dp[N], pd[N], ch[N]; ll ans = 0; vector<int> g[N], P; bool dfs(int v, int p){ if(v == t) return 1; bool ok = 0; dp[v] = 0; pd[v] = 0; vector<int> dps; int child_t = -1; for(int u: g[v]){ if(u != p){ bool b = dfs(u, v); if(!b) dps.pb(dp[u] + 1); if(b) child_t = u; ok |= b; } } ch[v] = child_t; sort(all(dps), greater<int>()); if(ok){ pd[v] = pd[child_t] + int(dps.size()); }else{ dp[v] = dps.size() <= 1 ? int(dps.size()) : dps[1] + (dps.size() - 1); } // cout << dp[v] << ' ' << v << ' ' << pd[v] << '\n'; if(ok) P.pb(v); return ok; } void solve(){ cin >> n >> t >> s; for(int i = 0; i < n - 1; ++i){ int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } dp[t] = pd[t] = 0; dfs(s, 0); int ans = MOD; vector<pair<int, int>> A; int sz = P.size(); Fenwick fw(sz); reverse(all(P)); for(int i = 0; i < sz; ++i){ int v = P[i]; fw.add(i + 1, 1); for(int u: g[v]){ if(ch[u] != v && u != ch[v]){ A.pb({pd[v] + dp[u], i}); // cout << u << ' ' << v << '\n'; } } // cout << P[i] << endl; } // en; sort(all(A), greater<pair<int, int>>()); for(int i = 0; i < A.size(); ++i){ // cout << A[i].first << ' ' << A[i].second << '\n'; int pos = A[i].second; if(fw.get(pos + 1) > 0){ fw.add(pos + 1, -1); }else ans = min(ans, A[i].first + pos - fw.get(pos)); } cout << (ans==MOD?int(A.size()):ans); } int main(){ cin.tie(0); ios::sync_with_stdio(0); int tt = 1, aa; // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(tt--){ solve(); } cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n"; return 0; }

Compilation message (stderr)

mousetrap.cpp: In function 'void solve()':
mousetrap.cpp:97:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |   for(int i = 0; i < A.size(); ++i){
      |                  ~~^~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:112:15: warning: unused variable 'aa' [-Wunused-variable]
  112 |   int tt = 1, aa;
      |               ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...