이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5+5, ml = 17;
int n, m, k, tin[mx], cnt, up[ml][mx], depth[mx], weight[mx];
vector<pair<int, int>> adj[mx]; vector<int> ans;
void dfs(int node){
tin[node] = cnt++;
for (int l = 1; l < ml; l++) up[l][node] = up[l-1][up[l-1][node]];
for (pair<int, int> nxt : adj[node])
if (nxt.first != up[0][node])
depth[nxt.first] = depth[node]+1, up[0][nxt.first] = node, dfs(nxt.first);
}
int jmp(int node, int d){
for (int l = 0; l < ml; l++)
if ((d>>l)%2 == 1)
node = up[l][node];
return node;
}
int getLCA(int a, int b){
if (depth[a] < depth[b]) swap(a, b);
a = jmp(a, depth[a]-depth[b]); if (a == b) return a;
for (int l = ml-1; l >= 0; l--)
if (up[l][a] != up[l][b])
a = up[l][a], b = up[l][b];
return up[0][a];
}
int solve(int node){
int ret = weight[node];
for (pair<int, int> nxt : adj[node])
if (nxt.first != up[0][node]){
int val = solve(nxt.first); ret += val;
if (val >= k) ans.push_back(nxt.second);
}
return ret;
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i < n; i++){
int a, b; cin >> a >> b;
adj[a].push_back({b, i}); adj[b].push_back({a, i});
}dfs(1);
for (int i = 0; i < m; i++){
int s; cin >> s; int cn[s+1];
for (int j = 0; j < s; j++) cin >> cn[j];
sort(cn, cn+s, [](int a, int b) { return tin[a] < tin[b]; } ); cn[s] = cn[0];
for (int j = 0; j < s; j++){
int lca = getLCA(cn[j], cn[j+1]);
weight[lca]--; weight[cn[j+1]]++;
}
}solve(1); sort(ans.begin(), ans.end());
cout<<ans.size()<<endl; for (int res : ans) cout<<res<<" ";
}
| # | 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... |