Submission #144773

#TimeUsernameProblemLanguageResultExecution timeMemory
144773liwiMousetrap (CEOI17_mousetrap)C++11
100 / 100
1732 ms151212 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; #define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0) char _; #define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end()) #define all(a) a.begin(),a.end() #define println printf("\n"); #define readln(x) getline(cin,x); #define pb push_back #define endl "\n" #define INT_INF 0x3f3f3f3f #define LL_INF 0x3f3f3f3f3f3f3f3f #define MOD 1000000007 #define MOD2 1190492669 #define SEED 131 #define mp make_pair #define fastio cin.tie(0); cin.sync_with_stdio(0); #define MAXN 1000005 typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef unordered_map<int,int> umii; typedef pair<int,int> pii; typedef pair<double,double> pdd; typedef pair<ll,ll> pll; typedef pair<int,pii> triple; typedef int8_t byte; mt19937 g1(time(0)); int randint(int a, int b){return uniform_int_distribution<int>(a, b)(g1);} ll randlong(ll a,ll b){return uniform_int_distribution<long long>(a, b)(g1);} ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);} ll lcm(ll a, ll b){return a*b/gcd(a,b);} ll fpow(ll b, ll exp, ll mod){if(exp == 0) return 1;ll t = fpow(b,exp/2,mod);if(exp&1) return t*t%mod*b%mod;return t*t%mod;} ll divmod(ll i, ll j, ll mod){i%=mod,j%=mod;return i*fpow(j,mod-2,mod)%mod;} int num_nodes,T,M,dp[MAXN],par[MAXN],depth[MAXN],sum[MAXN]; bool done[MAXN],marked[MAXN]; vector<int> connections[MAXN]; inline bool cmp(int a, int b){ return depth[a]>depth[b]; } int dfs(int node, int prev){ for(int check:connections[node]){ if(check == prev) continue; par[check] = node; depth[check] = depth[node]+1; dfs(check,node); } } int solve(int node, int prev){ int bst = -1, bst2 = -1, cnt = 0; for(int check:connections[node]){ if(check == prev) continue; cnt++; } sum[node] = sum[prev]+cnt; for(int check:connections[node]){ if(check == prev) continue; int n = solve(check,node); if(n >= bst) bst2 = bst, bst = n; else if(n > bst2) bst2 = n; } if(cnt == 0) return dp[node] = 0; if(bst2 == -1) return dp[node] = 1; return dp[node] = bst2+cnt; } bool works(int mid){ memset(done,false,sizeof done); deque<int> v; int current = M, cnt = 0, og = 0; while(current != T){ int mm = 0; for(int check:connections[current]){ if(check == par[current] || marked[check]) continue; int cost = cnt+sum[current]+dp[check]-depth[current]-(current!=M); if(cost > mid){ v.pb(check); mm++; }else done[check] = true; } cnt+=mm, og+=mm; current = par[current]; } sort(all(v),cmp); current = M; while(current != T){ if(v.size()){ int n = v.front(); v.pop_front(); done[n] = true; } for(int check:connections[current]){ if(check == par[current] || done[check] || marked[check]) continue; return false; } current = par[current]; } if(og <= mid) return true; return false; } int main(){ scanf("%d %d %d",&num_nodes,&T,&M); for(int i=1; i<num_nodes; i++){ int a,b; scanf(" %d %d",&a,&b); connections[a].pb(b); connections[b].pb(a); } dfs(T,-1); solve(T,-1); int curr = M; while(curr){ marked[curr] = true; curr = par[curr]; } int low = 0, high = 2*num_nodes, ans = high; while(low <= high){ int mid = (low+high)/2; if(works(mid)){ ans = mid; high = mid-1; }else low = mid+1; } printf("%d\n",ans); } /* 3 1 3 1 2 2 3 ans=0 */

Compilation message (stderr)

mousetrap.cpp: In function 'int dfs(int, int)':
mousetrap.cpp:62:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:117:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d",&num_nodes,&T,&M);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:119:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a,b; scanf(" %d %d",&a,&b);
            ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...