This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<cstdio>
#include<algorithm>
#include<vector>
#define N_ 201000
#define SZ 262144
#define pii pair<int,int>
#define X first
#define Y second
using namespace std;
vector<int>E[N_];
int n, chk[N_], cnt;
long long S[N_], Far[N_], Res[N_];
struct Edge {
int e, l;
}Ed[N_*2];
void Add_Edge(int a, int b, int c) {
E[a].push_back(cnt);
Ed[cnt++] = { b,c };
}
void DFS1(int a, int pp) {
for (auto &t : E[a]) {
int x = Ed[t].e, l = Ed[t].l;
if (x == pp)continue;
DFS1(x, a);
Far[a] = max(Far[a], Far[x] + l);
S[a] += S[x] + l;
}
}
void DFS2(int a, int pp, long long f) {
int pv1 = -1, pv2 = -1;
long long f1 = 0, f2 = 0;
Far[a] = max(Far[a], f);
for (auto &t : E[a]) {
int x = Ed[t].e, l = Ed[t].l;
if (x == pp)continue;
S[x] = S[a] - l + Ed[t ^ 1].l;
if (f1 <= Far[x] + l) {
pv2 = pv1, f2 = f1;
pv1 = x, f1 = Far[x] + l;
}
else if (f2 <= Far[x] + l) {
pv2 = x, f2 = Far[x] + l;
}
}
for (auto &t : E[a]) {
int x = Ed[t].e, l = Ed[t].l;
if (x == pp)continue;
if (x == pv1) {
DFS2(x, a, Ed[t ^ 1].l + max(f2,f));
}
else {
DFS2(x, a, Ed[t ^ 1].l + max(f1,f));
}
}
}
int Num[N_], End[N_], ReNum[N_], par[N_], pL[N_];
long long DD[N_];
struct Tree {
long long Mx[SZ + SZ], K[SZ + SZ];
void UDT(int nd) {
Mx[nd] = max(Mx[nd * 2], Mx[nd * 2 + 1]);
}
void init(int nd, int b, int e) {
K[nd] = 0;
if (b == e) {
Mx[nd] = DD[ReNum[b]];
return;
}
int m = (b + e) >> 1;
init(nd * 2, b, m);
init(nd * 2 + 1, m + 1, e);
UDT(nd);
}
void Add2(int nd, long long x) {
Mx[nd] += x;
K[nd] += x;
}
void Spread(int nd) {
Add2(nd * 2, K[nd]);
Add2(nd * 2 + 1, K[nd]);
K[nd] = 0;
}
void Add(int nd, int b, int e, int s, int l, long long x) {
if (s > l)return;
if (b == s && e == l) {
Add2(nd, x);
return;
}
int m = (b + e) >> 1;
Spread(nd);
if (s <= m)Add(nd * 2, b, m, s, min(m, l), x);
if (l > m)Add(nd * 2 + 1, m + 1, e, max(m + 1, s), l, x);
UDT(nd);
}
int Find(int nd, int b, int e) {
if (Mx[nd] == 0)return 0;
if (b == e)return ReNum[b];
int m = (b + e) >> 1;
Spread(nd);
if (Mx[nd * 2] == Mx[nd])return Find(nd * 2, b, m);
return Find(nd * 2 + 1, m + 1, e);
}
}TT;
void DFS3(int a, int pp, long long d) {
Num[a] = ++cnt;
ReNum[cnt] = a;
DD[a] = d;
par[a] = pp;
for (auto &t : E[a]) {
int x = Ed[t].e, l = Ed[t].l;
if (x == pp)continue;
pL[x] = l;
DFS3(x, a, d+l);
}
End[a] = cnt;
}
int main() {
//freopen("input.txt", "r", stdin);
int i, a, b, c, d;
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d%d%d%d", &a, &b, &c, &d);
Add_Edge(a, b, c);
Add_Edge(b, a, d);
}
DFS1(1, 0);
DFS2(1, 0, 0);
for (i = 1; i <= n; i++)Res[i] = 1e18;
for (i = 1; i <= n; i++)Res[1] = min(Res[1], S[i]);
int pv = -1;
for (i = 1; i <= n; i++) {
if (Res[2] > S[i] - Far[i]) {
Res[2] = S[i] - Far[i];
pv = i;
}
}
int root = pv;
cnt = 0;
DFS3(root, 0, 0);
TT.init(1, 1, n);
for (i = 1; i <= n; i++)chk[i] = 0;
chk[root] = 1;
long long ss = S[root];
for (i = 2; i <= n; i++) {
ss -= TT.Mx[1];
Res[i] = min(Res[i], ss);
int a = TT.Find(1, 1, n);
if (!a) continue;
while (!chk[a]) {
TT.Add(1, 1, n, Num[a], End[a], -pL[a]);
chk[a] = 1;
a = par[a];
}
}
int Q;
scanf("%d", &Q);
while (Q--) {
scanf("%d", &a);
printf("%lld\n", Res[a]);
}
}
Compilation message (stderr)
designated_cities.cpp: In function 'void DFS2(int, int, long long int)':
designated_cities.cpp:46:20: warning: unused variable 'l' [-Wunused-variable]
int x = Ed[t].e, l = Ed[t].l;
^
designated_cities.cpp:30:16: warning: variable 'pv2' set but not used [-Wunused-but-set-variable]
int pv1 = -1, pv2 = -1;
^~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:120:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
designated_cities.cpp:122:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &a, &b, &c, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:157:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Q);
~~~~~^~~~~~~~~~
designated_cities.cpp:159:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a);
~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |