이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 1e5, L = 17;
int n, m, k;
vector<int> g[N], ind[N], sol, llca[N];
int up[N], p[L][N], h[N];
set<int> qry[N];
void dfs(int u, int l, int d){
p[0][u] = l;
h[u] = d++;
for(int i = 0, v; i < (int)g[u].size(); i++){
v = g[u][i];
if(v == l) up[u] = ind[u][i];
else dfs(v, u, d);
}
}
int kth(int u, int k){
for(int i = L-1; i >= 0; i--)
if(k >= (1<<i))
k -= (1<<i), u = p[i][u];
return u;
}
int lca(int u, int v){
if(h[v] > h[u]) swap(u, v);
u = kth(u, h[u]-h[v]);
if(u == v) return u;
for(int i = L-1; i >= 0; i--)
if(p[i][u] != p[i][v])
u = p[i][u], v = p[i][v];
return p[0][u];
}
void solve(int u){
for(int v : g[u]){
if(v == p[0][u]) continue;
solve(v);
if(qry[v].size() > qry[u].size()) swap(qry[u], qry[v]);
qry[u].insert(qry[v].begin(), qry[v].end());
}
for(int &x : llca[u]) qry[u].erase(x);
if((int)qry[u].size() >= k) sol.push_back(up[u]);
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n >> m >> k;
for(int i = 1, u, v; i < n; i++){
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
ind[u].push_back(i);
ind[v].push_back(i);
}
dfs(0, 0, 0);
for(int i = 1; i < L; i++)
for(int j = 0; j < n; j++)
p[i][j] = p[i-1][p[i-1][j]];
while(m--){
int s, a, b;
cin >> s >> b;
b--;
qry[b].insert(m);
while(--s){
cin >> a;
a--;
b = lca(b, a);
qry[a].insert(m);
}
llca[b].push_back(m);
}
solve(0);
sort(sol.begin(), sol.end());
cout << sol.size() << "\n";
for(int &x : sol) cout << x << " ";
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... |