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 ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 2e5 + 1;
vector <vector <int> > g(nmax);
vector <pii> edges;
int a[nmax][22];
int in[nmax], out[nmax];
int timer = 0;
void prepare_dfs(int v, int e = 0){
a[v][0] = e;
in[v] = timer++;
for(int j = 1; j <= 20; j++)
if(a[v][j - 1]) a[v][j] = a[a[v][j - 1]][j - 1];
for(int to : g[v]){
to = edges[to].f + edges[to].s - v;
if(to == e) continue;
prepare_dfs(to, v);
}
out[v] = timer++;
}
bool is_ancestor(int u, int v){
return (!u) || (in[u] < in[v] && out[u] > out[v]);
}
int lca(int x, int y){
if(is_ancestor(x, y))
return x;
if(is_ancestor(y, x))
return y;
for(int j = 20; j >= 0; j--){
if(is_ancestor(a[x][j], y)) continue;
x = a[x][j];
}
return a[x][0];
}
int cnt[nmax];
vector <int> ans;
int k;
void DFS(int v, int e = 0){
for(int to : g[v]){
int x = to + 1;
to = edges[to].f + edges[to].s - v;
if(to == e) {
continue;
}
DFS(to, v);
if(cnt[to] >= k) ans.pb(x);
cnt[v] += cnt[to];
}
}
int main(){
//ios_base::sync_with_stdio(false); cin.tie(0);
int n, m; cin >> n >> m >> k;
for(int i = 0; i < n - 1; i++){
int u, v; cin >> u >> v;
g[u].pb(i);
g[v].pb(i);
edges.pb({u, v});
}
prepare_dfs(1);
while(m--){
int l; cin >> l;
vector <pii> vec;
for(int i= 0; i < l; i++){
int x; cin >> x;
vec.pb({in[x], x});
}
sort(vec.begin(), vec.end());
for(int i = 0; i < l - 1; i++){
int x = vec[i].s, y = vec[i + 1].s;
int u = lca(x, y);
vec.pb({in[u], u});
}
sort(vec.begin(), vec.end());
stack <int> st;
st.push(vec[0].s);
for(int i = 1; i < vec.size(); i++){
int x = vec[i].s;
//cout << x << " ";
if(x == vec[i - 1].s) continue;
while(!st.empty()){
int v = st.top();
if(out[v] < out[x]) st.pop();
else break;
}
vec[i].f = st.top();
st.push(x);
}
// cout << "\n";
for(int i = 1; i < vec.size(); i++){
if(vec[i].s == vec[i - 1].s) continue;
int x = vec[i].f, y = vec[i].s;
cnt[x]--, cnt[y]++;
}
// cout <<"\n";
}
DFS(1);
cout << ans.size() << "\n";
sort(ans.begin(), ans.end());
for(int to : ans) cout << to << ' ';
return 0;
}
Compilation message (stderr)
railway.cpp: In function 'int main()':
railway.cpp:95:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i = 1; i < vec.size(); i++){
| ~~^~~~~~~~~~~~
railway.cpp:108:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for(int i = 1; i < vec.size(); i++){
| ~~^~~~~~~~~~~~
# | 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... |