답안 #374995

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
374995 2021-03-08T18:49:02 Z YJU Mousetrap (CEOI17_mousetrap) C++14
100 / 100
1094 ms 193900 KB
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector,Ofast")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=1e6+5;
const ld pi=acos(-1);
const ll INF=(1LL<<30);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()

ll n,t,s,dp[N],x,y,fa[N],dg[N];
vector<ll> v[N];

void DFS(ll nd,ll pa){
	
	fa[nd]=pa;
	
	ll ma=0,mb=0;
	for(auto i:v[nd]){
		if(i==pa)continue;
		DFS(i,nd);
		if(dp[i]>=ma)mb=ma,ma=dp[i];
		else if(dp[i]>=mb)mb=dp[i];
	}
	dp[nd]=mb+SZ(v[nd])-1;
}

ll lis[N],now;

bool ck(ll k){
	ll ti=0;
	for(int i=1;i<now;i++){
		ll tmp=0;
		for(auto j:v[lis[i]]){
			if(j==lis[i+1]||j==lis[i-1])continue;
			if(dg[i]+dp[j]+1-(i!=1)>k)++tmp;
		}
		ti+=tmp;k-=tmp;
		if(k<0||ti>i)return 0;
	}
	return 1;
}

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>n>>t>>s;
	REP(i,n-1)cin>>x>>y,v[x].pb(y),v[y].pb(x);
	
	if(s==t){cout<<"0\n";return 0;}
	
	DFS(t,0);
	
	dp[t]=0;
	
	for(int i=s;i;i=fa[i])lis[++now]=i;
	for(int i=now-1;i>=0;i--)dg[i]=dg[i+1]+SZ(v[lis[i]])-1-(lis[i]!=t);
	
	ll r=INF,l=-1;
	while(l<r-1){
		ll mid=(r+l)>>1;
		if(ck(mid)){
			r=mid;
		}else{
			l=mid;
		}
	}
	cout<<r<<"\n";
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 16 ms 23788 KB Output is correct
4 Correct 17 ms 23788 KB Output is correct
5 Correct 16 ms 23788 KB Output is correct
6 Correct 17 ms 23788 KB Output is correct
7 Correct 16 ms 23788 KB Output is correct
8 Correct 17 ms 23788 KB Output is correct
9 Correct 16 ms 23788 KB Output is correct
10 Correct 16 ms 23788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 326 ms 78572 KB Output is correct
2 Correct 296 ms 73188 KB Output is correct
3 Correct 810 ms 76972 KB Output is correct
4 Correct 378 ms 50412 KB Output is correct
5 Correct 805 ms 77036 KB Output is correct
6 Correct 808 ms 77164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 16 ms 23788 KB Output is correct
4 Correct 17 ms 23788 KB Output is correct
5 Correct 16 ms 23788 KB Output is correct
6 Correct 17 ms 23788 KB Output is correct
7 Correct 16 ms 23788 KB Output is correct
8 Correct 17 ms 23788 KB Output is correct
9 Correct 16 ms 23788 KB Output is correct
10 Correct 16 ms 23788 KB Output is correct
11 Correct 16 ms 23788 KB Output is correct
12 Correct 17 ms 23916 KB Output is correct
13 Correct 17 ms 23916 KB Output is correct
14 Correct 17 ms 24044 KB Output is correct
15 Correct 17 ms 24044 KB Output is correct
16 Correct 17 ms 23916 KB Output is correct
17 Correct 17 ms 23884 KB Output is correct
18 Correct 17 ms 23916 KB Output is correct
19 Correct 17 ms 23916 KB Output is correct
20 Correct 16 ms 23916 KB Output is correct
21 Correct 17 ms 23916 KB Output is correct
22 Correct 17 ms 23916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 16 ms 23788 KB Output is correct
4 Correct 17 ms 23788 KB Output is correct
5 Correct 16 ms 23788 KB Output is correct
6 Correct 17 ms 23788 KB Output is correct
7 Correct 16 ms 23788 KB Output is correct
8 Correct 17 ms 23788 KB Output is correct
9 Correct 16 ms 23788 KB Output is correct
10 Correct 16 ms 23788 KB Output is correct
11 Correct 326 ms 78572 KB Output is correct
12 Correct 296 ms 73188 KB Output is correct
13 Correct 810 ms 76972 KB Output is correct
14 Correct 378 ms 50412 KB Output is correct
15 Correct 805 ms 77036 KB Output is correct
16 Correct 808 ms 77164 KB Output is correct
17 Correct 16 ms 23788 KB Output is correct
18 Correct 17 ms 23916 KB Output is correct
19 Correct 17 ms 23916 KB Output is correct
20 Correct 17 ms 24044 KB Output is correct
21 Correct 17 ms 24044 KB Output is correct
22 Correct 17 ms 23916 KB Output is correct
23 Correct 17 ms 23884 KB Output is correct
24 Correct 17 ms 23916 KB Output is correct
25 Correct 17 ms 23916 KB Output is correct
26 Correct 16 ms 23916 KB Output is correct
27 Correct 17 ms 23916 KB Output is correct
28 Correct 17 ms 23916 KB Output is correct
29 Correct 16 ms 23788 KB Output is correct
30 Correct 338 ms 92140 KB Output is correct
31 Correct 342 ms 92012 KB Output is correct
32 Correct 535 ms 193900 KB Output is correct
33 Correct 386 ms 178432 KB Output is correct
34 Correct 839 ms 90128 KB Output is correct
35 Correct 818 ms 90220 KB Output is correct
36 Correct 1094 ms 101236 KB Output is correct
37 Correct 1066 ms 101276 KB Output is correct
38 Correct 802 ms 105452 KB Output is correct
39 Correct 813 ms 105588 KB Output is correct
40 Correct 785 ms 105708 KB Output is correct