# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1245464 | ducdev | Pastiri (COI20_pastiri) | C++20 | 123 ms | 33496 KiB |
// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 5e5;
const int MOD = 1e9 + 7;
const int INF = 1e9;
template <class X, class Y>
bool minimize(X &x, const Y &y) {
if (x <= y) return false;
x = y;
return true;
};
int n, k;
int sheepIdx[MAX_N + 5];
vector<int> adj[MAX_N + 5];
namespace SUBTASK_1 {
const int N = 5e5;
int dp[N + 5];
bool checkSubtask() {
for (int u = 1; u <= n; u++)
if (adj[u].size() > 2) return false;
return true;
};
void trace(int i) {
vector<int> t;
while (i > 0) {
if (dp[i] == dp[i - 1] + 1) {
t.push_back(sheepIdx[i]);
i--;
} else {
int prevIdx = sheepIdx[i - 1];
int curIdx = sheepIdx[i];
t.push_back((curIdx + prevIdx + 1) >> 1);
i -= 2;
};
};
for (int idx : t) cout << idx << ' ';
cout << '\n';
};
void Solve() {
for (int i = 1; i <= n; i++) dp[i] = INF;
dp[1] = 1;
for (int i = 2; i <= k; i++) {
int prevIdx = sheepIdx[i - 1];
int curIdx = sheepIdx[i];
if ((curIdx - prevIdx + 1) & 1) {
minimize(dp[i], dp[i - 2] + 1);
};
minimize(dp[i], dp[i - 1] + 1);
};
cout << dp[k] << '\n';
trace(k);
};
}; // namespace SUBTASK_1
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("PASTIRI.INP", "r")) {
freopen("PASTIRI.INP", "r", stdin);
freopen("PASTIRI.OUT", "w", stdout);
};
cin >> n >> k;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
};
for (int i = 1; i <= k; i++) {
cin >> sheepIdx[i];
};
sort(sheepIdx + 1, sheepIdx + k + 1);
if (SUBTASK_1::checkSubtask())
return SUBTASK_1::Solve(), 0;
};
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |