#include <bits/stdc++.h>
#define vi vector<int>
#define ve vector
#define ll long long
#define vf vector<float>
#define vll vector<pair<ll,ll>>
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 2e5+5
using namespace std;
int MOD = 1e9+7;
const int nax = 5e5+5;
const int nax2 = 200+5;
typedef complex<int> Point;
using cd = complex<double>;
const double PI = acos(-1);
#include"factories.h"
vii adj[nax];
bool vis[nax];
int sb[nax], par[nax];
ve<ll> dst[nax];
int dfs(int u, int p, bool type, ll cur){
if(type){
dst[u].pb(cur);
}
sb[u] = 1;
for(auto x : adj[u]){
if(x.fi == p || vis[x.fi]) continue;
sb[u] += dfs(x.fi,u, type, cur+x.se);
}
return sb[u];
}
int getcent(int u, int p, int ms){
for(auto x : adj[u]){
if(vis[x.fi] || x.fi == p) continue;
if(sb[x.fi]*2 > ms) return getcent(x.fi,u,ms);
}
return u;
}
void centdec(int u, int p){
int c = getcent(u,u,dfs(u,u,0,0));
dfs(c,c,1,0);
vis[c] = 1, par[c] = p;
for(auto x : adj[c]){
if(vis[x.fi] || x .fi == p) continue;
centdec(x.fi, c);
}
}
ll ans[nax];
void Init(int n, int a[], int b[], int d[]){
for (int i = 0; i < n; ++i)
{
ans[i] = INF;
}
for (int i = 0; i < n-1; ++i)
{
adj[a[i]].pb({b[i], d[i]});
adj[b[i]].pb({a[i], d[i]});
}
centdec(0,-1);
for (int i = 0; i < n; ++i)
{
reverse(dst[i].begin(), dst[i].end());
}
}
long long Query(int s, int x[], int t, int y[]){
ll aff = INF;
for (int i = 0; i < s; ++i)
{
int cur = x[i], cnt = 0;
while(cur != -1){
ans[cur] = min(ans[cur], dst[x[i]][cnt]);
cur = par[cur], cnt++;
}
}
for (int i = 0; i < t; ++i)
{
int cur = y[i], cnt = 0;
while(cur != -1){
aff = min(aff , ans[cur] + dst[y[i]][cnt]);
cur = par[cur], cnt++;
}
}
for (int i = 0; i < s; ++i)
{
int cur = x[i];
while(cur != -1){
ans[cur] = INF;
cur = par[cur];
}
}
return aff;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24224 KB |
Output is correct |
2 |
Correct |
253 ms |
33904 KB |
Output is correct |
3 |
Correct |
275 ms |
33996 KB |
Output is correct |
4 |
Correct |
276 ms |
33996 KB |
Output is correct |
5 |
Correct |
292 ms |
34384 KB |
Output is correct |
6 |
Correct |
186 ms |
33540 KB |
Output is correct |
7 |
Correct |
288 ms |
33828 KB |
Output is correct |
8 |
Correct |
291 ms |
33736 KB |
Output is correct |
9 |
Correct |
295 ms |
34128 KB |
Output is correct |
10 |
Correct |
180 ms |
33232 KB |
Output is correct |
11 |
Correct |
283 ms |
33792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23904 KB |
Output is correct |
2 |
Correct |
2062 ms |
130408 KB |
Output is correct |
3 |
Correct |
3066 ms |
167008 KB |
Output is correct |
4 |
Correct |
686 ms |
80280 KB |
Output is correct |
5 |
Correct |
3824 ms |
221556 KB |
Output is correct |
6 |
Correct |
3067 ms |
167936 KB |
Output is correct |
7 |
Correct |
862 ms |
59596 KB |
Output is correct |
8 |
Correct |
313 ms |
48924 KB |
Output is correct |
9 |
Correct |
991 ms |
68024 KB |
Output is correct |
10 |
Correct |
858 ms |
60780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24224 KB |
Output is correct |
2 |
Correct |
253 ms |
33904 KB |
Output is correct |
3 |
Correct |
275 ms |
33996 KB |
Output is correct |
4 |
Correct |
276 ms |
33996 KB |
Output is correct |
5 |
Correct |
292 ms |
34384 KB |
Output is correct |
6 |
Correct |
186 ms |
33540 KB |
Output is correct |
7 |
Correct |
288 ms |
33828 KB |
Output is correct |
8 |
Correct |
291 ms |
33736 KB |
Output is correct |
9 |
Correct |
295 ms |
34128 KB |
Output is correct |
10 |
Correct |
180 ms |
33232 KB |
Output is correct |
11 |
Correct |
283 ms |
33792 KB |
Output is correct |
12 |
Correct |
15 ms |
23904 KB |
Output is correct |
13 |
Correct |
2062 ms |
130408 KB |
Output is correct |
14 |
Correct |
3066 ms |
167008 KB |
Output is correct |
15 |
Correct |
686 ms |
80280 KB |
Output is correct |
16 |
Correct |
3824 ms |
221556 KB |
Output is correct |
17 |
Correct |
3067 ms |
167936 KB |
Output is correct |
18 |
Correct |
862 ms |
59596 KB |
Output is correct |
19 |
Correct |
313 ms |
48924 KB |
Output is correct |
20 |
Correct |
991 ms |
68024 KB |
Output is correct |
21 |
Correct |
858 ms |
60780 KB |
Output is correct |
22 |
Correct |
2266 ms |
131040 KB |
Output is correct |
23 |
Correct |
2413 ms |
134576 KB |
Output is correct |
24 |
Correct |
3561 ms |
168280 KB |
Output is correct |
25 |
Correct |
3627 ms |
172000 KB |
Output is correct |
26 |
Correct |
3600 ms |
168968 KB |
Output is correct |
27 |
Correct |
4471 ms |
217940 KB |
Output is correct |
28 |
Correct |
809 ms |
84360 KB |
Output is correct |
29 |
Correct |
3675 ms |
168312 KB |
Output is correct |
30 |
Correct |
3848 ms |
167792 KB |
Output is correct |
31 |
Correct |
3708 ms |
168708 KB |
Output is correct |
32 |
Correct |
1046 ms |
68880 KB |
Output is correct |
33 |
Correct |
321 ms |
49264 KB |
Output is correct |
34 |
Correct |
635 ms |
55044 KB |
Output is correct |
35 |
Correct |
670 ms |
55456 KB |
Output is correct |
36 |
Correct |
950 ms |
58156 KB |
Output is correct |
37 |
Correct |
882 ms |
58168 KB |
Output is correct |