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;
#define int int64_t
vector<int> idx;
signed main() {
int n,m,k;cin>>n>>m>>k;
vector<vector<int>> g(n);
int hh=0;
map<array<int,2>,int> mp;
for (int i=1;i<n;i++) {
int a,b;cin>>a>>b;a--;b--;
g[a].push_back(b);
g[b].push_back(a);
mp[{a,b}] = hh++;
}
idx.assign(n, -1);
int cnt=0;
vector<vector<int>> lft(n, vector<int>(20, -1));
vector<int> d(n);
function<void(int,int)> dfs=[&](int node,int pv) {
idx[node]=cnt++;
for (int x:g[node]) {
if (x==pv)continue;
d[x]=d[node]+1;
lft[x][0]=node;
dfs(x,node);
}
};
dfs(0, -1);
for (int j=1;j<20;j++) {
for (int i=0;i<n;i++) {
lft[i][j]=lft[i][j-1];
if (lft[i][j] != -1) {
lft[i][j] = lft[lft[i][j]][j-1];
}
}
}
function<int(int,int)> jmp=[&](int a, int steps) -> int {
int c=0;
while (steps) {
if ((steps&1)==1) {
a=lft[a][c];
}
c++;steps>>=1;
}
return a;
};
function<int(int,int)> lca=[&](int a,int b) -> int {
if (d[a]<d[b])swap(a,b);
a=jmp(a,d[a]-d[b]);
if (a==b)return a;
for (int i=19;i>=0;i--) {
if (lft[a][i] != lft[b][i]) {
a=lft[a][i];
b=lft[b][i];
}
}
return lft[a][0];
};
vector<int> sm(n, 0);
for (int i=0;i<m;i++) {
int s;cin>>s;
vector<int> inp(s);
for (auto &x:inp){cin>>x;x--;}
// sort(inp.begin(),inp.end(), [](int a, int b) { return idx[a] < idx[b]; });
for (int j=0;j<s;j++) {
int n1=inp[j];
int n2=inp[(j+1)%s];
sm[n1]++;
sm[n2]++;
sm[lca(n1,n2)] -= 2;
}
}
function<void(int,int)> dfs2=[&](int node,int pv) {
for (int x:g[node]) {
if (x == pv) continue;
dfs2(x,node);
sm[node] += sm[x];
}
};
dfs2(0, -1);
vector<int> out;
for (int i=0;i<n;i++) {
if (sm[i] >= 2*k) {
int code=-1;
if (mp.find({i, lft[i][0]}) != mp.end()) {
code = mp[{i, lft[i][0]}];
}
else if (mp.find({lft[i][0], i}) != mp.end()) {
code = mp[{lft[i][0], i}];
}
if (code != -1) {
out.push_back(code);
}
else {
assert(0 == -1);
}
}
}
sort(out.begin(), out.end());
if (out.size() == 0) {
cout<<0<<"\n";
return 0;
}
vector<int> dis={out[0]};
for (int x:out) {
if (dis.back() != x)dis.push_back(x);
}
cout<<dis.size()<<"\n";
for (int x:dis) {
cout<<x+1<<" ";
}
cout<<"\n";
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |