#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
#define MAX_N 500000
#define MAX_Q 100000
#define MAX_SUM_ST 1000000
#define MAX_VALUE 1000000000
#define ff first
#define ss second
#define pb push_back
#include "factories.h"
#define mp make_pair
typedef long long ll;
using namespace std;
ll n,a[1000001],b[1000001],d[1000001],k=0,first[1000001],belong[25][1000001],sz[1000001],centroid,nearest[1000001],o=0,p[1000001];
bool vis[1000001];
unordered_map<ll,ll>pre[1000001];
vector<int>vec;
vector<pair<ll,ll> >adj[1000001];
void dfs1(ll node,ll par){
sz[node]=1;
for(int i=0;i<adj[node].size();i++){
if(adj[node][i].ff!=par && vis[adj[node][i].ff]==0){
dfs1(adj[node][i].ff,node);
sz[node]+=sz[adj[node][i].ff];
}
}
}
void dfs2(ll node,ll par,ll dis){
pre[centroid][node]=dis;
for(int i=0;i<adj[node].size();i++){
if(adj[node][i].ff!=par && vis[adj[node][i].ff]==0){
dfs2(adj[node][i].ff,node,dis+adj[node][i].ss);
}
}
}
void solve(ll node,ll par,ll level,ll parent){
// cout << node << '\n';
o++;
dfs1(node,par);
ll cent=node;
while(1){
bool f=true;
for(int i=0;i<adj[cent].size();i++){
if(sz[adj[cent][i].ff]<sz[cent] && vis[adj[cent][i].ff]==0 && sz[adj[cent][i].ff]*2>sz[node]){
f=false;
cent=adj[cent][i].ff;
break;
}
}
if(f) break;
}
centroid=cent;
dfs2(cent,-1,0);
// cout << cent << ' ' << level << '\n';
p[cent]=parent;
vis[cent]=1;
for(int i=0;i<adj[cent].size();i++){
if(vis[adj[cent][i].ff]==0){
solve(adj[cent][i].ff,-1,level+1,cent);
}
}
}
void Init(int N, int A[], int B[], int D[]) {
n=(ll)N;
for(int i=0;i<(N-1);i++){
a[i]=(ll)A[i];
b[i]=(ll)B[i];
d[i]=(ll)D[i];
adj[a[i]].pb(mp(b[i],d[i]));
adj[b[i]].pb(mp(a[i],d[i]));
}
solve(0,-1,1,-1);
for(int i=0;i<n;i++){
nearest[i]=1e18;
}
}
void add(int node,int cent){
if(cent==-1) return;
nearest[cent]=min(nearest[cent],pre[cent][node]);
// cout << node << ' ' << cent << ' ' << nearest[cent] << '\n';
vec.pb(cent);
add(node,p[cent]);
}
ll query(int node,int cent){
if(cent==-1) return 2e18;
return min(nearest[cent]+pre[cent][node],query(node,p[cent]));
}
long long Query(int S, int X[], int T, int Y[]) {
for(int i=0;i<S;i++){
add(X[i],X[i]);
}
ll ans=2e18;
for(int i=0;i<T;i++){
ans=min(ans,query(Y[i],Y[i]));
}
for(int i=0;i<vec.size();i++){
nearest[vec[i]]=1e18;
}
vec.clear();
return ans;
}
Compilation message
factories.cpp: In function 'void dfs1(ll, ll)':
factories.cpp:22:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i=0;i<adj[node].size();i++){
| ~^~~~~~~~~~~~~~~~~
factories.cpp: In function 'void dfs2(ll, ll, ll)':
factories.cpp:31:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i=0;i<adj[node].size();i++){
| ~^~~~~~~~~~~~~~~~~
factories.cpp: In function 'void solve(ll, ll, ll, ll)':
factories.cpp:44:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i=0;i<adj[cent].size();i++){
| ~^~~~~~~~~~~~~~~~~
factories.cpp:58:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i=0;i<adj[cent].size();i++){
| ~^~~~~~~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:97:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for(int i=0;i<vec.size();i++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
79340 KB |
Output is correct |
2 |
Correct |
903 ms |
91756 KB |
Output is correct |
3 |
Correct |
1228 ms |
92140 KB |
Output is correct |
4 |
Correct |
1209 ms |
92396 KB |
Output is correct |
5 |
Correct |
1436 ms |
93036 KB |
Output is correct |
6 |
Correct |
433 ms |
90604 KB |
Output is correct |
7 |
Correct |
1193 ms |
91656 KB |
Output is correct |
8 |
Correct |
1230 ms |
91756 KB |
Output is correct |
9 |
Correct |
1474 ms |
92268 KB |
Output is correct |
10 |
Correct |
428 ms |
90012 KB |
Output is correct |
11 |
Correct |
1170 ms |
91756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
78956 KB |
Output is correct |
2 |
Correct |
6521 ms |
369356 KB |
Output is correct |
3 |
Execution timed out |
8118 ms |
502276 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
79340 KB |
Output is correct |
2 |
Correct |
903 ms |
91756 KB |
Output is correct |
3 |
Correct |
1228 ms |
92140 KB |
Output is correct |
4 |
Correct |
1209 ms |
92396 KB |
Output is correct |
5 |
Correct |
1436 ms |
93036 KB |
Output is correct |
6 |
Correct |
433 ms |
90604 KB |
Output is correct |
7 |
Correct |
1193 ms |
91656 KB |
Output is correct |
8 |
Correct |
1230 ms |
91756 KB |
Output is correct |
9 |
Correct |
1474 ms |
92268 KB |
Output is correct |
10 |
Correct |
428 ms |
90012 KB |
Output is correct |
11 |
Correct |
1170 ms |
91756 KB |
Output is correct |
12 |
Correct |
58 ms |
78956 KB |
Output is correct |
13 |
Correct |
6521 ms |
369356 KB |
Output is correct |
14 |
Execution timed out |
8118 ms |
502276 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |