#include "factories.h"
#include <bits/stdc++.h>
//#define int long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
#define endl "\n"
#define pii pair<int,int>
using namespace std;
const int MAXN = 5e5+5;
const int mod7 = 1e9+7;
const long long inf = 1e15;
const int lg = 20;
int t = 1;
vector<vector<pii>> graf(MAXN);
vector<long long> rez(MAXN, inf);
int subtreesize[MAXN];
long long dist[MAXN];
int tin[MAXN];
int tout[MAXN];
int parents[MAXN];
int up[MAXN][lg];
bool wasCentoid[MAXN];
int root;
int n,q;
void dfs(int nod, int p, long long d)
{
dist[nod] = d;
tin[nod] = t++;
up[nod][0] = p;
for(auto x: graf[nod])if(x.fi != p)dfs(x.fi, nod, d+x.sc);
tout[nod] = t++;
}
void fillUp()
{
for(int i=1; i<lg; i++)
{
for(int j=0; j<n; j++)
{
up[j][i] = up[up[j][i-1]][i-1];
}
}
}
int lca(int u, int v)
{
if(u==v)return u;
if(tin[u] < tin[v] && tout[u] > tout[v])return u;
if(tin[u] > tin[v] && tout[u] < tout[v])return v;
for(int i=lg-1; i>=0; i--)
{
int guess = up[v][i];
if(!(tin[u] > tin[guess] && tout[u] < tout[guess]))v = guess;
}
return up[v][0];
}
int dfs2(int nod, int p)
{
subtreesize[nod] = 1;
for(auto x: graf[nod])
{
if(x.fi == p || wasCentoid[x.fi])continue;
subtreesize[nod] += dfs2(x.fi, nod);
}
return subtreesize[nod];
}
int findCentroid(int nod, int p, int treesize)
{
for(auto x: graf[nod])
{
if(x.fi==p || wasCentoid[x.fi])continue;
if(subtreesize[x.fi] * 2 > treesize)return findCentroid(x.fi, nod, treesize);
}
return nod;
}
void solve(int nod, int p)
{
int treesize = dfs2(nod, nod);
int centroid = findCentroid(nod, nod, treesize);
wasCentoid[centroid] = 1;
parents[centroid] = p;
if(p == -1)
{
root = centroid;
parents[centroid] = centroid;
}
for(auto x:graf[centroid])
{
if(wasCentoid[x.fi])continue;
solve(x.fi, centroid);
}
}
long long findDist(int u, int v)
{
return dist[u] + dist[v] - 2*dist[lca(u,v)];
}
void update(int nod)
{
int trnod = nod;
while(trnod != parents[trnod])
{
rez[trnod] = min(rez[trnod],findDist(nod, trnod));
trnod = parents[trnod];
}
rez[trnod] = min(rez[trnod], findDist(nod, trnod));
}
long long query(int nod)
{
int trnod = nod;
long long trrez= inf;
while(trnod != parents[trnod])
{
trrez = min(trrez, rez[trnod] + findDist(nod, trnod));
trnod = parents[trnod];
}
trrez = min(trrez, rez[trnod] + findDist(nod, trnod));
return trrez;
}
/*
signed main()
{
ios_base::sync_with_stdio(false),cin.tie(0), cout.tie(0);
int tt=1;
//cin >> tt;
while(tt--)
{
cin >> n >> q;
rez = vector<long long>(n, inf);
for(int i=0; i<n-1; i++)
{
int a,b,w;cin >> a >> b >> w;
graf[a].pb({b,w});
graf[b].pb({a,w});
}
dfs(0,0,0);
fillUp();
solve(0,0,false);
//for(int i=0; i<n; i++)cout << parents[i] << " ";cout << endl;
while(q--)
{
int a,b;cin >> a >> b;
vector<int> factory1(a);
for(int i=0; i<a; i++)
{
cin >> factory1[i];
update(factory1[i]);
}
vector<int> factory2(b);
int finalrez = inf;
for(int i=0; i<b; i++)
{
cin >> factory2[i];
finalrez = min(finalrez, query(factory2[i]));
}
///reset cenmtorid tree
for(int i=0; i<n; i++)
{
int nod = i;
while(rez[nod] !=inf)
{
rez[nod] = inf;
nod = parents[nod];
}
}
cout << finalrez << endl;
}
}
}
*/
void Init(int N, int A[], int B[], int D[]) {
ios_base::sync_with_stdio(false);cin.tie(0);
n = N;
for(int i=0; i<n-1; i++)
{
graf[A[i]].pb({B[i],D[i]});
graf[B[i]].pb({A[i],D[i]});
}
dfs(0,0,0);
fillUp();
solve(0,-1);
}
long long Query(int S, int X[], int T, int Y[]) {
for(int i=0; i<S; i++)update(X[i]);
long long finalrez = inf;
for(int i=0; i<T; i++)finalrez = min(finalrez, query(Y[i]));
for(int i=0; i<S; i++)
{
int nod = X[i];
while(rez[nod] !=inf)
{
rez[nod] = inf;
nod = parents[nod];
}
}
return finalrez;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
16728 KB |
Output is correct |
2 |
Correct |
386 ms |
34384 KB |
Output is correct |
3 |
Correct |
767 ms |
34312 KB |
Output is correct |
4 |
Correct |
750 ms |
34388 KB |
Output is correct |
5 |
Correct |
392 ms |
34492 KB |
Output is correct |
6 |
Correct |
146 ms |
34388 KB |
Output is correct |
7 |
Correct |
773 ms |
34388 KB |
Output is correct |
8 |
Correct |
796 ms |
34388 KB |
Output is correct |
9 |
Correct |
374 ms |
34388 KB |
Output is correct |
10 |
Correct |
143 ms |
34156 KB |
Output is correct |
11 |
Correct |
762 ms |
34388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16392 KB |
Output is correct |
2 |
Correct |
1131 ms |
117396 KB |
Output is correct |
3 |
Correct |
3292 ms |
118356 KB |
Output is correct |
4 |
Correct |
402 ms |
117948 KB |
Output is correct |
5 |
Correct |
1795 ms |
141772 KB |
Output is correct |
6 |
Correct |
3364 ms |
120368 KB |
Output is correct |
7 |
Correct |
2556 ms |
54868 KB |
Output is correct |
8 |
Correct |
226 ms |
55752 KB |
Output is correct |
9 |
Correct |
915 ms |
58604 KB |
Output is correct |
10 |
Correct |
2406 ms |
56236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
16728 KB |
Output is correct |
2 |
Correct |
386 ms |
34384 KB |
Output is correct |
3 |
Correct |
767 ms |
34312 KB |
Output is correct |
4 |
Correct |
750 ms |
34388 KB |
Output is correct |
5 |
Correct |
392 ms |
34492 KB |
Output is correct |
6 |
Correct |
146 ms |
34388 KB |
Output is correct |
7 |
Correct |
773 ms |
34388 KB |
Output is correct |
8 |
Correct |
796 ms |
34388 KB |
Output is correct |
9 |
Correct |
374 ms |
34388 KB |
Output is correct |
10 |
Correct |
143 ms |
34156 KB |
Output is correct |
11 |
Correct |
762 ms |
34388 KB |
Output is correct |
12 |
Correct |
8 ms |
16392 KB |
Output is correct |
13 |
Correct |
1131 ms |
117396 KB |
Output is correct |
14 |
Correct |
3292 ms |
118356 KB |
Output is correct |
15 |
Correct |
402 ms |
117948 KB |
Output is correct |
16 |
Correct |
1795 ms |
141772 KB |
Output is correct |
17 |
Correct |
3364 ms |
120368 KB |
Output is correct |
18 |
Correct |
2556 ms |
54868 KB |
Output is correct |
19 |
Correct |
226 ms |
55752 KB |
Output is correct |
20 |
Correct |
915 ms |
58604 KB |
Output is correct |
21 |
Correct |
2406 ms |
56236 KB |
Output is correct |
22 |
Correct |
1509 ms |
124656 KB |
Output is correct |
23 |
Correct |
1675 ms |
127324 KB |
Output is correct |
24 |
Correct |
5197 ms |
126656 KB |
Output is correct |
25 |
Correct |
5331 ms |
130388 KB |
Output is correct |
26 |
Correct |
5295 ms |
126804 KB |
Output is correct |
27 |
Correct |
2308 ms |
145744 KB |
Output is correct |
28 |
Correct |
452 ms |
128184 KB |
Output is correct |
29 |
Correct |
5341 ms |
126480 KB |
Output is correct |
30 |
Correct |
5259 ms |
126048 KB |
Output is correct |
31 |
Correct |
5203 ms |
128376 KB |
Output is correct |
32 |
Correct |
889 ms |
64172 KB |
Output is correct |
33 |
Correct |
215 ms |
60676 KB |
Output is correct |
34 |
Correct |
775 ms |
55892 KB |
Output is correct |
35 |
Correct |
804 ms |
57660 KB |
Output is correct |
36 |
Correct |
2403 ms |
56404 KB |
Output is correct |
37 |
Correct |
2361 ms |
56404 KB |
Output is correct |