// Author: caption_mingle
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 1e5 + 7;
int n, m, k, h[N], up[N][20];
int timer, in[N], out[N], cnt[N], id[N];
pair<int, int> edge[N];
vector<int> g[N];
void dfs(int u, int p) {
in[u] = ++timer;
for(int i = 1; i <= 16; i++) up[u][i] = up[up[u][i - 1]][i - 1];
for(int v : g[u]) {
if(v == p) continue;
h[v] = h[u] + 1;
up[v][0] = u;
dfs(v, u);
}
out[u] = timer;
}
int lca(int u, int v) {
if(h[u] < h[v]) swap(u, v);
int k = h[u] - h[v];
for(int i = 0; (1 << i) <= k; i++) {
if(k >> i & 1) u = up[u][i];
}
if(u == v) return u;
for(int i = 16; i >= 0; i--) {
if(up[u][i] != up[v][i]) {
u = up[u][i];
v = up[v][i];
}
}
return up[u][0];
}
bool inside(int u, int v) {
return in[u] <= in[v] and out[v] <= out[u];
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
cin >> n >> m >> k;
for(int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
edge[i] = {u, v};
}
dfs(1, 0);
for(int i = 1; i <= m; i++) {
int sz; cin >> sz;
vector<int> vec;
for(int j = 1; j <= sz; j++) {
int x; cin >> x;
vec.pb(x);
}
for(int j = 1; j < sz; j++) {
vec.pb(lca(vec[j], vec[j - 1]));
}
sort(all(vec), [&](int x, int y) {
return in[x] < in[y];
});
vec.erase(unique(all(vec)), vec.end());
vector<int> st;
for(int u : vec) {
while(sz(st) and !inside(st.back(), u)) st.pop_back();
if(sz(st)) {
cnt[st.back()]--;
// cerr << st.back() << ' ' << u << ln;
cnt[u]++;
}
st.pb(u);
}
}
for(int i = 1; i < n; i++) {
auto& [u, v] = edge[i];
if(h[v] < h[u]) swap(u, v);
// cerr << "EDGE " << u << ' ' << v << ' ' << i << ln;
id[v] = i;
}
vector<int> vec;
for(int i = 1; i <= n; i++) vec.pb(i);
sort(all(vec), [&](int x, int y) {
return h[x] > h[y];
});
vector<int> ans;
for(int x : vec) {
cnt[up[x][0]] += cnt[x];
// cerr << x << ' ' << cnt[x] << ln;
if(cnt[x] >= k) ans.pb(id[x]);
}
cout << sz(ans) << ln;
sort(all(ans));
for(int x : ans) cout << x << ' ';
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
railway.cpp: In function 'int main()':
railway.cpp:57:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | freopen(task ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:58:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | freopen(task ".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |