제출 #785824

#제출 시각아이디문제언어결과실행 시간메모리
785824vjudge1Ball Machine (BOI13_ballmachine)C++17
25 / 100
1087 ms25748 KiB
#include <bits/stdc++.h> using namespace std; int n, q, root; vector<int> parent, depth, subtreeMin; vector<vector<int>> ancestor; vector<vector<int>> adjList; vector<pair<int, int>> queries; vector<bool> colour; int depthFirstSearchA(const int& u) { subtreeMin[u] = u; for (auto &v : adjList[u]) { depth[v] = depth[u] + 1; ancestor[v][0] = parent[v]; for (int b = 1; ancestor[v][b - 1] != -1; b++) ancestor[v][b] = ancestor[ancestor[v][b - 1]][b - 1]; subtreeMin[u] = min(depthFirstSearchA(v), subtreeMin[u]); } sort(adjList[u].begin(), adjList[u].end(), [&](const int& v1, const int& v2) -> bool { return subtreeMin[v1] < subtreeMin[v2]; }); return subtreeMin[u]; } bool queryTypeOne() { int d = -1; for (int u = 1; u <= n; u++) { if (!adjList[u].empty() && adjList[u].size() != 2) return false; if (adjList[u].empty()) { if (d == -1) d = depth[u]; else if (d != depth[u]) return false; } } return true; } bool queryTypeThree() { if (queries[0].first != 1) return false; for (int i = 1; i < q; i++) if (queries[i].first != 2) return false; return true; } int manualInsert(const int& u) { for (auto &v : adjList[u]) if (!colour[v]) return manualInsert(v); colour[u] = true; return u; } int manualRemove(int u) { int rollsCount = 0; while (u != root && colour[parent[u]]) u = parent[u], rollsCount++; colour[u] = false; return rollsCount; } int main() { // freopen("inp.txt", "r", stdin); // freopen("out.txt", "w", stdout); ios::sync_with_stdio(false); cin >> n >> q; adjList.resize(n + 1); parent.resize(n + 1); for (int u = 1; u <= n; u++) { cin >> parent[u]; if (parent[u] == 0) root = u; else adjList[parent[u]].push_back(u); } depth.resize(n + 1); subtreeMin.resize(n + 1); ancestor.resize(n + 1, vector<int>(20, -1)); depthFirstSearchA(root); queries.resize(q); for (int i = 0; i < q; i++) cin >> queries[i].first >> queries[i].second; colour.resize(n + 1); if (queryTypeOne()) { for (auto &query : queries) { int type = query.first, x = query.second; if (type == 1) { int u; while (x --> 0) u = manualInsert(root); cout << u << '\n'; } if (type == 2) cout << manualRemove(x) << '\n'; } return 0; } if (queryTypeThree()) { for (auto &query : queries) { int type = query.first, x = query.second; if (type == 1) { int u; while (x --> 0) u = manualInsert(root); cout << u << '\n'; } if (type == 2) { int v = x; for (int b = 19; b >= 0; b--) if (ancestor[v][b] != -1) if (colour[ancestor[v][b]]) v = ancestor[v][b]; cout << depth[x] - depth[v] << '\n'; colour[v] = false; } } return 0; } }

컴파일 시 표준 에러 (stderr) 메시지

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:142:30: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
  142 |                 cout << u << '\n';
      |                              ^~~~
ballmachine.cpp:121:30: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
  121 |                 cout << u << '\n';
      |                              ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...