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>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define inf (int)1e18
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1 << j))
#define onbit(i, j) (i | (1 << j))
#define vi vector<int>
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
int n, k;
vi ke[maxn], adj[maxn], save;
pii edge[maxn];
int h[maxn], d[maxn], a[maxn];
bool sheep[maxn], vis[maxn];
void bfs()
{
memset(d,-1,sizeof d);
queue<int> q;
fo(i, 1, n) if (sheep[i])
{
q.push(i), d[i] = 0;
}
while (q.size())
{
int u = q.front();
q.pop();
for (int v : ke[u])
{
if (d[v] == -1)
{
d[v] = d[u] + 1;
q.push(v);
}
}
}
}
void dfs(int u, int par = 0, int now = 0)
{
if (d[now] + h[now] < d[u] + h[u])
now = u;
a[u] = now;
for (int v : ke[u])
{
if (v == par)
continue;
h[v] = h[u] + 1;
dfs(v, u, now);
}
}
void dfs1(int u, int par = 0)
{
vis[u] = 1;
for (int v : ke[u]) if(d[u] == d[v] + 1)
{
if (vis[v])
continue;
dfs1(v, u);
}
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
fo(i, 1, n - 1)
{
int u, v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
edge[i] = {u, v};
}
fo(i, 1, k)
{
int x;
cin >> x;
sheep[x] = 1;
}
int node = 1;
fo(i, 1, n) if (sheep[i])
{
node = i;
break;
}
bfs();
dfs(node);
a[node] = node;
fo(i, 1, n) if (sheep[i]) adj[h[i]].pb(i);
fod(i, n, 0) for (int it : adj[i]) save.pb(it);
vi ans;
for (int it : save) if(!vis[it])
{
ans.pb(a[it]);
dfs1(a[it]);
}
cout << ans.size();
en;
for (int it : ans)
cout << it << ' ';
en;
}
Compilation message (stderr)
pastiri.cpp:91:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
91 | main()
| ^~~~
pastiri.cpp: In function 'int main()':
pastiri.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
pastiri.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |