//============================================================================
// Name : joi14p1.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Scan and Debug
void scan(){}
template<typename F, typename... R> void scan(F &f,R&... r){cin>>f;scan(r...);}
int di_; string dnms_, co_ = ",";
void debug_(){cout<<endl;}
template<typename F, typename... R> void debug_(F f, R... r){while(dnms_[di_] != ',')cout<<dnms_[di_++];di_++;cout<<": "<<f<<",";debug_(r...);}
#define debug(...) dnms_=#__VA_ARGS__+co_,di_=0,debug_(__VA_ARGS__)
const int MAX = 500001;
struct ed {
int v, w;
};
vector<ed> matrix[MAX];
const ll INF = 0x3f3f3f3f3f3f3f3f;
int N,
sz[MAX], par[MAX], lv[MAX];
bool blocked[MAX];
ll bestFor[MAX];
vector<ll> dis[MAX];
int gsz(int cur, int par) {
sz[cur] = 1;
for (ed adj : matrix[cur])
if ((adj.v ^ par) && !blocked[adj.v])
sz[cur] += gsz(adj.v, cur);
return sz[cur];
}
int gcentroid(int cur, int par, int half) {
for (ed adj : matrix[cur])
if ((adj.v ^ par) && !blocked[adj.v] && sz[adj.v] > half)
return gcentroid(adj.v, cur, half);
return cur;
}
void dfs(int cur, int par=-1, ll __dis=0) {
dis[cur].push_back(__dis);
for (ed adj : matrix[cur])
if ((adj.v ^ par) && !blocked[adj.v])
dfs(adj.v, cur, __dis + adj.w);
}
int nxt[MAX], tpar[MAX], nxtPtr = -1;
ll tdis[MAX];
int decomp(int cur=0, int __par=-1, int __lv=0) {
gsz(cur, __par);
if (sz[cur] == 1) {// If size is one, centroid is self
lv[cur] = __lv;
return cur;
}
int centroid = gcentroid(cur, __par, sz[cur] >> 1);
lv[centroid] = __lv;
blocked[centroid] = true;
dis[centroid].push_back(0);
tpar[centroid] = -1;
tdis[centroid] = 0;
nxt[++nxtPtr] = centroid;
while (nxtPtr >= 0) {
int cur = nxt[nxtPtr]; nxtPtr--;
for (ed adj : matrix[cur]) {
if ((adj.v ^ tpar[cur]) && !blocked[adj.v]) {
nxt[++nxtPtr] = adj.v;
tpar[adj.v] = cur;
tdis[adj.v] = tdis[cur] + adj.w;
dis[adj.v].push_back(tdis[adj.v]);
}
}
}
for (ed adj : matrix[centroid])
if (!blocked[adj.v])
par[decomp(adj.v, centroid, __lv + 1)] = centroid;
return centroid;
}
// Grader Functions
void Init(int N_, int A[], int B[], int D[]) {
N = N_;
for (int i = 0; i < N - 1; i++) {
matrix[A[i]].push_back({B[i], D[i]});
matrix[B[i]].push_back({A[i], D[i]});
}
// Init Centroid Tree
par[decomp()] = -1;
memset(bestFor, 0x3f, sizeof bestFor);
}
int back[20000001], backSz = 0;
long long Query(int S, int X[], int T, int Y[]) {
ll best = INF;
backSz = 0;
for (int i = 0; i < T; i++) {
for (int cur = Y[i], clv = lv[cur]; cur != -1; cur = par[cur], clv--) {
bestFor[cur] = min(bestFor[cur], dis[Y[i]][clv]), assert(cur != par[cur]);
back[backSz++] = cur;
}
}
for (int i = 0; i < S; i++)
for (int cur = X[i], clv = lv[cur]; cur != -1; cur = par[cur], clv--)
best = min(best, dis[X[i]][clv] + bestFor[cur]), assert(cur != par[cur]);
for (int i = 0; i < backSz; i++)
bestFor[back[i]] = INF;
return best;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
28280 KB |
Output is correct |
2 |
Correct |
512 ms |
37112 KB |
Output is correct |
3 |
Correct |
410 ms |
37240 KB |
Output is correct |
4 |
Correct |
403 ms |
37452 KB |
Output is correct |
5 |
Correct |
434 ms |
37560 KB |
Output is correct |
6 |
Correct |
294 ms |
36728 KB |
Output is correct |
7 |
Correct |
431 ms |
37176 KB |
Output is correct |
8 |
Correct |
403 ms |
37368 KB |
Output is correct |
9 |
Correct |
441 ms |
37624 KB |
Output is correct |
10 |
Correct |
265 ms |
36728 KB |
Output is correct |
11 |
Correct |
398 ms |
37208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
27896 KB |
Output is correct |
2 |
Correct |
3256 ms |
135944 KB |
Output is correct |
3 |
Correct |
5153 ms |
161780 KB |
Output is correct |
4 |
Correct |
1218 ms |
89624 KB |
Output is correct |
5 |
Correct |
6246 ms |
215516 KB |
Output is correct |
6 |
Correct |
5140 ms |
162896 KB |
Output is correct |
7 |
Correct |
1564 ms |
59768 KB |
Output is correct |
8 |
Correct |
555 ms |
49640 KB |
Output is correct |
9 |
Correct |
1638 ms |
62328 KB |
Output is correct |
10 |
Correct |
1520 ms |
60920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
28280 KB |
Output is correct |
2 |
Correct |
512 ms |
37112 KB |
Output is correct |
3 |
Correct |
410 ms |
37240 KB |
Output is correct |
4 |
Correct |
403 ms |
37452 KB |
Output is correct |
5 |
Correct |
434 ms |
37560 KB |
Output is correct |
6 |
Correct |
294 ms |
36728 KB |
Output is correct |
7 |
Correct |
431 ms |
37176 KB |
Output is correct |
8 |
Correct |
403 ms |
37368 KB |
Output is correct |
9 |
Correct |
441 ms |
37624 KB |
Output is correct |
10 |
Correct |
265 ms |
36728 KB |
Output is correct |
11 |
Correct |
398 ms |
37208 KB |
Output is correct |
12 |
Correct |
26 ms |
27896 KB |
Output is correct |
13 |
Correct |
3256 ms |
135944 KB |
Output is correct |
14 |
Correct |
5153 ms |
161780 KB |
Output is correct |
15 |
Correct |
1218 ms |
89624 KB |
Output is correct |
16 |
Correct |
6246 ms |
215516 KB |
Output is correct |
17 |
Correct |
5140 ms |
162896 KB |
Output is correct |
18 |
Correct |
1564 ms |
59768 KB |
Output is correct |
19 |
Correct |
555 ms |
49640 KB |
Output is correct |
20 |
Correct |
1638 ms |
62328 KB |
Output is correct |
21 |
Correct |
1520 ms |
60920 KB |
Output is correct |
22 |
Correct |
3923 ms |
138188 KB |
Output is correct |
23 |
Correct |
4228 ms |
140032 KB |
Output is correct |
24 |
Correct |
6330 ms |
166500 KB |
Output is correct |
25 |
Correct |
6483 ms |
170124 KB |
Output is correct |
26 |
Correct |
5744 ms |
164284 KB |
Output is correct |
27 |
Correct |
6956 ms |
218696 KB |
Output is correct |
28 |
Correct |
1576 ms |
93912 KB |
Output is correct |
29 |
Correct |
5804 ms |
163704 KB |
Output is correct |
30 |
Correct |
5671 ms |
163232 KB |
Output is correct |
31 |
Correct |
6186 ms |
163784 KB |
Output is correct |
32 |
Correct |
1674 ms |
65808 KB |
Output is correct |
33 |
Correct |
537 ms |
50056 KB |
Output is correct |
34 |
Correct |
972 ms |
54480 KB |
Output is correct |
35 |
Correct |
1177 ms |
54792 KB |
Output is correct |
36 |
Correct |
1491 ms |
57928 KB |
Output is correct |
37 |
Correct |
1446 ms |
57848 KB |
Output is correct |