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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 100001;
const int K = 20;
int n, m, k, x;
vector<pair<int, int>> g[MAXN];
int cnt[MAXN], up[MAXN][K], d[MAXN], tin[MAXN], timer = 1;
void dfs(int u, int p = 0) {
d[u] = d[p] + 1;
tin[u] = timer++;
up[u][0] = p;
for (int i = 1; i < K; i++) {
up[u][i] = up[up[u][i - 1]][i - 1];
}
for (auto v : g[u]) {
if (v.first != p) {
dfs(v.first, u);
}
}
}
int lca(int a, int b) {
int i = 19;
if (d[a] < d[b]) swap(a, b);
while (i >= 0 && d[a] > d[b]) {
if (d[up[a][i]] >= d[b])a = up[a][i];
i--;
}
if (a == b) return a;
for (i = 19; i >= 0; i--) {
if (up[a][i] != up[b][i]) {
a = up[a][i];
b = up[b][i];
}
}
return up[a][0];
}
vector<int> ans;
int dfs2(int x, int p = 0, int id = 0) {
for (auto u : g[x]) {
if (u.first != p) cnt[x] += dfs2(u.first, x, u.second);
}
if (cnt[x] >= 2*k && id)ans.push_back(id);
return cnt[x];
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n - 1; i++) {
int a, b; cin >> a >> b;
g[a].push_back({ b,i });
g[b].push_back({ a,i });
}
dfs(1);
for (int i = 1; i <= m; i++) {
cin >> x;
vector<int> v(x);
for (int& i : v) cin >> i;
sort(v.begin(), v.end(), [](int a, int b) {return tin[a] < tin[b]; });
v.push_back(v[0]);
for (int i = 0; i < x; i++) {
int a = v[i], b = v[i+1];
int u = lca(a, b);
cnt[u] -= 2;
cnt[a]++;
cnt[b]++;
}
}
dfs2(1);
cout << ans.size() << '\n';
sort(ans.begin(), ans.end());
for (int i : ans)cout << i << ' ';
cout << "\n";
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... |