Task: | Company Queries II |
Sender: | paulschulte |
Submission time: | 2024-10-16 12:38:31 +0300 |
Language: | C++ (C++17) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.01 s | details |
#2 | ACCEPTED | 0.01 s | details |
#3 | ACCEPTED | 0.01 s | details |
#4 | ACCEPTED | 0.01 s | details |
#5 | ACCEPTED | 0.01 s | details |
#6 | ACCEPTED | 0.56 s | details |
#7 | ACCEPTED | 0.42 s | details |
#8 | ACCEPTED | 0.51 s | details |
#9 | ACCEPTED | 0.56 s | details |
#10 | ACCEPTED | 0.52 s | details |
#11 | ACCEPTED | 0.01 s | details |
#12 | ACCEPTED | 0.62 s | details |
Code
// ~/.vim/cpp_template.cpp#include <bits/stdc++.h>#include <iostream>#include <vector>#include <algorithm>#include <string>#include <limits>#define REP(i,a,b) for (int i = a; i < b; i++)// Type Aliases for 1D and 2D vectors with initialization#define vi(n, val) vector<int>(n, val) // 1D vector of ints with size n, initialized to val#define vll(n, val) vector<long long>(n, val) // 1D vector of long longs with size n, initialized to val#define ll long long#define vvi(n, m, val) vector<vector<int>>(n, vector<int>(m, val)) // 2D vector of ints (n x m), initialized to val#define vvll(n, m, val) vector<vector<long long>>(n, vector<long long>(m, val)) // 2D vector of long longs (n x m), initialized to val#define LL_INF std::numeric_limits<long long int>::max()#define INF std::numeric_limits<int>::max()using namespace std;template <typename T>void pV(const std::vector<T>& vec, const std::string& label = "Vector") {std::cout << label << ": [ ";for (const auto& elem : vec) {std::cout << elem << " ";}std::cout << "]" << std::endl;}void dfs(int s, vector<bool> *visited, vector<int> (*adj)[]) {if ((*visited)[s]) return;(*visited)[s] = true;// process node sfor (auto u: (*adj)[s]) {dfs(u, visited, adj);}}/*// DEPTH-FRIRST-SEARCHvector<int> adj[N];vector<bool> visited(N, false);int u, v;for(int i = 0; i < M;i++){cin >> u >> v;u--;v--;adj[u].push_back(v);adj[v].push_back(u);}*/vector<long long> dijkstra(int N, int x, vector<vector<pair<int,int>>> *adj){vector<bool> processed(N, false);vector<long long> distance(N, LL_INF);priority_queue<pair<long long,int>> q;//for (int i = 1; i <= n; i++) distance[i] = INF;distance[x] = 0;q.push({0,x});while (!q.empty()) {int a = q.top().second; q.pop();if (processed[a]) continue;processed[a] = true;for (auto u : (*adj)[a]) {int b = u.first, w = u.second;if (distance[a]+w < distance[b]) {distance[b] = distance[a]+w;q.push({-distance[b],b});}}}return distance;}/*DIJKSTRA//vector<vector<pair<int,int>>> adj(N, vector<pair<int, int>>(N));*/const int MAXN = 200005;const int LOG = 20;vector<int> adj[MAXN];int up[MAXN][LOG];int depth[MAXN];void dfs(int node, int parent) {up[node][0] = parent;for (int i = 1; i < LOG; i++) {if (up[node][i-1] != -1)up[node][i] = up[up[node][i-1]][i-1];elseup[node][i] = -1;}for (int neighbor : adj[node]) {if (neighbor != parent) {depth[neighbor] = depth[node] + 1;dfs(neighbor, node);}}}int lca(int a, int b) {if (depth[a] < depth[b])swap(a, b);for (int i = LOG - 1; i >= 0; i--) {if (depth[a] - (1 << i) >= depth[b])a = up[a][i];}if (a == b)return a;for (int i = LOG - 1; i >= 0; i--) {if (up[a][i] != up[b][i]) {a = up[a][i];b = up[b][i];}}return up[a][0];}int main() {ios::sync_with_stdio(0);cin.tie(0);// Your code starts hereint n, q;cin >> n >> q;for (int i = 2; i <= n; i++) {int b;cin >> b;adj[b].push_back(i);adj[i].push_back(b);}depth[1] = 0;dfs(1, -1);//querieswhile (q--) {int a, b;cin >> a >> b;cout << lca(a, b) << endl;}return 0;}
Test details
Test 1
Verdict: ACCEPTED
input |
---|
10 10 1 2 3 4 5 6 7 8 9 6 9 8 10 10 3 ... |
correct output |
---|
6 8 3 1 8 ... |
user output |
---|
6 8 3 1 8 ... |
Test 2
Verdict: ACCEPTED
input |
---|
10 10 1 1 1 1 1 1 1 1 1 1 7 3 4 4 1 ... |
correct output |
---|
1 1 1 1 1 ... |
user output |
---|
1 1 1 1 1 ... |
Test 3
Verdict: ACCEPTED
input |
---|
10 10 1 1 1 1 2 3 4 4 1 1 8 2 7 8 3 ... |
correct output |
---|
1 1 1 1 1 ... |
user output |
---|
1 1 1 1 1 ... |
Test 4
Verdict: ACCEPTED
input |
---|
10 10 1 1 3 1 2 2 5 3 9 7 2 7 6 3 9 ... |
correct output |
---|
2 2 3 1 1 ... |
user output |
---|
2 2 3 1 1 ... |
Test 5
Verdict: ACCEPTED
input |
---|
10 10 1 2 3 2 5 3 2 2 4 6 1 1 3 1 9 ... |
correct output |
---|
1 1 1 2 2 ... |
user output |
---|
1 1 1 2 2 ... |
Test 6
Verdict: ACCEPTED
input |
---|
200000 200000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
correct output |
---|
74862 8750 16237 72298 58111 ... |
user output |
---|
74862 8750 16237 72298 58111 ... Truncated |
Test 7
Verdict: ACCEPTED
input |
---|
200000 200000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
correct output |
---|
1 1 1 1 1 ... |
user output |
---|
1 1 1 1 1 ... Truncated |
Test 8
Verdict: ACCEPTED
input |
---|
200000 200000 1 2 1 2 3 2 1 6 3 1 10 12 13 4... |
correct output |
---|
1 2 2 2 1 ... |
user output |
---|
1 2 2 2 1 ... Truncated |
Test 9
Verdict: ACCEPTED
input |
---|
200000 200000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
correct output |
---|
2796 633 633 151 2690 ... |
user output |
---|
2796 633 633 151 2690 ... Truncated |
Test 10
Verdict: ACCEPTED
input |
---|
200000 200000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
correct output |
---|
365 73 103 365 216 ... |
user output |
---|
365 73 103 365 216 ... Truncated |
Test 11
Verdict: ACCEPTED
input |
---|
2 4 1 1 1 1 2 2 1 ... |
correct output |
---|
1 1 1 2 |
user output |
---|
1 1 1 2 |
Test 12
Verdict: ACCEPTED
input |
---|
200000 200000 1 1 2 3 4 5 6 7 8 9 10 11 12 1... |
correct output |
---|
27468 6353 27468 6353 6353 ... |
user output |
---|
27468 6353 27468 6353 6353 ... Truncated |