#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define siz 200000/100
#define pii pair<int, int>
#define ll long long
#define int ll
int n;
vector<vector<int>> graph;
vector<vector<pii>> wGraph;
bool isCentroid [siz];
int centroidParent [siz];
int subtreeSize [siz];
int parent [siz];
vector<vector<int>> centroidGraph;
int nodeAssignedVal [siz];
int getSubtreeSizes(int curNode) {
int locNodes = 1;
subtreeSize[curNode] = 1;
for (int newNode : graph[curNode]) {
if (newNode != parent[curNode] and !isCentroid[newNode]) {
parent[newNode] = curNode;
locNodes += getSubtreeSizes(newNode);
subtreeSize[curNode] += subtreeSize[newNode];
}
}
return locNodes;
}
int getCentroidSub(int curNode, int& totalNodes) {
bool centroidOk = true;
int counter = totalNodes-1;
for (int newNode : graph[curNode]) {
if (isCentroid[newNode] or parent[curNode] == newNode) continue;
counter -= subtreeSize[newNode];
if (subtreeSize[newNode] > (totalNodes)/2) {
centroidOk = false;
break;
}
}
if ((parent[curNode] == -1 or counter <= (totalNodes)/2) and centroidOk) return curNode;
for (int newNode : graph[curNode]) {
if (newNode != parent[curNode] and !isCentroid[newNode]) {
int res = getCentroidSub(newNode, totalNodes);
if (res != -1) return res;
}
}
return -1;
}
int getCentroid(int root) {
parent[root] = -1;
int totalNodes = getSubtreeSizes(root);
int res = getCentroidSub(root, totalNodes);
return res;
}
int generateCentroidSubtree(int curNode) {
int centroidNode = getCentroid(curNode);
isCentroid[centroidNode] = true;
for (int newNode : graph[centroidNode]) {
if (!isCentroid[newNode]) {
centroidParent[generateCentroidSubtree(newNode)] = centroidNode;
}
}
return centroidNode;
}
int centroidDecomp() {
for (int i = 0; i < n; i++) {
isCentroid[i] = false;
centroidParent[i] = -1;
}
generateCentroidSubtree(0);
centroidGraph = vector<vector<int>>(n);
int root;
for (int i = 0; i < n; i++) {
if (centroidParent[i] != -1) centroidGraph[centroidParent[i]].push_back(i);
else root = i;
}
return root;
}
void dfs(unordered_map<int, unordered_map<int, int>>& perChild, int curNode, int parentNode, int subtreeNode, int forbiddenNode, int curDist, int edgesOnPath) {
if (curNode == forbiddenNode) return;
if (subtreeNode == -1 and parentNode != -1) subtreeNode = curNode;
if (subtreeNode != -1) {
if (perChild[subtreeNode].find(curDist) == perChild[subtreeNode].end()) perChild[subtreeNode][curDist] = n;
perChild[subtreeNode][curDist] = min(perChild[subtreeNode][curDist], edgesOnPath);
perChild[subtreeNode][0] = 0;
}
for (pii newEdge : wGraph[curNode]) {
if (newEdge.first == parentNode) continue;
dfs(perChild, newEdge.first, curNode, subtreeNode, forbiddenNode, curDist+newEdge.second, edgesOnPath+1);
}
}
int best_path(int32_t ln, int32_t kval, int32_t edges [siz-1][2], int32_t edgeLen [siz-1]) {
n = ln;
graph = vector<vector<int>>(n);
wGraph = vector<vector<pii>>(n);
for (int i = 0; i < n-1; i++) {
graph[edges[i][0]].push_back(edges[i][1]);
graph[edges[i][1]].push_back(edges[i][0]);
wGraph[edges[i][0]].push_back(pii(edges[i][1], edgeLen[i]));
wGraph[edges[i][1]].push_back(pii(edges[i][0], edgeLen[i]));
}
centroidDecomp();
int minEdgesInPath = n;
for (int i = 0; i < n; i++) {
unordered_map<int, unordered_map<int, int>> perChild;
dfs(perChild, i, -1, -1, centroidParent[i], 0, 0);
unordered_map<int, multiset<int>> s;
for (pair<int, unordered_map<int, int>> j : perChild) {
for (pii k : j.second) s[k.first].insert(k.second);
}
for (pair<int, unordered_map<int, int>> j : perChild) {
for (pii k : j.second) s[k.first].erase(s[k.first].find(k.second));
for (pii k : j.second) {
if (s.find(kval-k.first) != s.end() and !s[kval-k.first].empty()) {
minEdgesInPath = min(minEdgesInPath, (*(s[kval-k.first].begin()))+k.second);
}
}
for (pii k : j.second) s[k.first].insert(k.second);
}
}
if (minEdgesInPath == n) minEdgesInPath = -1;
return minEdgesInPath;
}
// int H[siz-1][2];
// int L[siz-1];
// int32_t main() {
// int N, K;
// cin >> N >> K;
// for (int i = 0; i < N-1; i++) cin >> H[i][0] >> H[i][1];
// for (int i = 0; i < N-1; i++) cin >> L[i];
// cout << best_path(N, K, H, L);
// }
Compilation message
race.cpp: In function 'long long int centroidDecomp()':
race.cpp:89:12: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
89 | return root;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
308 KB |
Output is correct |
3 |
Correct |
5 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
304 KB |
Output is correct |
8 |
Correct |
4 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
5 ms |
332 KB |
Output is correct |
12 |
Correct |
4 ms |
332 KB |
Output is correct |
13 |
Correct |
3 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
304 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
332 KB |
Output is correct |
18 |
Correct |
3 ms |
372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
308 KB |
Output is correct |
3 |
Correct |
5 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
304 KB |
Output is correct |
8 |
Correct |
4 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
5 ms |
332 KB |
Output is correct |
12 |
Correct |
4 ms |
332 KB |
Output is correct |
13 |
Correct |
3 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
304 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
332 KB |
Output is correct |
18 |
Correct |
3 ms |
372 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
50 ms |
636 KB |
Output is correct |
22 |
Correct |
162 ms |
784 KB |
Output is correct |
23 |
Correct |
168 ms |
784 KB |
Output is correct |
24 |
Correct |
107 ms |
716 KB |
Output is correct |
25 |
Correct |
97 ms |
912 KB |
Output is correct |
26 |
Correct |
101 ms |
788 KB |
Output is correct |
27 |
Correct |
21 ms |
460 KB |
Output is correct |
28 |
Correct |
98 ms |
784 KB |
Output is correct |
29 |
Correct |
94 ms |
716 KB |
Output is correct |
30 |
Correct |
96 ms |
796 KB |
Output is correct |
31 |
Correct |
97 ms |
780 KB |
Output is correct |
32 |
Correct |
98 ms |
780 KB |
Output is correct |
33 |
Correct |
158 ms |
788 KB |
Output is correct |
34 |
Correct |
273 ms |
836 KB |
Output is correct |
35 |
Correct |
255 ms |
764 KB |
Output is correct |
36 |
Correct |
217 ms |
716 KB |
Output is correct |
37 |
Correct |
213 ms |
716 KB |
Output is correct |
38 |
Correct |
272 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
308 KB |
Output is correct |
3 |
Correct |
5 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
304 KB |
Output is correct |
8 |
Correct |
4 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
5 ms |
332 KB |
Output is correct |
12 |
Correct |
4 ms |
332 KB |
Output is correct |
13 |
Correct |
3 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
304 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
332 KB |
Output is correct |
18 |
Correct |
3 ms |
372 KB |
Output is correct |
19 |
Runtime error |
71 ms |
31404 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
308 KB |
Output is correct |
3 |
Correct |
5 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
304 KB |
Output is correct |
8 |
Correct |
4 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
5 ms |
332 KB |
Output is correct |
12 |
Correct |
4 ms |
332 KB |
Output is correct |
13 |
Correct |
3 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
304 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
332 KB |
Output is correct |
18 |
Correct |
3 ms |
372 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
50 ms |
636 KB |
Output is correct |
22 |
Correct |
162 ms |
784 KB |
Output is correct |
23 |
Correct |
168 ms |
784 KB |
Output is correct |
24 |
Correct |
107 ms |
716 KB |
Output is correct |
25 |
Correct |
97 ms |
912 KB |
Output is correct |
26 |
Correct |
101 ms |
788 KB |
Output is correct |
27 |
Correct |
21 ms |
460 KB |
Output is correct |
28 |
Correct |
98 ms |
784 KB |
Output is correct |
29 |
Correct |
94 ms |
716 KB |
Output is correct |
30 |
Correct |
96 ms |
796 KB |
Output is correct |
31 |
Correct |
97 ms |
780 KB |
Output is correct |
32 |
Correct |
98 ms |
780 KB |
Output is correct |
33 |
Correct |
158 ms |
788 KB |
Output is correct |
34 |
Correct |
273 ms |
836 KB |
Output is correct |
35 |
Correct |
255 ms |
764 KB |
Output is correct |
36 |
Correct |
217 ms |
716 KB |
Output is correct |
37 |
Correct |
213 ms |
716 KB |
Output is correct |
38 |
Correct |
272 ms |
716 KB |
Output is correct |
39 |
Runtime error |
71 ms |
31404 KB |
Execution killed with signal 11 |
40 |
Halted |
0 ms |
0 KB |
- |