Submission #41008

#TimeUsernameProblemLanguageResultExecution timeMemory
41008DoanPhuDucFactories (JOI14_factories)C++98
100 / 100
4809 ms368468 KiB
#include "factories.h" #include <bits/stdc++.h> #define FOR(x, a, b) for (int x = a; x <= b; ++x) #define FOD(x, a, b) for (int x = a; x >= b; --x) #define REP(x, a, b) for (int x = a; x < b; ++x) #define DEBUG(X) { cout << #X << " = " << X << endl; } #define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl; } #define PR0(A, n) { cout << #A << " = "; REP(_, 0, n) cout << A[_] << " "; cout << endl; } using namespace std; typedef long long LL; typedef pair <int, int> II; const int N = 5e5 + 10; const LL INF = (LL)1e18; const int LG = 19; int n, s, t, q, timer; int a[N], b[N], e[N], h[N]; LL f[N], d[N][LG + 3]; vector <II> adj[N]; struct CD { int cPar[N], c[N]; void Init() { memset(cPar, -1, sizeof cPar); memset(d, -1, sizeof d); DFS(1); Centroid(1, -1, c[1], -2); } void DFS(int u, int par = -1) { c[u] = 1; REP(k, 0, adj[u].size()) { int v = adj[u][k].first; if (v == par || cPar[v] != -1) continue; DFS(v, u); c[u] += c[v]; } } void DFS(int u, int par, int cen) { REP(k, 0, adj[u].size()) { int v = adj[u][k].first, w = adj[u][k].second; if (v == par || cPar[v] != -1) continue; d[v][h[cen]] = d[u][h[cen]] + w; DFS(v, u, cen); } } void Centroid(int u, int par, int sz, int preC) { REP(k, 0, adj[u].size()) { int v = adj[u][k].first; if (v == par || cPar[v] != -1) continue; if (c[v] * 2 > sz) { Centroid(v, u, sz, preC); return; } } cPar[u] = preC; if (preC != -2) h[u] = h[preC] + 1; else h[u] = 1; d[u][h[u]] = 0; DFS(u, -1, u); REP(k, 0, adj[u].size()) { int v = adj[u][k].first; if (cPar[v] != -1) continue; DFS(v); Centroid(v, u, c[v], u); } } } CD; void Init(int NN, int A[], int B[], int D[]) { n = NN; REP(i, 0, n - 1) { int u = A[i], v = B[i], w = D[i]; ++u; ++v; adj[u].push_back(II(v, w)); adj[v].push_back(II(u, w)); } FOR(i, 1, n) f[i] = INF; CD.Init(); } void Update(int u) { int x = u; while (x != -2) { assert(d[u][h[x]] != -1); f[x] = min(f[x], d[u][h[x]]); x = CD.cPar[x]; } } void Assign(int u, LL v) { int x = u; while (x != -2) { f[x] = v; x = CD.cPar[x]; } } LL Query(int u) { int x = u; LL ans = INF; while (x != -2) { assert(d[u][h[x]] != -1); ans = min(ans, f[x] + d[u][h[x]]); x = CD.cPar[x]; } return ans; } long long Query(int S, int X[], int T, int Y[]) { s = S; t = T; LL ans = INF; REP(i, 0, s) { ++X[i]; Update(X[i]); } REP(i, 0, t) { ++Y[i]; ans = min(ans, Query(Y[i])); } REP(i, 0, s) Assign(X[i], INF); return ans; }

Compilation message (stderr)

factories.cpp: In member function 'void CD::DFS(int, int)':
factories.cpp:6:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
                                        ^
factories.cpp:37:9: note: in expansion of macro 'REP'
         REP(k, 0, adj[u].size()) {
         ^
factories.cpp: In member function 'void CD::DFS(int, int, int)':
factories.cpp:6:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
                                        ^
factories.cpp:44:9: note: in expansion of macro 'REP'
         REP(k, 0, adj[u].size()) {
         ^
factories.cpp: In member function 'void CD::Centroid(int, int, int, int)':
factories.cpp:6:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
                                        ^
factories.cpp:52:9: note: in expansion of macro 'REP'
         REP(k, 0, adj[u].size()) {
         ^
factories.cpp:6:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
                                        ^
factories.cpp:64:9: note: in expansion of macro 'REP'
         REP(k, 0, adj[u].size()) {
         ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...