# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
311690 | BeanZ | Railway (BOI17_railway) | C++14 | 279 ms | 54796 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// I_Love_LPL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 1e5 + 5;
const int lg = 17;
vector<pair<ll, ll>> node[N];
vector<ll> fuckoff[N];
vector<ll> ans;
ll dep[N], dp[N][18], up[N];
set<ll> st[N];
ll k;
ll LCA(ll u, ll v){
if (dep[u] > dep[v]) swap(u, v);
ll dis = dep[v] - dep[u];
for (int i = lg; i >= 0; i--){
if (dis & (1 << i)){
v = dp[v][i];
}
}
if (u == v) return u;
for (int i = lg; i >= 0; i--){
if (dp[u][i] != dp[v][i]){
v = dp[v][i];
u = dp[u][i];
}
}
return dp[u][0];
}
void dfs2(ll u, ll p){
for (auto j : node[u]){
if (j.first == p) continue;
dfs2(j.first, u);
if (st[u].size() < st[j.first].size()) swap(st[u], st[j.first]);
st[u].insert(st[j.first].begin(), st[j.first].end());
}
for (auto j : fuckoff[u]) st[u].erase(j);
if (st[u].size() >= k) ans.push_back(up[u]);
}
void dfs(ll u, ll p){
for (int i = 1; i <= lg; i++) dp[u][i] = dp[dp[u][i - 1]][i - 1];
for (auto j : node[u]){
if (j.first == p) continue;
dp[j.first][0] = u;
dep[j.first] = dep[u] + 1;
up[j.first] = j.second;
dfs(j.first, u);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("A.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll n, m;
cin >> n >> m >> k;
for (int i = 1; i < n; i++){
ll u, v;
cin >> u >> v;
node[u].push_back({v, i});
node[v].push_back({u, i});
}
dfs(1, 1);
for (int tc = 1; tc <= m; tc++){
ll s, lca;
cin >> s >> lca;
st[lca].insert(tc);
for (int i = 1; i < s; i++){
ll u;
cin >> u;
lca = LCA(lca, u);
st[u].insert(tc);
}
fuckoff[lca].push_back(tc);
}
dfs2(1, 1);
sort(ans.begin(), ans.end());
cout << ans.size() << endl;
for (auto j : ans) cout << j << " ";
}
/*
*/
Compilation message (stderr)
# | 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... |