#include <bits/stdc++.h>
using namespace std;
#define el "\n"
#define FOR(i,a,b) for(int i = (a), _b = (b); i <= _b; i++)
#define FORD(i,a,b) for(int i = (a), _b = (b); i >= _b; i--)
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define lg(x) __lg(x)
#define alla(a,n) a+1,a+n+1
#define ll long long
template <class T> bool maxi(T &x, T y) { if(x < y) { x = y ; return true ;} return false;}
template <class T> bool mini(T &x, T y) { if(x > y) { x = y ; return true ;} return false;}
const int N = 1e5 + 2;
int n, q, k;
vector<pair<int, int>> adj[N];
void inp()
{
cin >> n >> q >> k;
FOR(i, 1, n - 1) {
int x, y; cin >> x >> y;
adj[x].pb({y, i});
adj[y].pb({x, i});
}
}
/* Try your best
No regrets */
namespace subtask_1
{
int h[N], up[N][21];
int in[N], out[N], time = 0;
int ID[N];
void dfs(int u, int p)
{
in[u] = ++time;
up[u][0] = p;
FOR(j, 1, lg(n)) up[u][j] = up[up[u][j - 1]][j - 1];
for(pair<int, int> x : adj[u]) {
int v = x.first, id = x.second;
if(v == p) continue;
ID[v] = id;
h[v] = h[u] + 1;
dfs(v, u);
}
out[u] = time;
}
int LCA(int u, int v)
{
if(h[u] < h[v]) swap(u, v);
FORD(j, lg(n), 0) if(h[up[u][j]] >= h[v]) u = up[u][j];
if(u == v) return u;
FORD(j, lg(n), 0) if(up[u][j] != up[v][j]) {
u = up[u][j];
v = up[v][j];
}
return up[u][0];
}
vector<int> g[N];
int Val[N];
void update(int u, int v)
{
int p = LCA(u, v);
Val[u]++;
Val[v]++;
Val[p] -= 2;
}
void dfs_pre(int u, int p)
{
for(int v : g[u]) if(v != p) {
update(u, v);
dfs_pre(v, u);
}
}
void dfsAns(int u, int p)
{
for(pair<int, int> x : adj[u]) {
int v = x.first, id = x.second;
if(v == p) continue;
dfsAns(v, u);
Val[u] += Val[v];
}
}
void slv()
{
dfs(1, 1);
FOR(qry, 1, q) {
int x; cin >> x;
vector<int> V;
FOR(i, 1, x) {
int v; cin >> v;
V.pb(v);
}
sort(all(V),[](int x, int y){
return in[x] < in[y];
});
int sz = V.size();
for(int i = 0; i < sz - 1; i++) {
V.pb(LCA(V[i], V[i + 1]));
}
sort(all(V),[](int x, int y){
return in[x] < in[y];
});
V.resize(unique(all(V)) - V.begin());
deque<int> dq;
for(int v : V) g[v].clear();
// cout << "Queries " << qry << el;
for(int v : V) {
while(!dq.empty() && out[dq.back()] < in[v]) dq.pop_back();
if(dq.size()) {
// cout << dq.back() << " " << v << el;
g[dq.back()].pb(v);
g[v].pb(dq.back());
}
dq.push_back(v);
}
dfs_pre(V[0], 0);
}
dfsAns(1, 0);
int ans = 0;
vector<int> isAns;
FOR(i, 2, n) if(Val[i] >= k) {
isAns.pb(ID[i]);
}
sort(all(isAns));
cout << isAns.size() << el;
for(int x : isAns) cout << x << " ";
}
}
/* Code slowly, think carefully */
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define __Azul__ "task"
if(fopen(__Azul__".inp", "r")) {
freopen(__Azul__".inp", "r", stdin);
freopen(__Azul__".out", "w", stdout);
}
bool qs = 0;
int T = 1;
if(qs) cin >> T;
while(T--) {
inp();
subtask_1::slv();
}
cerr << "\nTime" << 0.001 * clock() << "s "; return 0;
}
Compilation message (stderr)
railway.cpp:156:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
156 | main()
| ^~~~
railway.cpp: In function 'int main()':
railway.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | freopen(__Azul__".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
163 | freopen(__Azul__".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... |