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 F first
#define S second
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 1e5 + 50;
int n, m, k, out[N], par[20][N], h[N], st[N], ft[N], t, p[N];
pii E[N];
vector<int> adj[N];
vector<int> vc;
void dfs(int u, int p) {
st[u] = ++t;
par[0][u] = p;
h[u] = h[p]+1;
for (int v:adj[u]) {
if (v == p) continue;
dfs(v, u);
}
ft[u] = t;
}
int Par(int u, int k) {
for (int i = 0; i < 20; i++) {
if (k>>i&1) u = par[i][u];
}
return u;
}
int LCA(int u, int v) {
if (h[u] < h[v]) swap(u, v);
u = Par(u, h[u]-h[v]);
if (u == v) return u;
for (int i = 19; i >= 0; i--) {
if (par[i][u] != par[i][v]) {
u = par[i][u];
v = par[i][v];
}
}
return par[0][u];
}
void dfs2(int u, int p) {
for (int v:adj[u]) {
if (v == p) continue;
dfs2(v, u);
out[u]+= out[v];
}
}
bool cmp(int u, int v) {
return st[u] < st[v];
}
void solve() {
cin >> n >> m >> k;
for (int i=1; i <= n-1; i++) {
int u,v;
cin >> u >> v;
E[i] = {u, v};
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1,0);
for (int k = 1; k < 20; k++) {
for (int u = 1; u <= n; u++) {
par[k][u] = par[k-1][par[k-1][u]];
}
}
while (m--) {
vc.clear();
int s;
cin >> s;
for (int i = 1; i <= s; i++) {
int u;
cin >> u;
vc.push_back(u);
}
sort(vc.begin(), vc.end(), cmp);
for (int i = 1; i < s; i++) {
vc.push_back(LCA(vc[i-1], vc[i]));
}
sort(vc.begin(), vc.end(), cmp);
vc.resize(unique(vc.begin(), vc.end()) - vc.begin());
p[vc[0]] = 0;
for (int i = 1; i < (int)vc.size(); i++) {
int u = vc[i];
p[u] = vc[i-1];
while (p[u] > 0 && ft[p[u]] < ft[u]) p[u] = p[p[u]];
out[u]++, out[p[u]]--;
}
}
dfs2(1, 0);
int ans = 0;
for (int i = 1; i <= n-1; i++) {
if (par[0][E[i].S] == E[i].F) swap(E[i].F, E[i].S);
if (out[E[i].F] >= k) ans++;
}
cout << ans << endl;
for (int i = 1; i <= n-1; i++) {
if (out[E[i].F] >= k) cout << i << ' ';
}
cout << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
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... |