#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
//#define int long long
#include "factories.h"
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<pll> vll;
typedef tree<pii,null_type,less<pii>,rb_tree_tag,
tree_order_statistics_node_update> oset;
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mid (l+r)/2
#define all(x) x.begin(),x.end()
#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)
#define cont(x) for(auto el:x) cout<<el<<' ';cout<<endl;
#define contp(x) for(auto el:x) cout<<el.fi<<'-'<<el.se<<' ';cout<<endl;
#define sp <<" "<<
#define DEBUG(x) cout<<(#x) sp x<<endl
#define carp(a,b) (((a%MOD)*(b%MOD))%MOD)
#define topla(a,b) (((a%MOD)+(b%MOD))%MOD)
const ll INF=1e18;
const int MAXN=5e5+5;
const int MAXK=26;
const int MOD=1e9+7;
//int N;
//int a[MAXN], b[MAXN], d[MAXN];
int n;
vll adj[MAXN];
int sub[MAXN];
bool mark[MAXN];
void dfs(int nd,int ata){
sub[nd]=1;
for(auto [kom,cost]:adj[nd]){
if(mark[kom] or kom==ata) continue;
dfs(kom,nd);
sub[nd]+=sub[kom];
}
}
vll pp[MAXN];
int cc;
void dfs2(int nd,int ata,ll h){
pp[nd].pb({cc,h});
for(auto [kom,cost]:adj[nd]){
if(mark[kom] or kom==ata) continue;
dfs2(kom,nd,h+cost);
}
}
int find_cent(int nd,int sz,int ata){
for(auto [kom,cost]:adj[nd]){
if(mark[kom] or kom==ata) continue;
if(sub[kom]*2>sz) return find_cent(kom,sz,nd);
}
return nd;
}
void solve(int nd){
dfs(nd,-1);
int c=find_cent(nd,sub[nd],-1);
mark[c]=true;
//calculate here
cc=c;
pp[cc].pb({c,0});
for(auto [kom,cost]:adj[c]){
if(!mark[kom]){
dfs2(kom,c,cost);
}
}
for(auto [kom,cost]:adj[c]){
if(!mark[kom]){
solve(kom);
}
}
}
void Init(int N, int a[], int b[], int d[]) {
n=N;
FOR(i,n){
a[i]++;b[i]++;
adj[a[i]].pb({b[i],d[i]});
adj[b[i]].pb({a[i],d[i]});
}
solve(1);
}
//int x[MAXN], y[MAXN];
ll ans;
ll mn[MAXN];
ll Query(int s, int x[], int t, int y[]) {
ans=INF;
FOR(i,s){
int nd=x[i]+1;
for(auto [c,cost]:pp[nd]) mn[c]=INF;
}
FOR(i,t){
int nd=y[i]+1;
for(auto [c,cost]:pp[nd]) mn[c]=INF;
}
FOR(i,s){
int nd=x[i]+1;
for(auto [c,cost]:pp[nd]) mn[c]=min(mn[c],cost);
}
FOR(i,t){
int nd=y[i]+1;
for(auto [c,cost]:pp[nd]) ans=min(ans,cost+mn[c]);
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |