이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5+5, ml = 17;
int n, m, k, tin[mx], tout[mx], cnt = 0, 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);
tout[node] = cnt;
}
bool isAnc(int a, int b){
return tin[a] <= tin[b] and tout[a] >= tout[b];
}
int lca(int a, int b){
if (isAnc(a, b)) return a;
if (isAnc(b, a)) return b;
for (int l = ml-1; l >= 0; l--)
if (up[l][a] != 0 and !isAnc(up[l][a], b))
a = up[l][a];
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++) weight[lca(cn[j], cn[j+1])]--, 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... |