Submission #2042818
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
struct CentroidPathDecomposition
{
struct Centroid
{
int ParIndex, ParDepth, Deep;
vector< int > node;
Centroid(int idx, int dep, int deep) : ParIndex(idx), ParDepth(dep), Deep(deep) {}
inline size_t size()
{
return (node.size());
}
inline int &operator[](int k)
{
return (node[k]);
}
inline pair< int, int > Up()
{
return (make_pair(ParIndex, ParDepth));
}
};
vector< vector< int > > graph;
vector< int > SubTreeSize, NextPath;
vector< int > TreeIndex, TreeDepth;
vector< Centroid > Centroids;
void BuildSubTreeSize()
{
stack< pair< int, int > > s;
s.emplace(0, -1);
while(!s.empty()) {
auto p = s.top();
s.pop();
if(~SubTreeSize[p.first]) {
NextPath[p.first] = -1;
for(auto &to : graph[p.first]) {
if(p.second == to) continue;
SubTreeSize[p.first] += SubTreeSize[to];
if(NextPath[p.first] == -1 || SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
NextPath[p.first] = to;
}
}
} else {
s.push(p);
SubTreeSize[p.first] = 1;
for(auto &to : graph[p.first]) {
if(p.second != to) s.emplace(to, p.first);
}
}
}
}
void BuildPath()
{
stack< pair< int, int > > s;
Centroids.emplace_back(-1, -1, 0);
s.emplace(0, -1);
TreeIndex[0] = 0;
while(!s.empty()) {
auto p = s.top();
s.pop();
TreeDepth[p.first] = (int) Centroids[TreeIndex[p.first]].size();
for(auto &to : graph[p.first]) {
if(p.second == to) continue;
if(to == NextPath[p.first]) { // Centroid-Path
TreeIndex[to] = TreeIndex[p.first];
} else { // Not Centroid-Path
TreeIndex[to] = (int) Centroids.size();
Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first], Centroids[TreeIndex[p.first]].Deep + 1);
}
s.emplace(to, p.first);
}
Centroids[TreeIndex[p.first]].node.emplace_back(p.first);
}
}
void AddEdge(int x, int y)
{
graph[x].push_back(y);
graph[y].push_back(x);
}
virtual void Build()
{
BuildSubTreeSize();
BuildPath();
}
inline size_t size()
{
return (Centroids.size());
}
inline pair< int, int > Information(int idx)
{
return (make_pair(TreeIndex[idx], TreeDepth[idx]));
}
inline Centroid &operator[](int k)
{
return (Centroids[k]);
}
inline int LCA(int a, int b)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
return (Centroids[TreeIdxA][TreeDepthA]);
}
inline virtual void query(int a, int b, const function< void(int, int, int) > &f)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
f(TreeIdxA, 0, TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
f(TreeIdxB, 0, TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
f(TreeIdxA, TreeDepthA, TreeDepthB + 1);
}
CentroidPathDecomposition(int SZ)
{
graph.resize(SZ);
SubTreeSize.assign(SZ, -1);
NextPath.resize(SZ);
TreeIndex.resize(SZ);
TreeDepth.resize(SZ);
}
};
struct TreeArray : CentroidPathDecomposition
{
TreeArray(int sz) : CentroidPathDecomposition(sz) {}
vector< int > index;
void Build()
{
CentroidPathDecomposition::Build();
int ptr = 0;
for(auto ¢roid : Centroids) {
index.emplace_back(ptr);
ptr += centroid.size();
}
}
inline int get(int a)
{
auto p = Information(a);
return (index[p.first] + p.second);
}
inline void query(int a, int b, const function< void(int, int) > &f)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
f(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
f(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
f(index[TreeIdxA] + TreeDepthA, index[TreeIdxA] + TreeDepthB + 1);
}
inline vector< tuple< int, int, bool > > get_path(int a, int b)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
vector< tuple< int, int, bool > > vs, ws;
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
vs.emplace_back(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1, false);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
ws.emplace_back(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1, true);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
bool rev = true;
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB), rev = false;
vs.emplace_back(index[TreeIdxA] + TreeDepthA, index[TreeIdxA] + TreeDepthB + 1, rev);
reverse(begin(ws), end(ws));
copy(begin(ws), end(ws), back_inserter(vs));
return (vs);
}
};
int main()
{
int N, Q, A[100000];
scanf("%d %d", &N, &Q);
for(int i = 0; i < Q; i++) {
scanf("%d", &A[i]);
}
TreeArray tree(N);
for(int i = 1; i < N; i++) {
int a, b;
scanf("%d %d", &a, &b);
tree.AddEdge(--a, --b);
}
tree.Build();
for(int i = 0; i < Q; i++) {
int x, v, w;
scanf("%d %d %d", &x, &v, &w);
--v, --w;
auto path = tree.get_path(v, w);
}
}
Submission Info
Submission Time |
|
Task |
J - MODクエリ |
User |
ei13333 |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
6694 Byte |
Status |
RE |
Exec Time |
144 ms |
Memory |
15984 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:224:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &Q);
^
./Main.cpp:226:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[i]);
^
./Main.cpp:231:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
^
./Main.cpp:237:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &x, &v, &w);
^
Judge Result
Set Name |
Small |
All |
Score / Max Score |
0 / 30 |
0 / 370 |
Status |
|
|
Set Name |
Test Cases |
Small |
00_sample_path_01.txt, 15_path_00.txt, 15_path_01.txt, 15_path_02.txt, 15_path_03.txt, 15_path_04.txt, 16_path_05.txt, 16_path_06.txt, 16_path_07.txt, 16_path_08.txt, 16_path_09.txt, 17_path_10.txt, 17_path_11.txt, 17_path_12.txt, 17_path_13.txt, 17_path_14.txt |
All |
00_sample_path_01.txt, 15_path_00.txt, 15_path_01.txt, 15_path_02.txt, 15_path_03.txt, 15_path_04.txt, 16_path_05.txt, 16_path_06.txt, 16_path_07.txt, 16_path_08.txt, 16_path_09.txt, 17_path_10.txt, 17_path_11.txt, 17_path_12.txt, 17_path_13.txt, 17_path_14.txt, 20_random_15.txt, 20_random_16.txt, 20_random_17.txt, 21_random_18.txt, 21_random_19.txt, 21_random_20.txt, 30_random_21.txt, 30_random_22.txt, 30_random_23.txt, 40_random_24.txt, 40_random_25.txt, 40_random_26.txt, 49_sample_02.txt, 61_test_27.txt, 61_test_28.txt, 61_test_29.txt, 63_star_30.txt, 63_star_31.txt, 63_star_32.txt, 64_three_33.txt, 64_three_34.txt, 64_three_35.txt |
Case Name |
Status |
Exec Time |
Memory |
00_sample_path_01.txt |
WA |
1 ms |
256 KB |
15_path_00.txt |
WA |
1 ms |
256 KB |
15_path_01.txt |
WA |
1 ms |
256 KB |
15_path_02.txt |
WA |
1 ms |
256 KB |
15_path_03.txt |
WA |
1 ms |
256 KB |
15_path_04.txt |
WA |
1 ms |
256 KB |
16_path_05.txt |
WA |
1 ms |
256 KB |
16_path_06.txt |
WA |
1 ms |
256 KB |
16_path_07.txt |
WA |
1 ms |
256 KB |
16_path_08.txt |
WA |
1 ms |
256 KB |
16_path_09.txt |
WA |
1 ms |
256 KB |
17_path_10.txt |
WA |
76 ms |
8448 KB |
17_path_11.txt |
WA |
76 ms |
8448 KB |
17_path_12.txt |
WA |
75 ms |
8448 KB |
17_path_13.txt |
WA |
75 ms |
8448 KB |
17_path_14.txt |
WA |
75 ms |
8448 KB |
20_random_15.txt |
WA |
1 ms |
256 KB |
20_random_16.txt |
WA |
1 ms |
256 KB |
20_random_17.txt |
RE |
99 ms |
256 KB |
21_random_18.txt |
WA |
1 ms |
256 KB |
21_random_19.txt |
WA |
1 ms |
256 KB |
21_random_20.txt |
WA |
1 ms |
256 KB |
30_random_21.txt |
WA |
1 ms |
256 KB |
30_random_22.txt |
WA |
1 ms |
256 KB |
30_random_23.txt |
WA |
1 ms |
256 KB |
40_random_24.txt |
WA |
141 ms |
11792 KB |
40_random_25.txt |
WA |
141 ms |
11792 KB |
40_random_26.txt |
WA |
144 ms |
11792 KB |
49_sample_02.txt |
RE |
96 ms |
256 KB |
61_test_27.txt |
WA |
98 ms |
11400 KB |
61_test_28.txt |
WA |
101 ms |
11392 KB |
61_test_29.txt |
WA |
104 ms |
12440 KB |
63_star_30.txt |
WA |
100 ms |
15984 KB |
63_star_31.txt |
WA |
104 ms |
15984 KB |
63_star_32.txt |
WA |
104 ms |
15984 KB |
64_three_33.txt |
WA |
88 ms |
8340 KB |
64_three_34.txt |
WA |
93 ms |
8280 KB |
64_three_35.txt |
WA |
95 ms |
8376 KB |