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 rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b - 1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
const int INF = 1000000007;
int n, m, k, cnt[100001], curr, jmp[100001][18], edgenum[100001], depth[100001];
vi ans, add[100001], rem[100001];
vii graph[100001];
void build(int u, int p, int d) {
jmp[u][0] = p;
depth[u] = d++;
trav(edge, graph[u]) {
int v, num;
tie(v, num) = edge;
if (v != p) {
edgenum[v] = num;
build(v, u, d);
}
}
}
int lca(int u, int v) {
if (depth[v] < depth[u]) swap(u, v);
per(i, 0, 18) if ((1 << i) <= depth[v] - depth[u]) {
v = jmp[v][i];
}
if (u == v) return u;
per(i, 0, 18) if (jmp[u][i] != jmp[v][i]) {
u = jmp[u][i];
v = jmp[v][i];
}
return jmp[u][0];
}
umap<int, bitset<100001>> mp;
vii vec;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cin >> n >> m >> k;
int a, b;
rep(i, 0, n - 1) {
cin >> a >> b;
graph[--a].emplace_back(--b, i);
graph[b].emplace_back(a, i);
}
build(0, 0, 0);
rep(j, 1, 18) rep(i, 0, n) jmp[i][j] = jmp[jmp[i][j - 1]][j - 1];
rep(i, 0, m) {
int s, u, v;
cin >> s >> u;
add[--u].pb(i);
rep(j, 1, s) {
cin >> v;
add[--v].pb(i);
int cand = lca(u, v);
if (depth[cand] < depth[u]) u = cand;
}
rem[u].pb(i);
}
rep(i, 1, n) vec.emplace_back(depth[i], i);
sort(all(vec), greater<>());
bitset<100001> bs;
trav(neigh, vec) {
int u = neigh.second;
if (mp.count(u))
bs = mp[u];
else
bs.reset();
mp.erase(u);
int p = jmp[u][0];
trav(layer, add[u]) bs[layer] = 1;
trav(layer, rem[u]) bs[layer] = 0;
if (u && k <= (int)bs.count()) ans.pb(edgenum[u]);
if (p) {
if (mp.count(p))
mp[p] |= bs;
else
mp.emplace(p, bs);
}
}
sort(all(ans));
cout << sz(ans) << endl;
trav(edge, ans) cout << edge + 1 << ' ';
cout << endl;
return 0;
}
# | 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... |