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>
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
using namespace std;
const int N = 1e5 + 10, LG = 20;
int h[N], par[N][LG], k;
vector<pii> adj[N];
vector<int> del[N], ans;
set<int> s[N];
void dd (int v, int p = -1) {
par[v][0] = p;
if (p != -1) h[v] = h[p] + 1;
for (int i = 1; i < LG; i++)
if (par[v][i - 1] != -1)
par[v][i] = par[par[v][i - 1]][i - 1];
for (auto u: adj[v])
if (p != u.F)
dd(u.F, v);
}
int lca (int u, int v) {
if (h[u] < h[v]) swap(u, v);
for (int i = LG - 1; i >= 0; i--)
if (par[u][i] != -1 && h[par[u][i]] >= h[v])
u = par[u][i];
if (u == v) return u;
for (int i = LG - 1; i >= 0; i--)
if (par[u][i] != par[v][i])
u = par[u][i], v = par[v][i];
return par[u][0];
}
void dfs (int v, int p = -1) {
for (auto u: adj[v]) {
if (u.F != p) {
dfs(u.F, v);
if (s[u.F].size() >= k) {
ans.pb(u.S);
}
if (s[v].size() < s[u.F].size()) s[v].swap(s[u.F]);
for (auto uu: s[u.F]) s[v].insert(uu);
s[u.F].clear();
for (auto u: del[v]) s[v].erase(u);
}
}
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);
int n, m;
cin >> n >> m >> k;
memset(par, -1, sizeof par);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].pb({v, i});
adj[v].pb({u, i});
}
dd(1);
for (int i = 1; i <= m; i++) {
int x;
cin >> x;
vector<int> d(x);
for (int j = 0; j < x; j++)
cin >> d[j];
for (auto u: d) s[u].insert(i);
int l = lca(d[0], d[1]);
for (int i = 2; i < x; i++)
l = lca(l, d[i]);
del[l].pb(i);
}
dfs(1);
cout << ans.size() << '\n';
sort(ans.begin(), ans.end());
for (auto u: ans) cout << u << ' ';
cout << '\n';
return 0;
}
Compilation message (stderr)
railway.cpp: In function 'void dfs(int, int)':
railway.cpp:40:25: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
40 | if (s[u.F].size() >= k) {
| ~~~~~~~~~~~~~~^~~~
# | 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... |