#include <bits/stdc++.h>
//#include "factories.h"
using namespace std;
#define ll long long
#define fi first
#define se second
const ll MOD = 1e9 + 7;
const ll N = 5e5 + 2;
const ll inf = 1e14 + 2;
vector<ll> minn(N);
vector<int> sz(N), used(N, 0);
vector<vector<pair<int, ll>>> adj(N), ancestors(N);
int n;
int dfs(int u,int par)
{
sz[u]=1;
for(pair<int,ll>pp:adj[u]){
int v=pp.fi;
int w=pp.se;
if(v==par||used[v])continue;
sz[u]+=dfs(v,u);
}
return sz[u];
}
void dfs2(int u,int par,int cen,ll depth)
{
ancestors[u].push_back({cen,depth});
for(pair<int,ll> pp:adj[u]){
int v=pp.fi;
ll w=pp.se;
if(v==par||used[v])continue;
dfs2(v,u,cen,depth+w);
}
}
int getcen(int u,int par,int treesize)
{
for(pair<int,ll> pp:adj[u]){
int v=pp.fi;
if(v==par||used[v])continue;
if((sz[v]<<1)>treesize)return getcen(v,u,treesize);
}
return u;
}
void find_cent(int node, int parent, int size) {
for (auto child : adj[node]) {
if (child.first == parent || used[child.first]) continue;
if (sz[child.first] * 2 > size) {
find_cent(child.first, node, size);
return;
}
}
dfs2(node, -1, node, 0);
used[node] = 1;
for (auto child :adj[node]) {
if (used[child.first]) continue;
find_cent(child.first, node,dfs(child.fi,node));
}
}
void Init(int N, int A[], int B[], int D[]) {
for (int i = 0; i < N-1; i++) {
adj[A[i]].push_back(make_pair(B[i], D[i]));
adj[B[i]].push_back(make_pair(A[i], D[i]));
}
find_cent(0, -1,dfs(0,-1));
}
long long Query(int S, int X[], int T, int Y[])
{
for(int i=0;i<T;i++){
for(pair<int,long long> y:ancestors[Y[i]]){
minn[y.fi]=inf;
}
}
for(int i=0;i<S;i++){
for(pair<int,ll> y:ancestors[X[i]]){
minn[y.fi]=min(minn[y.fi],y.se);
}
}
ll ans=inf;
for(int i=0;i<T;i++){
for(pair<int,ll> y:ancestors[Y[i]]){
ans=min(ans,minn[y.fi]+y.se);
}
}
return ans;
}
Compilation message
factories.cpp: In function 'int dfs(int, int)':
factories.cpp:21:13: warning: unused variable 'w' [-Wunused-variable]
21 | int w=pp.se;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
48208 KB |
Output is correct |
2 |
Correct |
204 ms |
53288 KB |
Output is correct |
3 |
Correct |
218 ms |
53804 KB |
Output is correct |
4 |
Correct |
202 ms |
55880 KB |
Output is correct |
5 |
Correct |
232 ms |
54088 KB |
Output is correct |
6 |
Correct |
176 ms |
52808 KB |
Output is correct |
7 |
Correct |
203 ms |
53832 KB |
Output is correct |
8 |
Correct |
221 ms |
53844 KB |
Output is correct |
9 |
Correct |
241 ms |
54704 KB |
Output is correct |
10 |
Correct |
143 ms |
52808 KB |
Output is correct |
11 |
Correct |
214 ms |
53832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
48224 KB |
Output is correct |
2 |
Correct |
1570 ms |
207692 KB |
Output is correct |
3 |
Correct |
2113 ms |
279552 KB |
Output is correct |
4 |
Correct |
501 ms |
102312 KB |
Output is correct |
5 |
Correct |
3020 ms |
353324 KB |
Output is correct |
6 |
Correct |
2507 ms |
279112 KB |
Output is correct |
7 |
Correct |
554 ms |
86472 KB |
Output is correct |
8 |
Correct |
249 ms |
63124 KB |
Output is correct |
9 |
Correct |
618 ms |
99656 KB |
Output is correct |
10 |
Correct |
487 ms |
86872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
48208 KB |
Output is correct |
2 |
Correct |
204 ms |
53288 KB |
Output is correct |
3 |
Correct |
218 ms |
53804 KB |
Output is correct |
4 |
Correct |
202 ms |
55880 KB |
Output is correct |
5 |
Correct |
232 ms |
54088 KB |
Output is correct |
6 |
Correct |
176 ms |
52808 KB |
Output is correct |
7 |
Correct |
203 ms |
53832 KB |
Output is correct |
8 |
Correct |
221 ms |
53844 KB |
Output is correct |
9 |
Correct |
241 ms |
54704 KB |
Output is correct |
10 |
Correct |
143 ms |
52808 KB |
Output is correct |
11 |
Correct |
214 ms |
53832 KB |
Output is correct |
12 |
Correct |
13 ms |
48224 KB |
Output is correct |
13 |
Correct |
1570 ms |
207692 KB |
Output is correct |
14 |
Correct |
2113 ms |
279552 KB |
Output is correct |
15 |
Correct |
501 ms |
102312 KB |
Output is correct |
16 |
Correct |
3020 ms |
353324 KB |
Output is correct |
17 |
Correct |
2507 ms |
279112 KB |
Output is correct |
18 |
Correct |
554 ms |
86472 KB |
Output is correct |
19 |
Correct |
249 ms |
63124 KB |
Output is correct |
20 |
Correct |
618 ms |
99656 KB |
Output is correct |
21 |
Correct |
487 ms |
86872 KB |
Output is correct |
22 |
Correct |
1708 ms |
204804 KB |
Output is correct |
23 |
Correct |
1878 ms |
208036 KB |
Output is correct |
24 |
Correct |
3591 ms |
277296 KB |
Output is correct |
25 |
Correct |
3164 ms |
279668 KB |
Output is correct |
26 |
Correct |
2771 ms |
278700 KB |
Output is correct |
27 |
Correct |
3435 ms |
349768 KB |
Output is correct |
28 |
Correct |
686 ms |
102312 KB |
Output is correct |
29 |
Correct |
2672 ms |
277848 KB |
Output is correct |
30 |
Correct |
2599 ms |
278088 KB |
Output is correct |
31 |
Correct |
2610 ms |
277892 KB |
Output is correct |
32 |
Correct |
674 ms |
99656 KB |
Output is correct |
33 |
Correct |
236 ms |
63164 KB |
Output is correct |
34 |
Correct |
405 ms |
79688 KB |
Output is correct |
35 |
Correct |
388 ms |
80696 KB |
Output is correct |
36 |
Correct |
535 ms |
85604 KB |
Output is correct |
37 |
Correct |
588 ms |
85596 KB |
Output is correct |