답안 #144773

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
144773 2019-08-17T16:35:45 Z liwi Mousetrap (CEOI17_mousetrap) C++11
100 / 100
1732 ms 151212 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];
	}
	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

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);
            ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 24824 KB Output is correct
2 Correct 24 ms 24824 KB Output is correct
3 Correct 24 ms 24824 KB Output is correct
4 Correct 24 ms 24824 KB Output is correct
5 Correct 24 ms 24824 KB Output is correct
6 Correct 24 ms 24824 KB Output is correct
7 Correct 24 ms 24828 KB Output is correct
8 Correct 24 ms 24824 KB Output is correct
9 Correct 24 ms 24824 KB Output is correct
10 Correct 24 ms 24800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 456 ms 71808 KB Output is correct
2 Correct 425 ms 67192 KB Output is correct
3 Correct 1238 ms 72912 KB Output is correct
4 Correct 583 ms 48956 KB Output is correct
5 Correct 1177 ms 73036 KB Output is correct
6 Correct 1126 ms 73040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 24824 KB Output is correct
2 Correct 24 ms 24824 KB Output is correct
3 Correct 24 ms 24824 KB Output is correct
4 Correct 24 ms 24824 KB Output is correct
5 Correct 24 ms 24824 KB Output is correct
6 Correct 24 ms 24824 KB Output is correct
7 Correct 24 ms 24828 KB Output is correct
8 Correct 24 ms 24824 KB Output is correct
9 Correct 24 ms 24824 KB Output is correct
10 Correct 24 ms 24800 KB Output is correct
11 Correct 24 ms 24952 KB Output is correct
12 Correct 25 ms 24952 KB Output is correct
13 Correct 31 ms 24824 KB Output is correct
14 Correct 30 ms 24952 KB Output is correct
15 Correct 31 ms 24952 KB Output is correct
16 Correct 25 ms 24824 KB Output is correct
17 Correct 25 ms 25080 KB Output is correct
18 Correct 25 ms 24952 KB Output is correct
19 Correct 24 ms 24824 KB Output is correct
20 Correct 25 ms 24952 KB Output is correct
21 Correct 25 ms 24952 KB Output is correct
22 Correct 25 ms 24824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 24824 KB Output is correct
2 Correct 24 ms 24824 KB Output is correct
3 Correct 24 ms 24824 KB Output is correct
4 Correct 24 ms 24824 KB Output is correct
5 Correct 24 ms 24824 KB Output is correct
6 Correct 24 ms 24824 KB Output is correct
7 Correct 24 ms 24828 KB Output is correct
8 Correct 24 ms 24824 KB Output is correct
9 Correct 24 ms 24824 KB Output is correct
10 Correct 24 ms 24800 KB Output is correct
11 Correct 456 ms 71808 KB Output is correct
12 Correct 425 ms 67192 KB Output is correct
13 Correct 1238 ms 72912 KB Output is correct
14 Correct 583 ms 48956 KB Output is correct
15 Correct 1177 ms 73036 KB Output is correct
16 Correct 1126 ms 73040 KB Output is correct
17 Correct 24 ms 24952 KB Output is correct
18 Correct 25 ms 24952 KB Output is correct
19 Correct 31 ms 24824 KB Output is correct
20 Correct 30 ms 24952 KB Output is correct
21 Correct 31 ms 24952 KB Output is correct
22 Correct 25 ms 24824 KB Output is correct
23 Correct 25 ms 25080 KB Output is correct
24 Correct 25 ms 24952 KB Output is correct
25 Correct 24 ms 24824 KB Output is correct
26 Correct 25 ms 24952 KB Output is correct
27 Correct 25 ms 24952 KB Output is correct
28 Correct 25 ms 24824 KB Output is correct
29 Correct 24 ms 24824 KB Output is correct
30 Correct 456 ms 71816 KB Output is correct
31 Correct 457 ms 71872 KB Output is correct
32 Correct 877 ms 151212 KB Output is correct
33 Correct 529 ms 150008 KB Output is correct
34 Correct 1140 ms 73080 KB Output is correct
35 Correct 1129 ms 72948 KB Output is correct
36 Correct 1732 ms 81776 KB Output is correct
37 Correct 1623 ms 81932 KB Output is correct
38 Correct 1117 ms 83840 KB Output is correct
39 Correct 1142 ms 84048 KB Output is correct
40 Correct 1164 ms 83932 KB Output is correct