이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int n, m, k, curTime = 0, leftTime = 0;
vector<vector<pair<int, int> > > graph;
vector<int> reach, segmentTree, order, reachToPos, firstReach, leftReach, nodeValues, solution;
void dfs(int pos, int curParent){
reach[pos] = curTime++;
reachToPos[reach[pos]] = pos;
order.push_back(reach[pos]);
firstReach[pos] = order.size() - 1;
for (pair<int, int> x : graph[pos]){
if (x.first == curParent) continue;
dfs(x.first, pos);
order.push_back(reach[pos]);
}
leftReach[pos] = leftTime++;
}
void buildSegmentTree(int pos, int l, int r){
if (l == r) {
segmentTree[pos] = order[l];
return;
}
int mid = (l + r) / 2;
buildSegmentTree(pos * 2 + 1, l, mid);
buildSegmentTree(pos * 2 + 2, mid + 1, r);
segmentTree[pos] = min(segmentTree[pos * 2 + 1], segmentTree[pos * 2 + 2]);
}
int querySegmentTree(int pos, int curl, int curr, int tarl, int tarr){
//cout << curl << " " << curr << endl;
if (curl >= tarl && curr <= tarr){
return segmentTree[pos];
}
int mid = (curl + curr) / 2, response = INT_MAX;
if (mid >= tarl) {
response = min(response, querySegmentTree(pos * 2 + 1, curl, mid, tarl, tarr));
}
if (mid + 1 <= tarr){
response = min(response, querySegmentTree(pos * 2 + 2, mid + 1, curr, tarl, tarr));
}
return response;
}
int LCA(int a, int b){
if (firstReach[a] > firstReach[b]) swap(a, b);
return reachToPos[querySegmentTree(0, 0, order.size() - 1, firstReach[a], firstReach[b])];
}
bool cmp (int a, int b){
return leftReach[a] < leftReach[b];
}
int unite(vector<int> & neighbourhoods, int a, int b, int bIndex, int abLCA){
//cout << a << " " << b << endl;
int subLCA = LCA(b, neighbourhoods[bIndex + 1]);
while(bIndex < order.size() && leftReach[subLCA] < leftReach[abLCA]){
int nextIndex = unite(neighbourhoods, b, neighbourhoods[bIndex + 1], bIndex + 1, subLCA);
b = subLCA;
bIndex = nextIndex;
subLCA = LCA(b, neighbourhoods[bIndex + 1]);
}
nodeValues[a]++;
nodeValues[b]++;
nodeValues[abLCA] -= 2;
return bIndex;
}
void addNumbers(vector<int> & neighbourhoods){
// sorted by left values
sort(neighbourhoods.begin(), neighbourhoods.end(), cmp);
//for (int x : neighbourhoods) cout << x << " ";
//cout << endl;
int curLCA = neighbourhoods[0];
for (int i = 1; i < neighbourhoods.size(); i++){
int subLCA = LCA(curLCA, neighbourhoods[i]);
//cout << subLCA << endl;
i = unite(neighbourhoods, curLCA, neighbourhoods[i], i, subLCA);
curLCA = subLCA;
}
}
int sumDfs(int pos, int parent){
int sum = nodeValues[pos];
for (pair<int, int> x : graph[pos]){
if (x.first == parent) continue;
int subSum = sumDfs(x.first, pos);
//cout << x.second << subSum << endl;
if (subSum >= k) solution.push_back(x.second);
sum += subSum;
}
return sum;
}
int main(){
cin >> n >> m >> k;
graph.resize(n);
reach.resize(n);
reachToPos.resize(n);
firstReach.resize(n);
leftReach.resize(n);
nodeValues.assign(n, 0);
for (int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
a--, b--;
graph[a].push_back(make_pair(b, i));
graph[b].push_back(make_pair(a, i));
}
dfs(0, -1);
// for (int x : order) cout << x << " ";
// cout << endl;
// for (int x : firstReach) cout << x << " ";
// cout << endl;
// for (int x : reach) cout << x << " ";
// cout << endl;
// for (int x : reachToPos) cout << x << " ";
// cout << endl;
segmentTree.assign(order.size() * 4 + 1, 0);
buildSegmentTree(0, 0, order.size() - 1);
// for (int x : segmentTree) cout << x << " ";
// cout << endl;
// cout << LCA(1, 4) << endl;
// for (int x : leftReach) cout << x << " ";
// cout << endl;
for (int i = 0; i < m; i++){
int cnt;
cin >> cnt;
vector<int> neighbourhoods(cnt);
for (int & x : neighbourhoods) cin >> x, x--;
addNumbers(neighbourhoods);
// for (int x : nodeValues) cout << x << " ";
// cout << endl;
// cout << endl;
}
sumDfs(0, -1);
cout << solution.size() << endl;
sort(solution.begin(), solution.end());
for (int x : solution) cout << x + 1 << " ";
cout << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
railway.cpp: In function 'int unite(std::vector<int>&, int, int, int, int)':
railway.cpp:74:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | while(bIndex < order.size() && leftReach[subLCA] < leftReach[abLCA]){
| ~~~~~~~^~~~~~~~~~~~~~
railway.cpp: In function 'void addNumbers(std::vector<int>&)':
railway.cpp:98:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (int i = 1; i < neighbourhoods.size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |