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;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
int pref[mxN], in[mxN],out[mxN];
struct DSU{
// Disjoint Set Union data structuur
// Alle operaties werken in alpha(n) (supersnel, bijna constant)
vector<int> sz,parent, highest;
int components;
DSU(int n) {
sz.assign(n,1);
components = n;
parent.resize(n);
iota(all(parent),0);
highest.resize(n);
iota(all(highest),0);
}
void link(int a, int b) {
components--;
if(sz[a]<sz[b]) {
swap(a,b);
}
sz[a] = max(sz[a],sz[b]+1);
parent[b] = a;
if(in[highest[a]]>in[highest[b]]) {
highest[a] = highest[b];
}
}
int high(int a){
// Geeft de hoogste (oudste node) in de set van a
return highest[find(a)];
}
bool unite(int a, int b) {
// voegt de sets van a en b samen.
int pa = find(a),pb = find(b);
if(pa!=pb) link(pa,pb);
return pa!=pb;
}
int find(int a) {
// vindt de representatieve node van de set van a
if(a==parent[a]) return a;
return parent[a] = find(parent[a]);
}
};
vector<pi> adj[mxN];
vi query[mxN];
int cnt = 0;
void dfs(int at=0, int from=-1) {
in[at] = cnt++;
for(auto [to,haha]: adj[at]) {
if(to==from) continue;
dfs(to,at);
}
out[at] = cnt-1;
}
bitset<mxN> visited;
void lcadfs(int at, int from, DSU& dsu) {
visited[at]=true;
for(auto other : query[at]) {
if(visited[other]) {
// Offline algoritme van: https://usaco.guide/CPH.pdf#page=181
int lca = dsu.high(other);
pref[lca]--;
}
}
for(auto [to,haha]: adj[at]) {
if(to==from) continue;
lcadfs(to,at,dsu);
}
// Nodig voor het offline LCA algoritme
dsu.unite(at,from);
}
int k;
vi ans;
void prefdfs(int at=0, int from=-1) {
for(auto [to,id]: adj[at]) {
if(to==from) continue;
prefdfs(to,at);
if(pref[to]>=k) {
ans.push_back(id+1);
}
pref[at]+=pref[to];
}
}
int main() {
int n,m; cin >> n >> m >> k;
for(int i=0;i<n-1;++i) {
int a,b; cin >> a >> b;
--a,--b;
adj[a].push_back({b,i});
adj[b].push_back({a,i});
}
dfs();
for(int official=0;official<m;++official) {
int s; cin >> s;
vi nes(s);
for(int& i: nes) {cin >>i; --i; if(s>1) pref[i]++;}
if(s<=1) continue;
sort(all(nes), [&](int a, int b){return in[a]>in[b];});
for(int i=1;i<s;++i) {
int a = nes[i], b = nes[i-1];
query[a].push_back(b);
query[b].push_back(a);
}
// last one double
int a = nes[s-1], b = nes[s-2];
query[a].push_back(b);
query[b].push_back(a);
}
DSU dsu(n);
lcadfs(0,0,dsu);
prefdfs();
sort(all(ans));
cout << ans.size() << '\n';
cout << ans << '\n';
}
Compilation message (stderr)
railway.cpp: In function 'void dfs(int, int)':
railway.cpp:61:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | for(auto [to,haha]: adj[at]) {
| ^
railway.cpp: In function 'void lcadfs(int, int, DSU&)':
railway.cpp:78:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
78 | for(auto [to,haha]: adj[at]) {
| ^
railway.cpp: In function 'void prefdfs(int, int)':
railway.cpp:90:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
90 | for(auto [to,id]: adj[at]) {
| ^
# | 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... |