#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN = 5e5 + 5;
const ll INF = 1e16;
#define f first
#define s second
ll centro[MAXN],sz[MAXN],ans,st[MAXN];
vector <pair<ll,ll>> g[MAXN];
vector <ll> h[MAXN];
bool vis[MAXN];
void dfs(ll nodo, ll p, ll height, ll caso) {
if (caso!=0) h[nodo].push_back(height);
sz[nodo] = 1;
for (auto it=g[nodo].begin();it!=g[nodo].end();it++) {
if (it->f!=p && vis[it->f]==false) {
dfs(it->f,nodo,height + it->s,caso);
sz[nodo] += sz[it->f];
}
}
return;
}
ll find_centro(ll nodo, ll p, ll tot) {
for (auto it=g[nodo].begin();it!=g[nodo].end();it++) {
if (it->f!=p && vis[it->f]==false && sz[it->f]*2>tot) {
return find_centro(it->f,nodo,tot);
}
}
return nodo;
}
void decomp(ll nodo, ll p, ll pre) {
nodo = find_centro(nodo,p,sz[nodo]);
vis[nodo] = true;
centro[nodo] = pre;
for (auto it=g[nodo].begin();it!=g[nodo].end();it++) {
if (vis[it->f]==false) {
dfs(it->f,nodo,it->s,1);
decomp(it->f,nodo,nodo);
}
}
return;
}
void limpia(ll nodo) {
while (nodo!=0) {
st[nodo] = INF;
nodo = centro[nodo];
}
return;
}
void update(ll nodo) {
ll aux = centro[nodo], cnt = 0;
if (!h[nodo].empty()) cnt = h[nodo].size()-1;
st[nodo] = 0;
while (aux!=0) {
st[aux] = min(h[nodo][cnt],st[aux]);
cnt--;
aux = centro[aux];
}
return;
}
void query_centro(ll nodo) {
ll aux = centro[nodo], cnt = 0;
if (!h[nodo].empty()) cnt = h[nodo].size()-1;
ans = min(ans,st[nodo]);
while (aux!=0) {
ans = min(ans,st[aux]+h[nodo][cnt]);
cnt--;
aux = centro[aux];
}
return;
}
void Init(int N, int A[], int B[], int D[]) {
for (int i=0;i<N-1;i++) {
g[A[i]+1].push_back({B[i]+1,D[i]});
g[B[i]+1].push_back({A[i]+1,D[i]});
}
dfs(1,0,0,0);
decomp(1,0,0);
for (int i=0;i<=N;i++) st[i] = INF;
return;
}
long long Query(int S, int X[], int T, int Y[]) {
if (S>T) {
swap(S,T);
swap(X,Y);
}
ans = INF;
for (int i=0;i<S;i++) update(X[i]+1);
for (int i=0;i<T;i++) query_centro(Y[i]+1);
for (int i=0;i<S;i++) limpia(X[i]+1);
return ans;
}
/*
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n,q,S,T;
cin>>n>>q;
int A[n-1],B[n-1],D[n-1];
for (int i=0;i<n-1;i++) {
cin>>A[i]>>B[i]>>D[i];
}
Init(n,A,B,D);
while (q--) {
cin>>S>>T;
int X[S],Y[T];
for (int i=0;i<S;i++) cin>>X[i];
for (int i=0;i<T;i++) cin>>Y[i];
cout<<Query(S,X,T,Y)<<'\n';
}
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24148 KB |
Output is correct |
2 |
Correct |
252 ms |
41996 KB |
Output is correct |
3 |
Correct |
288 ms |
42408 KB |
Output is correct |
4 |
Correct |
246 ms |
42260 KB |
Output is correct |
5 |
Correct |
288 ms |
42668 KB |
Output is correct |
6 |
Correct |
186 ms |
41840 KB |
Output is correct |
7 |
Correct |
291 ms |
42260 KB |
Output is correct |
8 |
Correct |
244 ms |
42316 KB |
Output is correct |
9 |
Correct |
299 ms |
42760 KB |
Output is correct |
10 |
Correct |
191 ms |
41852 KB |
Output is correct |
11 |
Correct |
282 ms |
42220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23892 KB |
Output is correct |
2 |
Correct |
1962 ms |
154796 KB |
Output is correct |
3 |
Correct |
2421 ms |
178616 KB |
Output is correct |
4 |
Correct |
619 ms |
106004 KB |
Output is correct |
5 |
Correct |
3000 ms |
245244 KB |
Output is correct |
6 |
Correct |
2462 ms |
180208 KB |
Output is correct |
7 |
Correct |
854 ms |
70924 KB |
Output is correct |
8 |
Correct |
315 ms |
59484 KB |
Output is correct |
9 |
Correct |
934 ms |
75396 KB |
Output is correct |
10 |
Correct |
843 ms |
72088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24148 KB |
Output is correct |
2 |
Correct |
252 ms |
41996 KB |
Output is correct |
3 |
Correct |
288 ms |
42408 KB |
Output is correct |
4 |
Correct |
246 ms |
42260 KB |
Output is correct |
5 |
Correct |
288 ms |
42668 KB |
Output is correct |
6 |
Correct |
186 ms |
41840 KB |
Output is correct |
7 |
Correct |
291 ms |
42260 KB |
Output is correct |
8 |
Correct |
244 ms |
42316 KB |
Output is correct |
9 |
Correct |
299 ms |
42760 KB |
Output is correct |
10 |
Correct |
191 ms |
41852 KB |
Output is correct |
11 |
Correct |
282 ms |
42220 KB |
Output is correct |
12 |
Correct |
13 ms |
23892 KB |
Output is correct |
13 |
Correct |
1962 ms |
154796 KB |
Output is correct |
14 |
Correct |
2421 ms |
178616 KB |
Output is correct |
15 |
Correct |
619 ms |
106004 KB |
Output is correct |
16 |
Correct |
3000 ms |
245244 KB |
Output is correct |
17 |
Correct |
2462 ms |
180208 KB |
Output is correct |
18 |
Correct |
854 ms |
70924 KB |
Output is correct |
19 |
Correct |
315 ms |
59484 KB |
Output is correct |
20 |
Correct |
934 ms |
75396 KB |
Output is correct |
21 |
Correct |
843 ms |
72088 KB |
Output is correct |
22 |
Correct |
2079 ms |
160516 KB |
Output is correct |
23 |
Correct |
2045 ms |
145552 KB |
Output is correct |
24 |
Correct |
2909 ms |
167036 KB |
Output is correct |
25 |
Correct |
2970 ms |
170836 KB |
Output is correct |
26 |
Correct |
3012 ms |
167816 KB |
Output is correct |
27 |
Correct |
3581 ms |
228544 KB |
Output is correct |
28 |
Correct |
784 ms |
96616 KB |
Output is correct |
29 |
Correct |
3035 ms |
167088 KB |
Output is correct |
30 |
Correct |
3060 ms |
166504 KB |
Output is correct |
31 |
Correct |
3037 ms |
167088 KB |
Output is correct |
32 |
Correct |
897 ms |
76492 KB |
Output is correct |
33 |
Correct |
328 ms |
59928 KB |
Output is correct |
34 |
Correct |
652 ms |
64356 KB |
Output is correct |
35 |
Correct |
766 ms |
64836 KB |
Output is correct |
36 |
Correct |
947 ms |
69020 KB |
Output is correct |
37 |
Correct |
968 ms |
69080 KB |
Output is correct |