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>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef __int128 sll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> piii;
ll const INF = 1e13;
ll n, k, a, b;
vector<ll> sheep;
vector<ll> adj[500005];
vector<ll> ans;
ll depth[500005], dist[500005], visited[500005], dp[500005], save[500005], done[500005];
bool compare(ll x, ll y)
{
return depth[x] > depth[y];
}
void dfs(ll from, ll to)
{
if (dist[to] + depth[to] > dp[to]) dp[to] = dist[to] + depth[to], save[to] = to;
for (ll u: adj[to])
{
if (u==from) continue;
depth[u] = depth[to] + 1;
dp[u] = dp[to];
save[u] = save[to];
dfs(to, u);
}
}
void mark( ll to)
{
done[to] = true;
for (ll u: adj[to])
{
if (done[u] || dist[u]!=dist[to]-1) continue;
mark(u);
}
}
int main()
{
input2(n, k);
for (ll i=0; i<n-1; ++i)
{
input2(a, b);
adj[a].pb(b);
adj[b].pb(a);
}
sheep.resize(k);
fill(dist+1, dist+n+1, INF);
fill(visited+1, visited+n+1, 0);
queue<ll> q;
for (ll i=0; i<k; ++i) {input(sheep[i]); q.push(sheep[i]); dist[sheep[i]] = 0;}
while (q.size())
{
ll u = q.front();
q.pop();
if (visited[u]) continue;
visited[u] = true;
for (ll x: adj[u]) if (dist[x] > dist[u] + 1) {dist[x] = dist[u] + 1; q.push(x);}
}
fill(dp+1, dp+n+1, -INF);
depth[1] = 0;
dfs(-1, 1);
fill(done+1, done+n+1, 0);
sort(sheep.begin(), sheep.end(), compare);
for (ll u: sheep)
{
if (done[u]) continue;
show(u);
mark(save[u]);
ans.pb(save[u]);
}
print(ll(ans.size()), '\n');
for (ll u: ans) print(u, ' ');
return 0;
}
Compilation message (stderr)
pastiri.cpp: In function 'int main()':
pastiri.cpp:13:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define input2(x, y) scanf("%lld%lld", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
pastiri.cpp:70:2: note: in expansion of macro 'input2'
70 | input2(n, k);
| ^~~~~~
pastiri.cpp:13:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define input2(x, y) scanf("%lld%lld", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
pastiri.cpp:73:3: note: in expansion of macro 'input2'
73 | input2(a, b);
| ^~~~~~
pastiri.cpp:12:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define input(x) scanf("%lld", &x);
| ~~~~~^~~~~~~~~~~~
pastiri.cpp:81:26: note: in expansion of macro 'input'
81 | for (ll i=0; i<k; ++i) {input(sheep[i]); q.push(sheep[i]); dist[sheep[i]] = 0;}
| ^~~~~
# | 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... |