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>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 1e5 + 1;
int n, m, k;
int num[maxN];
int depth[maxN];
int timedfs;
int bit[maxN];
int lift[maxN][20];
int child[maxN];
int val[maxN];
vector <pii> adj[maxN];
void pre_dfs(int u, int p){
depth[u] = depth[p] + 1;
lift[u][0] = p;
num[u] = ++timedfs;
for (int i = 1; i < 20; ++i){
lift[u][i] = lift[lift[u][i - 1]][i - 1];
}
for (const auto &i: adj[u]){
if (i.fi == p) continue;
pre_dfs(i.fi, u);
child[u] += child[i.fi] + 1;
}
}
vector <int> answ;
void dfs(int u, int p){
for (const auto &i: adj[u]){
if (i.fi == p) continue;
dfs(i.fi, u);
val[u] += val[i.fi];
if (val[i.fi] >= k){
answ.pb(i.se);
}
}
}
int lca(int u, int v){
if (depth[u] < depth[v]) swap(u, v);
int k = (depth[u] - depth[v]);
while (k){
int t = k & -k;
u = lift[u][__lg(t)];
k ^= t;
}
if (u == v) return u;
for (int i = 19; i >= 0; --i){
if (lift[u][i] != lift[v][i]){
u = lift[u][i];
v = lift[v][i];
}
}
return lift[u][0];
}
void update(int x, int val){
for (; x <= n; x += x & -x){
bit[x] += val;
}
}
int get(int x){
int ans = 0;
for (; x; x -= x & -x) ans += bit[x];
return ans;
}
void Init(){
cin >> n >> m >> k;
for (int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
adj[u].pb({v, i});
adj[v].pb({u, i});
}
pre_dfs(1, 0);
for (int i = 1; i <= m; ++i){
int S; cin >> S;
vector <int> v(S);
for (int &j: v) cin >> j;
sort(v.begin(), v.end(),[](const int &i, const int &j){
return depth[i] > depth[j];
});
int last = v[0];
++val[v[0]];
update(num[v[0]], 1);
for (int j = 1; j < S; ++j){
int u = v[j];
last = lca(last, u);
int t = get(num[u] + child[u]) - get(num[u]);
if (t){
update(num[v[j]], 1);
continue;
}
++val[u];
for (int i = 19; i >= 0; --i){
int anc = lift[u][i];
if (anc == 0) continue;
if (!(get(num[anc] + child[anc]) - get(num[anc]))){
u = anc;
}
}
u = lift[u][0];
--val[u];
update(num[v[j]], 1);
}
--val[last];
for (int j: v){
update(num[j], -1);
}
}
dfs(1, 0);
cout << answ.size() << "\n";
sort(answ.begin(), answ.end());
for (int i: answ) cout << i << " ";
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message (stderr)
railway.cpp: In function 'int main()':
railway.cpp:147:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |