답안 #855215

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
855215 2023-09-30T20:33:22 Z serifefedartar Parkovi (COCI22_parkovi) C++17
0 / 110
178 ms 39920 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 998244353;
const ll LOGN = 18; 
const ll MAXN = 2e5 + 5;

int n, k, q;
vector<vector<pair<int,ll>>> graph;
vector<int> parks, ans_parks;
ll nearest[MAXN], longest[MAXN];

void dfs(int node, int parent) {
	for (auto u : graph[node]) {
		if (u.f == parent)
			continue;
		dfs(u.f, node);

		nearest[node] = min(nearest[node], nearest[u.f] + u.s);
		if (longest[u.f] + u.s <= q) // bir üste aktarabilme durumu
			longest[node] = max(longest[node], longest[u.f] + u.s);
		else {
			parks.push_back(u.f);
			nearest[node] = min(nearest[node], u.s);
		}
	}

	if (nearest[node] + longest[node] <= q)
		longest[node] = -1e9; 
}

bool check(int x) {
	for (int i = 0; i < MAXN; i++)
		nearest[i] = 1e9, longest[i] = 0;
	parks.clear();

	q = x;
	dfs(1, 1);
	if (longest[1] > x)
		parks.push_back(1);

	return (parks.size() <= k);
}

int main() {
	fast
	cin >> n >> k;

	graph = vector<vector<pair<int,ll>>>(n+1, vector<pair<int,ll>>());
	for (int i = 1; i < n; i++) {
		ll a, b, w;
		cin >> a >> b >> w;
		graph[a].push_back({b, w});
		graph[b].push_back({a, w});
	}

	int L = 0;
	int R = n;
	int ans = -1;
	while (R >= L) {
		int mid = L + (R-L)/2;
		if (check(mid)) {
			R = mid - 1;
			ans = mid;
			ans_parks = parks;
		} else
			L = mid + 1;
	}
	
	cout << ans << "\n";
	set<int> st;
	for (auto u : ans_parks)
		st.insert(u);
	int rem = k - st.size();
	for (int i = 1; i <= n && rem; i++) {
		if (st.count(i) == 0) {
			st.insert(i);
			rem--;
		}
	}

	for (auto u : st)
		cout << u << " ";
	cout << "\n";
}

Compilation message

Main.cpp: In function 'bool check(int)':
Main.cpp:46:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   46 |  return (parks.size() <= k);
      |          ~~~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 3420 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 163 ms 37156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 178 ms 39920 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 3420 KB Output isn't correct
2 Halted 0 ms 0 KB -