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;
using ll = long long;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define file "input"
void setIO() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(file".in", "r")) {
freopen(file".in", "r", stdin);
freopen(file".out", "w", stdout);
}
}
const int N = 1e5+5, LOG = 17;
int n, m, k, add[N], par[N];
vector<pair<int, int>> e;
vector<int> graph[N], ret;
int cnt, st[N], en[N], anc[N][LOG];
void prep(int u, int p) {
st[u] = ++cnt;
anc[u][0] = p;
foru(i, 1, LOG-1) anc[u][i] = anc[anc[u][i-1]][i-1];
fore(i, graph[u]) {
int v = e[i].fi ^ e[i].se ^ u;
if (v == p) continue;
par[v] = i;
prep(v, u);
}
en[u] = cnt;
}
bool isAnc(int u, int v) {
return st[u] <= st[v] && st[v] <= en[u];
}
int lca(int u, int v) {
if (isAnc(u, v)) return u;
ford(j, LOG-1, 0) if (!isAnc(anc[u][j], v)) u = anc[u][j];
return anc[u][0];
}
int dfs(int u, int p) {
int sum = 0;
fore(i, graph[u]) {
int v = e[i].fi ^ e[i].se ^ u;
if (v == p) continue;
int value = dfs(v, u) + add[i];
if (value >= k) ret.push_back(i);
sum += value;
}
return sum;
}
int main() {
setIO();
cin >> n >> m >> k;
foru(i, 2, n) {
int u, v;
cin >> u >> v;
graph[u].push_back(sz(e));
graph[v].push_back(sz(e));
e.emplace_back(u, v);
}
par[1] = n+1;
en[0] = n+1;
prep(1, 0);
foru(i, 1, m) {
int k;
cin >> k;
vector<int> node;
foru(j, 1, k) {
int u;
cin >> u;
node.push_back(u);
}
sort(all(node), [&](int u, int v){
return st[u] < st[v];
});
int lst = node[0];
// cout << lst << "\n";
foru(j, 1, k-1) {
int cur = node[j];
lst = lca(lst, cur);
add[par[cur]] += 1;
add[par[lst]] -= 1;
// cout << cur << " " << lst << "\n";
lst = cur;
}
// cout << "-----------\n";
}
dfs(1, 0);
sort(all(ret));
cout << sz(ret) << "\n";
fore(x, ret) cout << x+1 << " ";
}
Compilation message (stderr)
railway.cpp: In function 'void setIO()':
railway.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen(file".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(file".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... |