이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define endl '\n'
#define pl(var) " [" << #var << ": " << (var) << "] "
#define ll long long
vector<vector<pair<int, int>>> adj;
multiset<int> token[100'010];
vector<int> s;
int k;
vector<int> ans;
vector<int> votes;
void merge(int u, int v) {
if (token[u].size() < token[v].size()) {
swap(token[u], token[v]);
swap(votes[u], votes[v]);
}
for (int elt : token[v]) {
token[u].insert(elt);
int cnt = token[u].count(elt);
if (cnt == s[elt]) {
votes[u]--;
} else if (cnt == 1) {
votes[u]++;
}
}
}
void dfs(int u, int par, int idx) {
for (auto [v, _idx] : adj[u]) {
if (v == par) continue;
dfs(v, u, _idx);
}
if (idx == -1) return;
for (auto [v, _] : adj[u]) {
if (v == par) continue;
merge(u, v);
}
if (votes[u] >= k) {
ans.push_back(idx);
}
}
void solve() {
int n, m; cin >> n >> m >> k;
adj.resize(n);
votes.resize(n); // number of votes for the ith edge
s.resize(m);
for (int i = 0; i < n - 1; i++) {
int a, b; cin >> a >> b;
a--, b--;
adj[a].emplace_back(b, i);
adj[b].emplace_back(a, i);
}
for (int i = 0; i < m; i++) {
cin >> s[i];
assert(s[i] >= 1);
for (int j = 0; j < s[i]; j++) {
int a; cin >> a;
token[a - 1].insert(i);
votes[a - 1]++;
}
}
dfs(0, 0, -1);
cout << ans.size() << endl;
sort(all(ans));
for (int i : ans) cout << i+1 << ' ';
cout << endl;
}
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--)
solve();
}
# | 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... |