Submission #76416

# Submission time Handle Problem Language Result Execution time Memory
76416 2018-09-13T13:37:56 Z MrTEK Torrent (COI16_torrent) C++14
31 / 100
2000 ms 25552 KB
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define len(a) (int)a.size()
#define fi first
#define sc second
#define d1(w) cerr<<#w<<":"<<w<<endl;
#define d2(w,c) cerr<<#w<<":"<<w<<" "<<#c<<":"<<c<<endl;
#define d3(w,c,z) cerr<<#w<<":"<<w<<" "<<#c<<":"<<c<<" "<<#z<<":"<<z<<endl;
#define left ind+ind
#define right ind+ind+1
#define mid (l+r)/2
#define FAST_IO ios_base::sync_with_stdio(false);
#define endl '\n'
#define bit __builtin_popcount

typedef long long int ll;

const int maxn = 620;
const long long LINF = 1e18;
const int LOG = 31;
const int INF = 1e9;
const int P = 31;
const int N = 3e5 + 5;
const int M = 1e6 + 5;
const int SQ = 350;
const int MOD = 1e9 + 7;

typedef long long int lli;
typedef pair<int,int> pii;

vector <pii> ed[N],path;
int n,a,b,vis[N],vis2[N],id,dp[N],h[N],ans = INF;

int solve(int cur,int back,int back_edge) {
	// if (back_edge != -1 && dp[back_edge] != -1)
	// 	return dp[back_edge];
	vector <int> v;
	for (auto i : ed[cur])
		if (i.fi != back && h[i.sc] == 0)
			v.pb(solve(i.fi,cur,i.sc));
	sort(v.begin(),v.end(),greater<int>());
	int re = 0,cnt = 0;
	for (auto i : v)	
		re = max(re,(++cnt) + i);
	if (back_edge == -1) return re;
	return dp[back_edge] = re;
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	// freopen(".gir","r",stdin);
	// freopen(".cik","w",stdout);

	cin >> n >> a >> b;
	for (int i = 1 ; i < n ; i++) {
		int u,v;
		cin >> u >> v;
		ed[u].pb(mp(v,++id));
		ed[v].pb(mp(u,++id));
	}

	queue <pair< pair <int,int>,int> > Q;
	Q.push(mp(mp(a,-1),-1));
	while(len(Q)) {
		auto temp = Q.front();
		Q.pop();
		if (vis[temp.fi.fi]) continue;
		vis[temp.fi.fi] = temp.fi.sc;
		vis2[temp.fi.fi] = temp.sc;
		if (temp.fi.fi == b) {
			int x = b;
			while(x != -1) {
				path.pb(mp(x,vis2[x]));
				x = vis[x];
			}
			break;
		}
		for (auto i : ed[temp.fi.fi]) {
			Q.push(mp(mp(i.fi,temp.fi.fi),i.sc));
		}
	}
	memset(dp,-1,sizeof dp);
	reverse(path.begin(),path.end());
	for (auto i : path) {
		if (i.sc != -1) {
			if (i.sc % 2 == 0)
				h[i.sc] = h[i.sc - 1] = 1;
			else
				h[i.sc] = h[i.sc + 1] = 1;
			ans = min(ans,max(solve(a,-1,-1),solve(b,-1,-1)));
			if (i.sc % 2 == 0)
				h[i.sc] = h[i.sc - 1] = 0;
			else
				h[i.sc] = h[i.sc + 1] = 0;
		} 
	}
	cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 8568 KB Output is correct
2 Correct 14 ms 8820 KB Output is correct
3 Correct 21 ms 8868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2062 ms 25552 KB Time limit exceeded
2 Halted 0 ms 0 KB -