Submission #825273

#TimeUsernameProblemLanguageResultExecution timeMemory
825273KLPPFactories (JOI14_factories)C++14
100 / 100
5925 ms400768 KiB
#include "factories.h" #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long int lld; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; #define rep(i,a,b) for(int i=a;i<b;i++) #define trav(a,v) for(auto a:v) void DFS(int node, vector<vector<pair<int,lld> > >&nei, vector<int> &sz, vector<bool> &used){ sz[node]=1; trav(a,nei[node]){ if(sz[a.first]==-1 && !used[a.first]){ DFS(a.first,nei,sz,used); sz[node]+=sz[a.first]; } } } int findcentroid(int node, int tree_sz, vector<vector<pair<int,lld> > >&nei, vector<int> &sz, vector<bool> &used){ trav(a,nei[node]){ if(!used[a.first] && 2*sz[a.first]>tree_sz && sz[a.first]<sz[node]){ return findcentroid(a.first,tree_sz,nei,sz,used); } } return node; } void DFS2(int node, lld dist, int cent, vector<vector<pair<int,lld> > >&nei,vector<bool> &visited, vector<bool> &used,vector<vector<pair<int,lld> > > &dists){ dists[node].push_back({cent,dist}); visited[node]=true; trav(a,nei[node]){ if(!visited[a.first] && !used[a.first]){ DFS2(a.first,dist+a.second,cent,nei,visited,used,dists); } } } const lld INF=1e18; int n; vector<vector<pair<int,lld>> >dists; vector<lld> ans; void Init(int N, int A[], int B[], int D[]) { n=N; ans.resize(n,INF); vector<vector<pair<int,lld>> >nei(n); rep(i,0,n-1){ int x,y; lld z; x=A[i]; y=B[i]; z=D[i]; nei[x].push_back({y,z}); nei[y].push_back({x,z}); } dists.resize(n); vector<int> sz; vector<bool> used(n,false); vector<bool> visited; rep(t,0,25){ sz.clear(); sz.resize(n,-1); visited.clear(); visited.resize(n,false); rep(i,0,n){ if(!used[i] && sz[i]==-1){ DFS(i,nei,sz,used); int cent=findcentroid(i,sz[i],nei,sz,used); DFS2(cent,0,cent,nei,visited,used,dists); used[cent]=true; } } } } queue<int> q; long long Query(int S, int X[], int T, int Y[]) { int s,t; s=S; t=T; rep(i,0,s){ int x; x=X[i]; trav(a,dists[x]){ q.push(a.first); ans[a.first]=min(ans[a.first],a.second); } } lld finans=INF; rep(i,0,t){ int x; x=Y[i]; trav(a,dists[x]){ finans=min(finans,a.second+ans[a.first]); } } while(!q.empty()){ int u=q.front(); q.pop(); ans[u]=INF; } return finans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...