Submission #405731

#TimeUsernameProblemLanguageResultExecution timeMemory
405731knightron0Factories (JOI14_factories)C++17
15 / 100
8066 ms143020 KiB
#include "factories.h" #include <bits/stdc++.h> using namespace std; #define pb push_back #define fr first #define sc second #define clr(a, x) memset(a, x, sizeof(a)) #define dbg(x) cout<<"("<<#x<<"): "<<x<<endl; #define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl; #define all(v) v.begin(), v.end() #define lcm(a, b) (a * b)/__gcd(a, b) #define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl; #define endl '\n' #define ll long long int #define float long double const int MOD = 1e9 + 7; const ll INF = 2e15; const int MAXN = 5e5 + 5; const int LOGN = 23; vector<array<ll, 2>> adj[MAXN]; ll par[MAXN], sz[MAXN], depth[MAXN], dist[LOGN][MAXN], ans[MAXN], nm; bool rm[MAXN]; int dfs(int u, int p){ sz[u] = 1; for(auto &[v, w]: adj[u]){ if(v != p && !rm[v]){ sz[u] += dfs(v, u); } } return sz[u]; } void dfs2(int u, int p, int l){ for(auto &[v, w]: adj[u]){ if(v != p && !rm[v]){ dist[l][v] = dist[l][u] + w; dfs2(v, u, l); } } } int centroid(int u, int p, int n){ for(auto &[v, w]: adj[u]){ if(v != p && !rm[v]){ if(sz[v]*2 > n){ return centroid(v, u, n); } } } return u; } void decompose(int u, int p, int d){ int n = dfs(u, p); int c = centroid(u, p, n); par[c] = p; depth[c] = d; dist[d][c] = 0; dfs2(c, -1, d); rm[c] = 1; for(auto &[v, w]: adj[c]){ if(!rm[v]){ decompose(v, c, d+1); } } } void update(int v, int s){ ans[v] = min(ans[v], dist[depth[v]][s]); if(depth[v] != 0){ update(par[v], s); } } ll qry(int v, int s){ if(depth[v] != 0){ return min(ans[v]+dist[depth[v]][s], qry(par[v], s)); } else { return ans[v] + dist[depth[v]][s]; } } void Init(int N, int A[], int B[], int D[]) { nm = N; for(int i=0;i<=N;i++){ rm[i] = false; sz[i] = 1; } for(int i= 0;i<nm-1;i++){ A[i]++; B[i]++; adj[A[i]].pb({B[i], D[i]}); adj[B[i]].pb({A[i], D[i]}); } decompose(1, 0, 0); for(int i=0;i<=nm;i++) ans[i]= INF; } ll Query(int S, int X[], int T, int Y[]){ for(int i=0;i<S;i++){ X[i]++; update(X[i], X[i]); } ll res = INF; for(int i = 0;i<T;i++){ Y[i]++; res = min(res, qry(Y[i], Y[i])); } for(int i=0;i<=nm;i++) ans[i]= INF; return res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...