답안 #1067733

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067733 2024-08-21T02:32:08 Z sleepntsheep Torrent (COI16_torrent) C++17
31 / 100
4 ms 604 KB
#include <cstdio>
#include <numeric>
#include <algorithm>
#include <climits>
#include <vector>

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

#define N 1005

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);

}

int main() {
	scanf("%d%d%d", &n, &a, &b);
	for (int u, v, i = 1; i < n; ++i) scanf("%d%d", &u, &v), g[u].push_back(v), g[v].push_back(u);

	dfs(a, -1);

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

	for (int c = b; c != a; c = par[c]) {
		g[c].erase(find(g[c].begin(), g[c].end(), par[c]));
		g[par[c]].erase(find(g[par[c]].begin(), g[par[c]].end(), c));
		efs(a, -1);
		efs(b, -1);
		ans = min(ans, max(dp[a], dp[b]));
		g[c].push_back(par[c]);
		g[par[c]].push_back(c);
	}

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

Compilation message

torrent.cpp: In function 'int main()':
torrent.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |  scanf("%d%d%d", &n, &a, &b);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:40:41: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |  for (int u, v, i = 1; i < n; ++i) scanf("%d%d", &u, &v), g[u].push_back(v), g[v].push_back(u);
      |                                    ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 4 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -