Submission #1067757

# Submission time Handle Problem Language Result Execution time Memory
1067757 2024-08-21T02:53:08 Z sleepntsheep Torrent (COI16_torrent) C++17
100 / 100
401 ms 31908 KB
#include <cstdio>
#include <numeric>
#include <algorithm>
#include <climits>
#include <vector>

using namespace std;
template <typename T>
using ve = vector<T>;

#define N 300005

int ans = INT_MAX;
int n, a, b, cb[N], dp[N], par[N];
ve<int> g[N], ab;

void dfs(int u, int p) {
	par[u] = p;
	for (auto v : g[u]) if (v != p) dfs(v, u);
}

void efs(int u, int p) {
	dp[u] = 0;

	ve<int> chdp;
	for (auto v : g[u]) if (v != p) {
		efs(v, u);
		chdp.push_back(dp[v]);
	}

	sort(chdp.begin(), chdp.end(), greater<>());

	for (int i = 0; i < (int)chdp.size(); ++i) 
		dp[u] = max(dp[u], chdp[i] + i + 1);

}

void Del(int u, int v) {
	g[u].erase(find(g[u].begin(), g[u].end(), v));
	g[v].erase(find(g[v].begin(), g[v].end(), u));
}
void Ins(int u, int v) {
	g[u].push_back(v), g[v].push_back(u);
}


int Cut_At(int i){
	Del(ab[i], par[ab[i]]);
	efs(a, -1), efs(b, -1);
	int z = max(dp[a], dp[b]);
	Ins(ab[i], par[ab[i]]);
	ans = min(ans, z);
	return dp[a] - dp[b];
}

int main() {
	scanf("%d%d%d", &n, &a, &b);
	for (int u, v, i = 1; i < n; ++i) scanf("%d%d", &u, &v), Ins(u, v);

	dfs(a, -1);

	for (int c = par[b]; c != a; c = par[c]) ab.push_back(c);
	reverse(ab.begin(), ab.end());
	ab.push_back(b);

	int lb = -1, ub = (int)ab.size();
	while (ub - lb > 1) {
		int md = lb + (ub - lb) / 2;
		int ii = Cut_At(md);
		if (ii >= 0) lb = md;
		else ub = md;
	}
	lb = -1, ub = (int)ab.size();
	while (ub - lb > 1) {
		int md = lb + (ub - lb) / 2;
		int ii = Cut_At(md);
		if (ii < 0) lb = md;
		else ub = md;
	}

	printf("%d\n", ans);
	return 0;
}

Compilation message

torrent.cpp: In function 'int main()':
torrent.cpp:57:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |  scanf("%d%d%d", &n, &a, &b);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:58:41: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |  for (int u, v, i = 1; i < n; ++i) scanf("%d%d", &u, &v), Ins(u, v);
      |                                    ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 4 ms 7516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 342 ms 21904 KB Output is correct
2 Correct 367 ms 27616 KB Output is correct
3 Correct 384 ms 30136 KB Output is correct
4 Correct 401 ms 28424 KB Output is correct
5 Correct 350 ms 25612 KB Output is correct
6 Correct 366 ms 26360 KB Output is correct
7 Correct 343 ms 31908 KB Output is correct