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>
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-Ofast")
#define N 500050
#define NN 1005000
#define PB push_back
#define endl '\n'
#define pri(x) cout << x << endl
#define _ << " " <<
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define M ll(1e9 + 7)
#define F first
#define S second
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef short int si;
typedef unsigned long long ull;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
//ordered_set se;
int n, k;
bool mk[N];
vector <int> g[N];
void solve_1()
{
vector <int> vr; vr.clear();
vector <int> ans; ans.clear();
int x;
for (int i = 1; i <= n; i++)
if (sz(g[i]) == 1)
x = i;
vector <int> pr; pr.clear();
while (1)
{
int nxt = -1;
for (auto it : g[x])
{
if (sz(vr) && it == vr.back()) continue;
nxt = it;
}
if (mk[x])
pr.PB(sz(vr));
vr.PB(x);
x = nxt;
if (x == -1)
break;
}
for (int i = 0; i < sz(pr); i++)
{
int nm = pr[i];
if (i + 1 == sz(pr))
{
ans.PB(vr[nm]);
continue;
}
int len = pr[i + 1] - nm;
if (len & 1)
{
ans.PB(vr[nm]);
continue;
}
else
{
len /= 2;
ans.PB(vr[nm + len]);
i++;
continue;
}
}
pri(sz(ans));
for (auto it : ans)
cout << it << " ";
cout << endl;
exit(0);
}
int main()
{
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
//
// freopen("1.out", "w", stdout);
cin >> n >> k;
bool ft = 1;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
if (x > y)
swap(x, y);
if (x + 1 != y)
ft = 0;
g[x].PB(y);
g[y].PB(x);
}
for (int i = 0; i < k; i++)
{
int x;
cin >> x;
mk[x] = 1;
}
if (ft)
solve_1();
}
# | 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... |