#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]*2)>treesize)return getcen(v,u,treesize);
}
return u;
}
void find_cent(int node, int parent, int size) {
int C=getcen(node,-1,dfs(node,-1));
dfs2(C, -1, C, 0);
used[C] = 1;
for (auto child :adj[C]) {
if (used[child.first]) continue;
find_cent(child.first, C,dfs(child.fi,C));
}
}
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 |
16 ms |
48208 KB |
Output is correct |
2 |
Correct |
198 ms |
53320 KB |
Output is correct |
3 |
Correct |
219 ms |
53832 KB |
Output is correct |
4 |
Correct |
212 ms |
53844 KB |
Output is correct |
5 |
Correct |
232 ms |
54104 KB |
Output is correct |
6 |
Correct |
159 ms |
52776 KB |
Output is correct |
7 |
Correct |
207 ms |
53600 KB |
Output is correct |
8 |
Correct |
207 ms |
53832 KB |
Output is correct |
9 |
Correct |
219 ms |
54088 KB |
Output is correct |
10 |
Correct |
154 ms |
52808 KB |
Output is correct |
11 |
Correct |
218 ms |
53804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
48208 KB |
Output is correct |
2 |
Correct |
1699 ms |
207728 KB |
Output is correct |
3 |
Correct |
2932 ms |
279684 KB |
Output is correct |
4 |
Correct |
623 ms |
102312 KB |
Output is correct |
5 |
Correct |
3827 ms |
353096 KB |
Output is correct |
6 |
Correct |
3065 ms |
279148 KB |
Output is correct |
7 |
Correct |
584 ms |
86600 KB |
Output is correct |
8 |
Correct |
236 ms |
59832 KB |
Output is correct |
9 |
Correct |
744 ms |
89532 KB |
Output is correct |
10 |
Correct |
651 ms |
87008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
48208 KB |
Output is correct |
2 |
Correct |
198 ms |
53320 KB |
Output is correct |
3 |
Correct |
219 ms |
53832 KB |
Output is correct |
4 |
Correct |
212 ms |
53844 KB |
Output is correct |
5 |
Correct |
232 ms |
54104 KB |
Output is correct |
6 |
Correct |
159 ms |
52776 KB |
Output is correct |
7 |
Correct |
207 ms |
53600 KB |
Output is correct |
8 |
Correct |
207 ms |
53832 KB |
Output is correct |
9 |
Correct |
219 ms |
54088 KB |
Output is correct |
10 |
Correct |
154 ms |
52808 KB |
Output is correct |
11 |
Correct |
218 ms |
53804 KB |
Output is correct |
12 |
Correct |
13 ms |
48208 KB |
Output is correct |
13 |
Correct |
1699 ms |
207728 KB |
Output is correct |
14 |
Correct |
2932 ms |
279684 KB |
Output is correct |
15 |
Correct |
623 ms |
102312 KB |
Output is correct |
16 |
Correct |
3827 ms |
353096 KB |
Output is correct |
17 |
Correct |
3065 ms |
279148 KB |
Output is correct |
18 |
Correct |
584 ms |
86600 KB |
Output is correct |
19 |
Correct |
236 ms |
59832 KB |
Output is correct |
20 |
Correct |
744 ms |
89532 KB |
Output is correct |
21 |
Correct |
651 ms |
87008 KB |
Output is correct |
22 |
Correct |
2156 ms |
204840 KB |
Output is correct |
23 |
Correct |
2130 ms |
208212 KB |
Output is correct |
24 |
Correct |
3612 ms |
277048 KB |
Output is correct |
25 |
Correct |
3385 ms |
279604 KB |
Output is correct |
26 |
Correct |
3480 ms |
278708 KB |
Output is correct |
27 |
Correct |
4370 ms |
349768 KB |
Output is correct |
28 |
Correct |
774 ms |
108640 KB |
Output is correct |
29 |
Correct |
3435 ms |
277860 KB |
Output is correct |
30 |
Correct |
3186 ms |
277880 KB |
Output is correct |
31 |
Correct |
3229 ms |
288140 KB |
Output is correct |
32 |
Correct |
726 ms |
103824 KB |
Output is correct |
33 |
Correct |
241 ms |
65424 KB |
Output is correct |
34 |
Correct |
435 ms |
79688 KB |
Output is correct |
35 |
Correct |
466 ms |
81480 KB |
Output is correct |
36 |
Correct |
617 ms |
88904 KB |
Output is correct |
37 |
Correct |
575 ms |
85732 KB |
Output is correct |