#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
#define ll int
#define endl '\n'
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int lg = 19;
vector<pair<ll, ll>> node[N];
ll dp[N][20], dep[N];
long long dis[N];
ll sz[N];
ll par[N];
long long ans[N];
bool ok[N], in[N];
void dfs(ll u, ll p){
for (int i = 1; i <= lg; i++) dp[u][i] = dp[dp[u][i - 1]][i - 1];
for (auto j : node[u]){
if (j.first == p) continue;
dp[j.first][0] = u;
dis[j.first] = dis[u] + j.second;
dep[j.first] = dep[u] + 1;
dfs(j.first, u);
}
}
ll findnewCT(ll u, ll p, ll S){
for (auto j : node[u]){
if (j.first == p || in[j.first]) continue;
if ((sz[j.first] * 2) >= S) return findnewCT(j.first, u, S);
}
return u;
}
void initsz(ll u, ll p){
sz[u] = 1;
for (auto j : node[u]){
if (j.first == p || in[j.first]) continue;
initsz(j.first, u);
sz[u] += sz[j.first];
}
}
ll findCT(ll u, ll S){
initsz(u, u);
u = findnewCT(u, u, S);
in[u] = 1;
for (auto j : node[u]){
if (in[j.first]) continue;
if (sz[j.first] > sz[u]){
ll x = findCT(j.first, S - sz[u]);
par[x] = u;
} else {
ll x = findCT(j.first, sz[j.first]);
par[x] = u;
}
}
return u;
}
ll LCA(ll u, ll v){
if (dep[u] > dep[v]) swap(u, v);
ll dist = dep[v] - dep[u];
for (int i = lg; i >= 0; i--){
if (dist & (1 << i)) v = dp[v][i];
}
if (u == v) return u;
for (int i = lg; i >= 0; i--){
if (dp[u][i] != dp[v][i]){
u = dp[u][i];
v = dp[v][i];
}
}
return dp[u][0];
}
long long dist(ll u, ll v){
ll lca = LCA(u, v);
return dis[u] + dis[v] - 2 * dis[lca];
}
//vector<ll> recover;
void upd(ll u, ll root){
//if (!ok[u]) recover.push_back(u), ok[u] = 1;
if (u == 0) return;
ans[u] = min(ans[u], dist(root, u));
upd(par[u], root);
}
long long get(ll u, ll root){
if (u == 0) return 1e16;
return min(ans[u] + dist(u, root), get(par[u], root));
}
long long Query(int S, int X[], int T, int Y[]){
//recover.clear();
for (int i = 1; i <= T; i++){
upd(Y[i - 1] + 1, Y[i - 1] + 1);
}
long long res = 1e18;
for (int i = 1; i <= S; i++){
res = min(res, get(X[i - 1] + 1, X[i - 1] + 1));
}
//for (auto j : recover) ans[j] = 1e18, ok[j] = 0;
for (int i = 1; i <= T; i++){
ll now = Y[i - 1] + 1;
while (now){
ans[now] = 1e18;
now = par[now];
}
}
return res;
}
void Init(int n, int A[], int B[], int D[]){
for (int i = 1; i < n; i++){
node[A[i - 1] + 1].push_back({B[i - 1] + 1, D[i - 1]});
node[B[i - 1] + 1].push_back({A[i - 1] + 1, D[i - 1]});
}
dfs(1, 1);
ll root = findCT(1, n);
for (int i = 1; i <= n; i++) ans[i] = 1e18;
}
/*
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("VietCT.INP", "r")){
freopen("VietCT.INP", "r", stdin);
freopen("VietCT.OUT", "w", stdout);
}
}
*/
/*
*/
Compilation message
factories.cpp: In function 'void Init(int, int*, int*, int*)':
factories.cpp:115:12: warning: unused variable 'root' [-Wunused-variable]
115 | ll root = findCT(1, n);
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
24192 KB |
Output is correct |
2 |
Correct |
1068 ms |
32632 KB |
Output is correct |
3 |
Correct |
1885 ms |
32880 KB |
Output is correct |
4 |
Correct |
1801 ms |
32824 KB |
Output is correct |
5 |
Correct |
1993 ms |
33060 KB |
Output is correct |
6 |
Correct |
451 ms |
32760 KB |
Output is correct |
7 |
Correct |
1795 ms |
32880 KB |
Output is correct |
8 |
Correct |
1918 ms |
32860 KB |
Output is correct |
9 |
Correct |
2008 ms |
33032 KB |
Output is correct |
10 |
Correct |
442 ms |
32756 KB |
Output is correct |
11 |
Correct |
1816 ms |
32896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
24008 KB |
Output is correct |
2 |
Correct |
3782 ms |
108860 KB |
Output is correct |
3 |
Execution timed out |
8012 ms |
109920 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
24192 KB |
Output is correct |
2 |
Correct |
1068 ms |
32632 KB |
Output is correct |
3 |
Correct |
1885 ms |
32880 KB |
Output is correct |
4 |
Correct |
1801 ms |
32824 KB |
Output is correct |
5 |
Correct |
1993 ms |
33060 KB |
Output is correct |
6 |
Correct |
451 ms |
32760 KB |
Output is correct |
7 |
Correct |
1795 ms |
32880 KB |
Output is correct |
8 |
Correct |
1918 ms |
32860 KB |
Output is correct |
9 |
Correct |
2008 ms |
33032 KB |
Output is correct |
10 |
Correct |
442 ms |
32756 KB |
Output is correct |
11 |
Correct |
1816 ms |
32896 KB |
Output is correct |
12 |
Correct |
20 ms |
24008 KB |
Output is correct |
13 |
Correct |
3782 ms |
108860 KB |
Output is correct |
14 |
Execution timed out |
8012 ms |
109920 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |