Submission #9103846


Source Code Expand

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
struct HLDecomposition {
	vector<set<int>> g; vector<int> vid, head, heavy, parent, depth, inv, in, out;
	HLDecomposition() {} HLDecomposition(int n) { init(n); }
	void init(int n) {
		g.resize(n); vid.resize(n, -1); head.resize(n); heavy.resize(n, -1);
		parent.resize(n); depth.resize(n); inv.resize(n); in.resize(n); out.resize(n);
	}
	void add(int u, int v) { g[u].insert(v); g[v].insert(u); }
	void build(int root) { dfs(root, -1); t = 0; dfs_hld(root); }

	int dfs(int curr, int prev) {
		parent[curr] = prev; int sub = 1, max_sub = 0;
		heavy[curr] = -1;
		for (int next : g[curr]) if (next != prev) {
			depth[next] = depth[curr] + 1;
			int sub_next = dfs(next, curr); sub += sub_next;
			if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next;
		}return sub;
	}

	int t = 0;
	void dfs_hld(int v = 0)
	{
		vid[v] = in[v] = t;
		t++;
		inv[in[v]] = v;
		if (0 <= heavy[v]) {
			head[heavy[v]] = head[v];
			dfs_hld(heavy[v]);
		}
		for (auto u : g[v]) if (depth[v] < depth[u])  if (u != heavy[v]) {
			head[u] = u;
			dfs_hld(u);
		}
		out[v] = t;
	}


	void foreach(int u, int v, function<void(int, int)> f) { // [x,y]
		if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]);
		if (head[u] != head[v]) foreach(u, parent[head[v]], f);
	}
	int ancestor(int from, int times) {
		while (true) {
			if (depth[head[from]] > depth[from] - times) {
				times -= depth[from] - depth[head[from]] + 1; if (head[from] == 0)return -1; from = parent[head[from]];
			}
			else return inv[vid[from] - times];
		}
	}
	int lca(int u, int v) {
		if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u;
		return lca(u, parent[head[v]]);
	}
	int distance(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }
	int child(int parent, int child, int times) {
		assert(depth[parent] < depth[child]);
		int d = distance(parent, child); assert(times - 1 <= d); return ancestor(child, d - times);
	}
	int go(int from, int to, int times) {
		int d = distance(from, to); assert(0 <= times and times <= d);
		int lc = lca(from, to); if (lc == to)return ancestor(from, times); if (lc == from)return child(from, to, times);
		int dd = distance(from, lc); if (dd <= times)return go(lc, to, times - dd); return ancestor(from, times);
	}
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan0
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














int N, u, v;
//---------------------------------------------------------------------------------------------------
void _main() {
	cin >> N >> u >> v;
	u--; v--;

	HLDecomposition hld(N);
	rep(i, 0, N - 1) {
		int a, b; cin >> a >> b;
		a--; b--;
		hld.add(a, b);
	}
	hld.build(u);

	int ans = 0;
	rep(leaf, 0, N) if (hld.g[leaf].size() == 1) {
		int lc = hld.lca(leaf, v);

		int taka = hld.distance(lc, u);
		int aoki = hld.distance(lc, v);

		if (aoki <= taka) continue;
		
		int taka2 = hld.distance(u, leaf);
		int aoki2 = hld.distance(v, leaf);

		int cnt = taka2;

		int d = aoki2 - taka2;
		cnt += d - 1;
		chmax(ans, cnt);
	}
	cout << ans << endl;
}





Submission Info

Submission Time
Task F - Playing Tag on Tree
User hamayanhamayan
Language C++14 (GCC 5.4.1)
Score 600
Code Size 4309 Byte
Status AC
Exec Time 79 ms
Memory 36352 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 26
Set Name Test Cases
Sample sample_01, sample_02, sample_03, sample_04
All path1_01, path1_02, path1_03, path2_01, path2_02, path2_03, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, random_10, random_11, random_12, random_13, sample_01, sample_02, sample_03, sample_04, star_01, star_02, star_03
Case Name Status Exec Time Memory
path1_01 AC 66 ms 36352 KiB
path1_02 AC 38 ms 22912 KiB
path1_03 AC 59 ms 31872 KiB
path2_01 AC 79 ms 30592 KiB
path2_02 AC 77 ms 33920 KiB
path2_03 AC 79 ms 36096 KiB
random_01 AC 2 ms 640 KiB
random_02 AC 4 ms 1024 KiB
random_03 AC 6 ms 1792 KiB
random_04 AC 4 ms 1152 KiB
random_05 AC 7 ms 1920 KiB
random_06 AC 38 ms 9728 KiB
random_07 AC 62 ms 14976 KiB
random_08 AC 35 ms 9088 KiB
random_09 AC 46 ms 11648 KiB
random_10 AC 65 ms 16000 KiB
random_11 AC 73 ms 17408 KiB
random_12 AC 77 ms 17408 KiB
random_13 AC 73 ms 17536 KiB
sample_01 AC 1 ms 256 KiB
sample_02 AC 1 ms 256 KiB
sample_03 AC 1 ms 256 KiB
sample_04 AC 1 ms 256 KiB
star_01 AC 49 ms 12032 KiB
star_02 AC 50 ms 12032 KiB
star_03 AC 58 ms 13824 KiB