Submission #200264

# Submission time Handle Problem Language Result Execution time Memory
200264 2020-02-05T19:43:06 Z alishahali1382 Mousetrap (CEOI17_mousetrap) C++14
25 / 100
1758 ms 99512 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 1000010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans, root, mouse;
int par[MAXN], h[MAXN];
int val[MAXN], dp[MAXN], dp2[MAXN];
pii mx[MAXN][3];
vector<int> G[MAXN];

void dfs1(int node){
	for (int v:G[node]) if (v!=par[node]){
		par[v]=node;
		h[v]=h[node]+1;
		dfs1(v);
	}
}

void dfs2(int node){
	for (int v:G[node]) if (v!=par[node]) val[node]++;
	for (int v:G[node]) if (v!=par[node]) dfs2(v);
}

void dfs3(int node){
	mx[node][0]=mx[node][1]=mx[node][2]={val[node], node};
	for (int v:G[node]) if (v!=par[node]){
		dfs3(v);
		pii p={dp[v]+val[node], v};
		if (mx[node][0]<p) swap(mx[node][0], p);
		if (mx[node][1]<p) swap(mx[node][1], p);
		if (mx[node][2]<p) swap(mx[node][2], p);
	}
	dp[node]=mx[node][1].first;
}

void dfs4(int node){
	for (int v:G[node]) if (v!=par[node]){
		vector<int> vec={dp2[node], val[node]-1};
		if (mx[node][0].second!=v) vec.pb(mx[node][0].first);
		if (mx[node][1].second!=v && mx[node][1].second!=node && mx[node][1]!=mx[node][0]) vec.pb(mx[node][1].first);
		if (mx[node][2].second!=v && mx[node][2].second!=node && mx[node][1]!=mx[node][2]) vec.pb(mx[node][2].first);
		sort(all(vec));
		reverse(all(vec));
		dp2[v]=vec[1];
	}
	for (int v:G[node]) if (v!=par[node]) dfs4(v);
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>root>>mouse;
	for (int i=1; i<n; i++){
		cin>>u>>v;
		G[u].pb(v);
		G[v].pb(u);
	}
	dfs1(root);
	for (root=mouse; par[par[root]]; root=par[root]);
	dfs2(root);
	dfs3(root);
	dfs4(root);
	
	ans=dp[mouse];
	cout<<ans<<'\n';
	return 0;
	
	
	for (int i=2; i<=n; i++) debug2(i, val[i])
	for (int i=2; i<=n; i++) cerr<<i<<" : "<<dp[i]<<' '<<dp2[i]<<endl;
	
	return 0;
}
/*
10 1 2
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10

5 1 2
1 2
2 3
2 4
2 5

10 1 4
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10

8 1 2
1 2
2 3
2 4
4 5
4 6
3 7
3 8

*/
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 23800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 550 ms 96248 KB Output is correct
2 Correct 510 ms 89152 KB Output is correct
3 Correct 1758 ms 99320 KB Output is correct
4 Correct 804 ms 61560 KB Output is correct
5 Correct 1741 ms 99424 KB Output is correct
6 Correct 1707 ms 99512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 23800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 23800 KB Output isn't correct
2 Halted 0 ms 0 KB -