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 debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <long double, pii> muchie;
const ll NMAX = 100001;
const ll VMAX = 1001;
const ll INF = (1LL << 60);
const ll MOD = 998244353;
const ll BLOCK = 447;
const ll base = 31;
const ll nr_of_bits = 21;
vector <int> sol;
vector <pii> v[NMAX];
int dp[NMAX][nr_of_bits + 1];
int sum[NMAX];
pii timp[NMAX];
int stamp;
bool isParent(int a, int b){
return (timp[a].first <= timp[b].first && timp[b].second <= timp[a].second);
}
int LCA(int a, int b){
int r = a, pas = nr_of_bits;
while(pas >= 0){
int nxt = dp[r][pas];
if(nxt != 0 && !isParent(nxt, b))
r = nxt;
pas--;
}
if(!isParent(r, b))
r = dp[r][0];
return r;
}
void DFS(int node, int p){
dp[node][0] = p;
timp[node].first = ++stamp;
for(auto x : v[node]){
if(x.first == p) continue;
DFS(x.first, node);
}
timp[node].second = stamp;
}
int c[NMAX];
bool cmp(int a, int b){
return timp[a].first < timp[b].first;
}
int k;
void Solve(int node, int p){
for(auto x : v[node]){
if(x.first == p) continue;
Solve(x.first, node);
sum[node] += sum[x.first];
if(sum[x.first] >= k){
sol.push_back(x.second);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, i;
cin >> n >> m >> k;
for(i = 1; i < n; i++){
int a, b;
cin >> a >> b;
v[a].push_back({b, i});
v[b].push_back({a, i});
}
DFS(1, 0);
for(int j = 1; j <= nr_of_bits; j++){
for(i = 1; i <= n; i++){
dp[i][j] = dp[dp[i][j - 1]][j - 1];
}
}
for(i = 1; i <= m; i++){
int t;
cin >> t;
for(int j = 1; j <= t; j++) cin >> c[j];
vector <int> total, stiva;
sort(c + 1, c + t + 1, cmp);
total.push_back(c[t]);
for(int j = 1; j < t; j++){
total.push_back(c[j]);
total.push_back(LCA(c[j], c[j + 1]));
}
sort(total.begin(), total.end(), cmp);
for(int j = 0; j < total.size(); j++){
if(j > 0 && timp[total[j - 1]].first == timp[total[j]].first)
continue;
while(stiva.size() && timp[stiva.back()].second < timp[total[j]].first)
stiva.pop_back();
if(stiva.size()){
sum[stiva.back()]--;
sum[total[j]]++;
}
stiva.push_back(total[j]);
}
}
Solve(1, 0);
sort(sol.begin(), sol.end());
cout << sol.size() << "\n";
for(auto x : sol) cout << x << " ";
return 0;
}
Compilation message (stderr)
railway.cpp: In function 'int main()':
railway.cpp:101:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for(int j = 0; j < total.size(); j++){
| ~~^~~~~~~~~~~~~~
# | 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... |