이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
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);
}
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;
dfs(x,node);
sm[node] += sm[x];
}
};
dfs2(0, -1);
vector<int> out;
for (int i=0;i<n;i++) {
if (sm[i] >= 2*k) {
out.push_back(i);
out.push_back(lft[i][0]);
}
}
sort(out.begin(), out.end());
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... |