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;
const int maxn = 100000;
int n, m, k;
vector<int> adj[maxn];
map<pair<int,int>, int> edges;
int sen_num[maxn]; // num people that senator i has
vector<int> senators[maxn]; // senators that node i is part of
map<int,int> mp[maxn];
int num_completed[maxn];
vector<int> ans;
int sz[maxn];
int subsz(int nd, int par) {
sz[nd] = 1;
for (auto i : adj[nd])
if (i != par)
sz[i] += subsz(i, nd);
return sz[nd];
}
void dfs(int nd, int par) {
// get from leaves
int maxch = -1, maxv = -1;
for (auto i : adj[nd])
if (i != par) {
dfs(i, nd);
if (maxch == -1 || sz[i] > maxv)
maxv = sz[i], maxch = i;
}
if (maxch != -1) {
num_completed[nd] = num_completed[maxch];
swap(mp[nd], mp[maxch]);
for (auto i : adj[nd])
if (i != par && i != maxch) {
// process pairs
for (auto j : mp[i]) {
if (mp[nd][j.first] < sen_num[j.first] && j.second < sen_num[j.first] &&
mp[nd][j.first] + j.second >= sen_num[j.first])
num_completed[nd]++;
mp[nd][j.first] += j.second;
}
num_completed[nd] += num_completed[i];
}
}
for (auto j : senators[nd]) {
mp[nd][j] = mp[nd][j] + 1;
if (mp[nd][j] == k)
num_completed[nd]++;
}
// process current
if (mp[nd].size() - num_completed[nd] >= k)
ans.push_back(edges[make_pair(nd, par)]);
}
signed main() {
// freopen("main.in", "r", stdin);
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);
adj[b].push_back(a);
edges[make_pair(a,b)] = i+1;
edges[make_pair(b,a)] = i+1;
}
for (int i = 0; i < m; i++) {
int x; cin >> x;
for (int j = 0; j < x; j++) {
int y; cin >> y; y--;
senators[y].push_back(i);
}
}
subsz(0, -1);
dfs(0, -1);
cout << ans.size() << endl;
for (auto i : ans)
cout << i << ' ';
cout << endl;
return 0;
}
Compilation message (stderr)
railway.cpp: In function 'void dfs(int, int)':
railway.cpp:63:43: warning: comparison of integer expressions of different signedness: 'std::map<int, int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
63 | if (mp[nd].size() - num_completed[nd] >= k)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
# | 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... |