Submission #144772

# Submission time Handle Problem Language Result Execution time Memory
144772 2019-08-17T16:34:52 Z liwi Mousetrap (CEOI17_mousetrap) C++11
100 / 100
1993 ms 164512 KB
#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];
	}
//	cout<<works(7)<<endl;
	int low = 0, high = 100000000, 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

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 time Memory Grader output
1 Correct 26 ms 24824 KB Output is correct
2 Correct 25 ms 24824 KB Output is correct
3 Correct 25 ms 24824 KB Output is correct
4 Correct 27 ms 24868 KB Output is correct
5 Correct 26 ms 24824 KB Output is correct
6 Correct 25 ms 24824 KB Output is correct
7 Correct 25 ms 24824 KB Output is correct
8 Correct 25 ms 24868 KB Output is correct
9 Correct 26 ms 24824 KB Output is correct
10 Correct 25 ms 24824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 459 ms 71836 KB Output is correct
2 Correct 409 ms 67216 KB Output is correct
3 Correct 1120 ms 72840 KB Output is correct
4 Correct 521 ms 48804 KB Output is correct
5 Correct 1115 ms 72952 KB Output is correct
6 Correct 1129 ms 73128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 24824 KB Output is correct
2 Correct 25 ms 24824 KB Output is correct
3 Correct 25 ms 24824 KB Output is correct
4 Correct 27 ms 24868 KB Output is correct
5 Correct 26 ms 24824 KB Output is correct
6 Correct 25 ms 24824 KB Output is correct
7 Correct 25 ms 24824 KB Output is correct
8 Correct 25 ms 24868 KB Output is correct
9 Correct 26 ms 24824 KB Output is correct
10 Correct 25 ms 24824 KB Output is correct
11 Correct 31 ms 24828 KB Output is correct
12 Correct 26 ms 24924 KB Output is correct
13 Correct 27 ms 24952 KB Output is correct
14 Correct 27 ms 25028 KB Output is correct
15 Correct 34 ms 25052 KB Output is correct
16 Correct 26 ms 24952 KB Output is correct
17 Correct 28 ms 24952 KB Output is correct
18 Correct 24 ms 24824 KB Output is correct
19 Correct 26 ms 24824 KB Output is correct
20 Correct 26 ms 24828 KB Output is correct
21 Correct 26 ms 24828 KB Output is correct
22 Correct 26 ms 24824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 24824 KB Output is correct
2 Correct 25 ms 24824 KB Output is correct
3 Correct 25 ms 24824 KB Output is correct
4 Correct 27 ms 24868 KB Output is correct
5 Correct 26 ms 24824 KB Output is correct
6 Correct 25 ms 24824 KB Output is correct
7 Correct 25 ms 24824 KB Output is correct
8 Correct 25 ms 24868 KB Output is correct
9 Correct 26 ms 24824 KB Output is correct
10 Correct 25 ms 24824 KB Output is correct
11 Correct 459 ms 71836 KB Output is correct
12 Correct 409 ms 67216 KB Output is correct
13 Correct 1120 ms 72840 KB Output is correct
14 Correct 521 ms 48804 KB Output is correct
15 Correct 1115 ms 72952 KB Output is correct
16 Correct 1129 ms 73128 KB Output is correct
17 Correct 31 ms 24828 KB Output is correct
18 Correct 26 ms 24924 KB Output is correct
19 Correct 27 ms 24952 KB Output is correct
20 Correct 27 ms 25028 KB Output is correct
21 Correct 34 ms 25052 KB Output is correct
22 Correct 26 ms 24952 KB Output is correct
23 Correct 28 ms 24952 KB Output is correct
24 Correct 24 ms 24824 KB Output is correct
25 Correct 26 ms 24824 KB Output is correct
26 Correct 26 ms 24828 KB Output is correct
27 Correct 26 ms 24828 KB Output is correct
28 Correct 26 ms 24824 KB Output is correct
29 Correct 26 ms 24796 KB Output is correct
30 Correct 462 ms 85180 KB Output is correct
31 Correct 457 ms 85212 KB Output is correct
32 Correct 925 ms 164512 KB Output is correct
33 Correct 530 ms 163440 KB Output is correct
34 Correct 1250 ms 86156 KB Output is correct
35 Correct 1137 ms 86224 KB Output is correct
36 Correct 1993 ms 95072 KB Output is correct
37 Correct 1809 ms 95168 KB Output is correct
38 Correct 1152 ms 97224 KB Output is correct
39 Correct 1175 ms 97200 KB Output is correct
40 Correct 1152 ms 97232 KB Output is correct