이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const ll sz=1e5+5;
ll p[sz], lvl[sz], dp[sz][100], ln, n, m, k, c[sz];
vector<ll>g[sz];
set<ll>s;
map<pair<ll, ll>, ll>m1;
/*
{} []
*/
void dfs1(ll u, ll par)
{
lvl[u]=lvl[par]+1;
dp[u][0]=par;
for(int i=1; i<ln; i++){
dp[u][i]=dp[dp[u][i-1]][i-1];
}
for(auto v:g[u]){
if(v!=par){
p[v]=u;
dfs1(v, u);
}
}
}
ll lca(ll u, ll v)
{
if(lvl[u]<lvl[v]) swap(u, v);
ll dif=lvl[u]-lvl[v];
for(int i=0; i<ln; i++){
if(dif&(1<<i)){
u=dp[u][i];
}
}
if(u==v) return u;
for(int i=ln-1; i>=0; i--){
if(dp[u][i]!=dp[v][i]){
u=dp[u][i];
v=dp[v][i];
}
}
return dp[u][0];
}
void dfs2(ll u, ll par)
{
for(auto v:g[u]){
if(v!=par){
dfs2(v, u);
c[u]+=c[v];
}
}
if(c[u]>=k && u!=1){
s.insert(m1[{u, par}]);
}
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m>>k;
ln=(ceil)(log2(n));
for(int i=1; i<n; i++){
ll a, b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
m1[{a, b}]=i;
m1[{b, a}]=i;
}
dfs1(1, 0);
for(int i=1; i<=m; i++){
ll num;
cin>>num;
ll k[num+1];
for(int j=1; j<=num; j++){
cin>>k[j];
}
set<pair<ll, ll>>cur;
for(int j=2; j<=num; j++){
ll x=k[j-1], y=k[j];
ll l=lca(x, y);
c[x]++; c[y]++; c[l]-=2;
}
}
dfs2(1, 0);
cout<<s.size()<<endl;
for(auto i:s) cout<<i<<' ';
}
/*
{} []
6 1 2
1 3
2 3
3 4
6 4
4 5
2 3 2
*/
# | 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... |