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>
#pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii, int> ipii;
const int MAXN = 2e5+10;
const int MAXA = 2e3+10;
const int INF = 1e18+10;
const int LOG = 19;
const int MOD = 1e9+7;
const int SQRT = 450;
const vector<int> NOL = {};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
int n, m, k;
vector <int> adj[MAXN];
int dep[MAXN], anc[MAXN][LOG+5], in[MAXN], tim;
map<pii,int> edge;
void dfs(int nw, int par){
dep[nw] = dep[par]+1; in[nw] = ++tim;
anc[nw][0] = par;
for(auto nx : adj[nw]){
if(nx==par) continue;
dfs(nx, nw);
}
}
int LCA(int x, int y){
if(dep[x] < dep[y]) swap(x, y);
for(int i=LOG-1; i>=0; i--){
if(dep[anc[x][i]] >= dep[y])
x = anc[x][i];
}
if(x == y) return x;
for(int i=LOG-1; i>=0; i--){
if(anc[x][i] != anc[y][i]){
x = anc[x][i]; y = anc[y][i];
}
}
return anc[x][0];
}
int cnt[MAXN];
void OUT(int nw, int par){
for(auto nx : adj[nw]){
if(nx==par) continue;
OUT(nx, nw);
cnt[nw] += cnt[nx];
}
}
signed main(){
cin >> n >> m >> k;
for(int i=1; i<=n-1; i++){
int x, y; cin >> x >> y;
edge[pii(x, y)] = edge[pii(y,x)] = i;
adj[x].pb(y); adj[y].pb(x);
}
dfs(1, 0);
for(int j=1; j<LOG; j++)
for(int i=1; i<=n; i++)
anc[i][j] = anc[ anc[i][j-1] ][j-1];
for(int i=1; i<=m; i++){
int num; cin >> num;
vector <pii> inp;
for(int xx=1; xx<=num; xx++){
int x; cin >> x;
inp.pb({in[x], x});
}
sort(inp.begin(), inp.end());
vector <int> stac;
for(auto [in, x] : inp){
if(stac.size() < 2) stac.pb(x);
else {
int depba = dep[LCA(stac.back(), x)];
while(stac.size()>=2 &&
dep[ LCA(stac.back(), stac[stac.size()-2]) ] >= depba){
int p = stac.back(); stac.pop_back();
int q = stac.back(); stac.pop_back();
int lca = LCA(p, q);
// cout << "upd: " << p << ' ' << q << ' ' << lca << " p\n";
cnt[p]++; cnt[q]++; cnt[lca] -= 2;
stac.pb(lca);
}
stac.pb(x);
}
}
while(stac.size()>=2){
int p = stac.back(); stac.pop_back();
int q = stac.back(); stac.pop_back();
int lca = LCA(p, q);
// cout << "upd: " << p << ' ' << q << ' ' << lca << " p\n";
cnt[p]++; cnt[q]++; cnt[lca] -= 2;
stac.pb(lca);
}
}
// for(int i=1; i<=n; i++) cout << cnt[i] << " p\n";
OUT(1, 0);
vector <int> ANS;
for(int i=1; i<=n; i++){
if(cnt[i] >= k) ANS.pb(edge[pii(i, anc[i][0]) ]);
}
sort(ANS.begin(), ANS.end());
cout << ANS.size() << '\n';
for(auto in : ANS) cout << in << ' ';
cout << '\n';
}
Compilation message (stderr)
railway.cpp: In function 'int main()':
railway.cpp:84:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
84 | for(auto [in, x] : inp){
| ^
# | 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... |