Submission #233667

#TimeUsernameProblemLanguageResultExecution timeMemory
233667almogwaldFactories (JOI14_factories)C++14
33 / 100
8055 ms78684 KiB
#include <iostream> #include <vector> #include <algorithm> #include <math.h> #include <set> #define forr(i,a,b,c) for(int i=a;i<b;i+=c) #define fort(i,a,b) forr(i,a,b,1) #define forn(i,n) fort(i,1,n) #define fori(i,n) fort(i,0,n) #define forrb(i,a,b,c) for(int i=b-1;i>=a;i-=c) #define fortb(i,a,b) forrb(i,a,b,1) #define fornb(i,n) fortb(i,1,n) #define forib(i,n) fortb(i,0,n) using namespace std; #define into(x) cin >> x; typedef vector<int> veci; typedef long long lol; typedef pair<lol,lol> point; #define def(x) lol x; into(x) #define MAX_N 500000 #define logn 19 int cons[2*MAX_N]; int scons[MAX_N]; lol consl[2 * MAX_N]; lol depth[MAX_N]; int dep[MAX_N]; int f[MAX_N][logn]; int conss[MAX_N]; int visited[MAX_N]; int n; void Init(int N, int A[], int B[], int D[]) { n = N; fori(i, N) { visited[i] = 0; conss[i] = 0; } fori(i, N - 1) { conss[A[i]]++; conss[B[i]]++; } fori(i, N - 1) { scons[i + 1] = scons[i] + conss[i]; } fori(i, N) { conss[i] = 0; } fori(i, N - 1) { cons[scons[A[i]] + conss[A[i]]] = B[i]; consl[scons[A[i]] + conss[A[i]]] = D[i]; cons[scons[B[i]] + conss[B[i]]] = A[i]; consl[scons[B[i]] + conss[B[i]]] = D[i]; conss[A[i]]++; conss[B[i]]++; } f[0][0] = 0; dep[0] = 0; depth[0] = 0; veci stk(1); while (!stk.empty()) { int cur = stk.back(); stk.pop_back(); visited[cur] = 1; forn(i, logn) { f[cur][i] = f[f[cur][i - 1]][i - 1]; } fori(i, conss[cur]) { int oth = cons[scons[cur] + i]; if (visited[oth] == 0) { f[oth][0] = cur; dep[oth] = dep[cur] + 1; depth[oth] = depth[cur] + consl[scons[cur] + i]; stk.push_back(oth); } } } } inline int lca(int l, int r) { int k = logn-1; while (dep[l] != dep[r]) { if (dep[l] + (1 << k) <= dep[r]) { r = f[r][k]; } if (dep[r] + (1 << k) <= dep[l]) { l = f[l][k]; } k--; } if (l == r) { return l; } k = logn - 1; while (k>=0) { if (f[r][k]!= f[l][k]) { r = f[r][k]; l = f[l][k]; } k--; } return f[l][0]; } long long Query(int S, int X[], int T, int Y[]) { if (S*T*20< n) { lol minmium = 1298719928229189219; fori(i, S) { fori(j, T) { minmium = min(minmium, depth[X[i]] + depth[Y[j]] - 2 * depth[lca(X[i], Y[j])]); } } return minmium; } else { set<pair<lol,int>> stk; fori(i, S) { stk.insert({ 0,X[i] }); } fori(i, n) { visited[i] = 0; } fori(j, T) { visited[Y[j]] = -1; } while (!stk.empty()) { auto it = stk.begin(); lol dis = it->first; int cur = it->second; stk.erase(it); if (visited[cur]) { if (visited[cur] == -1) { return dis; } continue; } visited[cur] = 1; fori(i, conss[cur]) { stk.insert({ consl[scons[cur]+i]+dis,cons[scons[cur] + i] }); } } } }

Compilation message (stderr)

factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:141:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...