# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
732177 | Redhood | Pastiri (COI20_pastiri) | C++14 | 549 ms | 83224 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define sz(x) int((x).size())
#define all(x) begin(x), end(x)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#ifdef DEBUG
void __dbg(int x) {cout << x;}
void __dbg(bool x) {cout << x;}
void __dbg(ll x) {cout << x;}
void __dbg(auto x) {
int f = 0;
for (int i : x)
cout << (f++ ? ", " : ""), __dbg(i);
}
void _dbg() {
cout << "]\e[39m" << endl;
}
template<typename T, typename... V>
void _dbg(T t, V... v) {
__dbg(t);
if (sizeof...(v))
cout << ", ";
_dbg(v...);
}
#define debug(x...) do {cout << "\e[92m" << __func__ << ":" << __LINE__ << "\t[" << #x << "] = ["; _dbg(x); } while (false)
#else
#define debug(...) 42
#endif
void mini(auto &a, auto b) {a = min(a, b); }
void maxi(auto &a, auto b) {a = max(a, b); }
int n, k;
vector<vi> G;
vi dists, tribes, comp;
const int NOT = -1;
const int TOL = -2;
int dfs(int i, int p) {
bool tolight = (dists[i] == 0);
bool lightchild = false;
int bestcomp = -1; // 0 = można tylko tutaj
for (int j : G[i]) if (j != p) {
int x = dfs(j, i);
if (x >= 0 && dists[j] == 1 + dists[i])
maxi(bestcomp, x);
if (x == TOL) {
if (dists[j] + 1 == dists[i]) {
lightchild = true;
}
else if (dists[j] == 1 + dists[i]) {
comp[j] = true;
maxi(bestcomp, dists[j] - 1);
}
else
comp[j] = true;
}
}
debug(i, dists[i], bestcomp, tolight, lightchild);
if (bestcomp >= 0)
tolight = false;
if (bestcomp >= 1)
lightchild = false;
if (p == -1 && (tolight || lightchild))
comp[i] = true;
if (tolight || lightchild)
return TOL;
if (bestcomp >= 1)
return bestcomp - 1;
return NOT;
}
void solve() {
cin >> n >> k;
G.assign(n, vi());
dists.assign(n, -1);
comp.assign(n, 0);
tribes.resize(k);
rep(_, n-1) {
int u, v;
cin >> u >> v;
--u; --v;
G[u].push_back(v);
G[v].push_back(u);
}
deque<int> bfs;
rep(i, k) {
cin >> tribes[i];
--tribes[i];
dists[tribes[i]] = 0;
bfs.push_back(tribes[i]);
}
while (sz(bfs)) {
int x = bfs[0];
bfs.pop_front();
for (int i : G[x]) {
if (dists[i] != -1)
continue;
dists[i] = dists[x] + 1;
bfs.push_back(i);
}
}
debug(tribes);
debug(dists);
dfs(0, -1);
int cc = accumulate(all(comp), 0);
cout << cc << '\n';
rep(i, n) if (comp[i])
cout << i+1 << ' ';
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int z = 1;
// cin >> z;
while (z--)
solve();
}
Compilation message (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... |