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 file ""
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define getbit(x, i) (((x) >> (i)) & 1)
#define bit(x) (1LL << (x))
#define popcount __builtin_popcountll
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
return l + rd() % (r - l + 1);
}
const int N = 1e5 + 5;
const int mod = (int)1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int oo = 1e9;
const long long ooo = 1e18;
template<class X, class Y> bool mini(X &a, Y b) {
return a > b ? (a = b, true) : false;
}
template<class X, class Y> bool maxi(X &a, Y b) {
return a < b ? (a = b, true) : false;
}
void add(int &a, int b) {
a += b;
if (a >= mod) a -= mod;
if (a < 0) a += mod;
}
int n, q, k;
vector<pair<int, int> > adj[N];
int fin[N];
int h[N], par[N][lg + 1];
int par_id[N];
int d[N];
vector<int> ans;
int timer = 0;
void dfs(int u, int p) {
fin[u] = ++timer;
par[u][0] = p;
for (int i = 1; i <= lg; ++i) par[u][i] = par[par[u][i - 1]][i - 1];
for (auto [v, id] : adj[u]) if (v != p) {
h[v] = h[u] + 1;
par_id[v] = id;
dfs(v, u);
}
}
int lca(int u, int v) {
if (h[u] < h[v]) swap(u, v);
for (int i = lg; i >= 0; --i) if (h[par[u][i]] >= h[v]) u = par[u][i];
if (u == v) return u;
for (int i = lg; i >= 0; --i) if (par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
return par[u][0];
}
void dfs_dis(int u, int p) {
for (auto [v, id] : adj[u]) if (v != p) {
dfs_dis(v, u);
d[u] += d[v];
if (d[v] >= 2 * k) ans.push_back(par_id[v]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n >> q >> k;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
adj[u].emplace_back(v, i);
adj[v].emplace_back(u, i);
}
h[0] = -1;
dfs(1, 0);
while (q--) {
vector<int> node;
int num;
cin >> num;
for (int i = 0; i < num; ++i) {
int x;
cin >> x;
node.push_back(x);
}
if (num == 1) continue;
sort(all(node), [&] (int x, int y) {
return fin[x] < fin[y];
});
node.push_back(node.front());
for (int i = 0; i < num; ++i) {
d[node[i]]++;
d[node[i + 1]]++;
d[lca(node[i], node[i + 1])] -= 2;
}
}
dfs_dis(1, 0);
sort(all(ans));
cout << (int) ans.size() << '\n';
for (int i : ans) cout << i << ' ';
return 0;
}
/*
6 3 2
1 3
2 3
3 4
6 4
4 5
4 1 3 2 5
2 6 3
2 3 2
2
2 3
*/
# | 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... |