#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
const int N = 200005;
int n, p5[N];
vector<int> iv[N], v[N];
vector<array<ll, 2> > se[N][2], m5;
map<array<int, 2>, int> e;
array<ll, 2> s[N], spt[N], p1;
array<ll, 3> p2;
ll t0, fp[N], is5[N];
vector<ll> u5, u5c;
bool t5[N];
array<ll, 2> operator+(array<ll, 2> x, int y) {
x[0] += y;
return x;
}
void dfs_0(int x = 1, int y = 0) {
for(int z : iv[x]) {
if(z == y) continue;
v[x].push_back(z);
dfs_0(z, x);
}
}
void dfs_1(int x = 1) {
s[x] = {0, x};
se[x][0].resize(sz(v[x]));
se[x][1].resize(sz(v[x]));
for(int z : v[x]) {
dfs_1(z);
s[x] = max(s[x], s[z] + e[{x, z}]);
}
if(!sz(v[x])) return;
se[x][0][0] = s[v[x][0]] + e[{x, v[x][0]}];
for(int i = 1; i < sz(v[x]); ++i) {
se[x][0][i] = max(se[x][0][i - 1], s[v[x][i]] + e[{x, v[x][i]}]);
}
se[x][1].back() = s[v[x].back()] + e[{x, v[x].back()}];
for(int i = sz(v[x]) - 2; i >= 0; --i) {
se[x][1][i] = max(se[x][1][i + 1], s[v[x][i]] + e[{x, v[x][i]}]);
}
}
void dfs_2(int x = 1) {
for(int i = 0; i < sz(v[x]); ++i) {
int z = v[x][i];
array<ll, 2> l = {0, 0}, r = {0, 0};
if(i > 0) l = se[x][0][i - 1];
if(i < sz(v[x]) - 1) r = se[x][1][i + 1];
spt[z] = max(max(l, r), spt[x]) + e[{z, x}];
dfs_2(z);
}
}
ll dfs_3(int x = 1) {
ll y = 0;
for(int z : v[x]) {
y += dfs_3(z) + e[{z, x}];
}
return y;
}
void dfs_4(ll y, int x = 1) {
p1 = max(p1, array<ll, 2>{y, x});
array<ll, 2> t2 = max(s[x], spt[x]);
array<ll, 3> t3 = {t2[0] + y, t2[1], x};
p2 = max(p2, t3);
for(int z : v[x]) {
dfs_4(y - e[{z, x}] + e[{x, z}], z);
}
}
void dfs_5(int x = p2[1], int y = 0) {
p5[x] = y;
for(int z : iv[x]) {
if(z == y) continue;
dfs_5(z, x);
}
}
void dfs_6(int x = p2[1], int y = 0) {
if(!t5[x]) {
is5[x] = is5[y] + e[{y, x}];
}
for(int z : iv[x]) {
if(z == y) continue;
dfs_6(z, x);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifdef x
freopen("x.txt", "r", stdin);
#endif
cin >> n;
for(int i = 1; i < n; ++i) {
int x, y, c, d;
cin >> x >> y >> c >> d;
iv[x].push_back(y);
iv[y].push_back(x);
e[{x, y}] = c;
e[{y, x}] = d;
t0 += c + d;
}
dfs_0();
dfs_1();
spt[1] = {0, 1};
dfs_2();
dfs_4(dfs_3());
dfs_5();
int x = p2[2];
while(x) {
t5[x] = 1;
x = p5[x];
}
dfs_6();
for(int i = 1; i <= n; ++i) {
m5.push_back(array<ll, 2>{is5[i], i});
}
sort(all(m5));
reverse(all(m5));
u5 = vector<ll>(n + 1);
for(auto x : m5) {
int y = x[1];
int iy = y;
while(!t5[y]) {
u5[iy] += e[{p5[y], y}];
t5[y] = 1;
y = p5[y];
}
}
u5c = u5;
sort(1 + all(u5c));
fp[1] = t0 - p1[0];
fp[2] = t0 - p2[0];
for(int i = n; i >= 3; --i) {
fp[3 + n - i] = fp[2 + n - i] - u5c[i];
}
int q;
cin >> q;
for(int i = 0; i < q; ++i) {
int x;
cin >> x;
cout << fp[x] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19100 KB |
Output is correct |
2 |
Correct |
10 ms |
19068 KB |
Output is correct |
3 |
Correct |
10 ms |
19148 KB |
Output is correct |
4 |
Correct |
11 ms |
19020 KB |
Output is correct |
5 |
Correct |
11 ms |
19120 KB |
Output is correct |
6 |
Correct |
11 ms |
19036 KB |
Output is correct |
7 |
Correct |
11 ms |
19124 KB |
Output is correct |
8 |
Correct |
10 ms |
19100 KB |
Output is correct |
9 |
Correct |
10 ms |
19020 KB |
Output is correct |
10 |
Correct |
11 ms |
19032 KB |
Output is correct |
11 |
Correct |
12 ms |
19020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19020 KB |
Output is correct |
2 |
Correct |
1563 ms |
87516 KB |
Output is correct |
3 |
Correct |
1445 ms |
118780 KB |
Output is correct |
4 |
Correct |
1573 ms |
85872 KB |
Output is correct |
5 |
Correct |
1622 ms |
86776 KB |
Output is correct |
6 |
Correct |
1590 ms |
91072 KB |
Output is correct |
7 |
Correct |
1599 ms |
85168 KB |
Output is correct |
8 |
Correct |
1488 ms |
120052 KB |
Output is correct |
9 |
Correct |
1479 ms |
81376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19020 KB |
Output is correct |
2 |
Correct |
1558 ms |
87396 KB |
Output is correct |
3 |
Correct |
1428 ms |
123356 KB |
Output is correct |
4 |
Correct |
1555 ms |
86004 KB |
Output is correct |
5 |
Correct |
1548 ms |
86908 KB |
Output is correct |
6 |
Correct |
1610 ms |
91600 KB |
Output is correct |
7 |
Correct |
1526 ms |
82356 KB |
Output is correct |
8 |
Correct |
1587 ms |
106312 KB |
Output is correct |
9 |
Correct |
1520 ms |
80988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19100 KB |
Output is correct |
2 |
Correct |
10 ms |
19068 KB |
Output is correct |
3 |
Correct |
10 ms |
19148 KB |
Output is correct |
4 |
Correct |
11 ms |
19020 KB |
Output is correct |
5 |
Correct |
11 ms |
19120 KB |
Output is correct |
6 |
Correct |
11 ms |
19036 KB |
Output is correct |
7 |
Correct |
11 ms |
19124 KB |
Output is correct |
8 |
Correct |
10 ms |
19100 KB |
Output is correct |
9 |
Correct |
10 ms |
19020 KB |
Output is correct |
10 |
Correct |
11 ms |
19032 KB |
Output is correct |
11 |
Correct |
12 ms |
19020 KB |
Output is correct |
12 |
Correct |
10 ms |
19068 KB |
Output is correct |
13 |
Correct |
15 ms |
19764 KB |
Output is correct |
14 |
Correct |
15 ms |
20072 KB |
Output is correct |
15 |
Correct |
15 ms |
19796 KB |
Output is correct |
16 |
Correct |
15 ms |
19788 KB |
Output is correct |
17 |
Correct |
15 ms |
19776 KB |
Output is correct |
18 |
Correct |
16 ms |
19772 KB |
Output is correct |
19 |
Correct |
19 ms |
19924 KB |
Output is correct |
20 |
Correct |
15 ms |
19856 KB |
Output is correct |
21 |
Correct |
17 ms |
19772 KB |
Output is correct |
22 |
Correct |
18 ms |
19776 KB |
Output is correct |
23 |
Correct |
18 ms |
19916 KB |
Output is correct |
24 |
Correct |
15 ms |
19756 KB |
Output is correct |
25 |
Correct |
15 ms |
20172 KB |
Output is correct |
26 |
Correct |
14 ms |
19804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19020 KB |
Output is correct |
2 |
Correct |
1563 ms |
87516 KB |
Output is correct |
3 |
Correct |
1445 ms |
118780 KB |
Output is correct |
4 |
Correct |
1573 ms |
85872 KB |
Output is correct |
5 |
Correct |
1622 ms |
86776 KB |
Output is correct |
6 |
Correct |
1590 ms |
91072 KB |
Output is correct |
7 |
Correct |
1599 ms |
85168 KB |
Output is correct |
8 |
Correct |
1488 ms |
120052 KB |
Output is correct |
9 |
Correct |
1479 ms |
81376 KB |
Output is correct |
10 |
Correct |
10 ms |
19020 KB |
Output is correct |
11 |
Correct |
1558 ms |
87396 KB |
Output is correct |
12 |
Correct |
1428 ms |
123356 KB |
Output is correct |
13 |
Correct |
1555 ms |
86004 KB |
Output is correct |
14 |
Correct |
1548 ms |
86908 KB |
Output is correct |
15 |
Correct |
1610 ms |
91600 KB |
Output is correct |
16 |
Correct |
1526 ms |
82356 KB |
Output is correct |
17 |
Correct |
1587 ms |
106312 KB |
Output is correct |
18 |
Correct |
1520 ms |
80988 KB |
Output is correct |
19 |
Correct |
9 ms |
19020 KB |
Output is correct |
20 |
Correct |
1600 ms |
87344 KB |
Output is correct |
21 |
Correct |
1420 ms |
123796 KB |
Output is correct |
22 |
Correct |
1537 ms |
85868 KB |
Output is correct |
23 |
Correct |
1551 ms |
87372 KB |
Output is correct |
24 |
Correct |
1522 ms |
86152 KB |
Output is correct |
25 |
Correct |
1581 ms |
87560 KB |
Output is correct |
26 |
Correct |
1600 ms |
86012 KB |
Output is correct |
27 |
Correct |
1614 ms |
86816 KB |
Output is correct |
28 |
Correct |
1563 ms |
90924 KB |
Output is correct |
29 |
Correct |
1547 ms |
88204 KB |
Output is correct |
30 |
Correct |
1502 ms |
85252 KB |
Output is correct |
31 |
Correct |
1631 ms |
84452 KB |
Output is correct |
32 |
Correct |
1544 ms |
107748 KB |
Output is correct |
33 |
Correct |
1475 ms |
81520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19100 KB |
Output is correct |
2 |
Correct |
10 ms |
19068 KB |
Output is correct |
3 |
Correct |
10 ms |
19148 KB |
Output is correct |
4 |
Correct |
11 ms |
19020 KB |
Output is correct |
5 |
Correct |
11 ms |
19120 KB |
Output is correct |
6 |
Correct |
11 ms |
19036 KB |
Output is correct |
7 |
Correct |
11 ms |
19124 KB |
Output is correct |
8 |
Correct |
10 ms |
19100 KB |
Output is correct |
9 |
Correct |
10 ms |
19020 KB |
Output is correct |
10 |
Correct |
11 ms |
19032 KB |
Output is correct |
11 |
Correct |
12 ms |
19020 KB |
Output is correct |
12 |
Correct |
13 ms |
19020 KB |
Output is correct |
13 |
Correct |
1563 ms |
87516 KB |
Output is correct |
14 |
Correct |
1445 ms |
118780 KB |
Output is correct |
15 |
Correct |
1573 ms |
85872 KB |
Output is correct |
16 |
Correct |
1622 ms |
86776 KB |
Output is correct |
17 |
Correct |
1590 ms |
91072 KB |
Output is correct |
18 |
Correct |
1599 ms |
85168 KB |
Output is correct |
19 |
Correct |
1488 ms |
120052 KB |
Output is correct |
20 |
Correct |
1479 ms |
81376 KB |
Output is correct |
21 |
Correct |
10 ms |
19020 KB |
Output is correct |
22 |
Correct |
1558 ms |
87396 KB |
Output is correct |
23 |
Correct |
1428 ms |
123356 KB |
Output is correct |
24 |
Correct |
1555 ms |
86004 KB |
Output is correct |
25 |
Correct |
1548 ms |
86908 KB |
Output is correct |
26 |
Correct |
1610 ms |
91600 KB |
Output is correct |
27 |
Correct |
1526 ms |
82356 KB |
Output is correct |
28 |
Correct |
1587 ms |
106312 KB |
Output is correct |
29 |
Correct |
1520 ms |
80988 KB |
Output is correct |
30 |
Correct |
10 ms |
19068 KB |
Output is correct |
31 |
Correct |
15 ms |
19764 KB |
Output is correct |
32 |
Correct |
15 ms |
20072 KB |
Output is correct |
33 |
Correct |
15 ms |
19796 KB |
Output is correct |
34 |
Correct |
15 ms |
19788 KB |
Output is correct |
35 |
Correct |
15 ms |
19776 KB |
Output is correct |
36 |
Correct |
16 ms |
19772 KB |
Output is correct |
37 |
Correct |
19 ms |
19924 KB |
Output is correct |
38 |
Correct |
15 ms |
19856 KB |
Output is correct |
39 |
Correct |
17 ms |
19772 KB |
Output is correct |
40 |
Correct |
18 ms |
19776 KB |
Output is correct |
41 |
Correct |
18 ms |
19916 KB |
Output is correct |
42 |
Correct |
15 ms |
19756 KB |
Output is correct |
43 |
Correct |
15 ms |
20172 KB |
Output is correct |
44 |
Correct |
14 ms |
19804 KB |
Output is correct |
45 |
Correct |
9 ms |
19020 KB |
Output is correct |
46 |
Correct |
1600 ms |
87344 KB |
Output is correct |
47 |
Correct |
1420 ms |
123796 KB |
Output is correct |
48 |
Correct |
1537 ms |
85868 KB |
Output is correct |
49 |
Correct |
1551 ms |
87372 KB |
Output is correct |
50 |
Correct |
1522 ms |
86152 KB |
Output is correct |
51 |
Correct |
1581 ms |
87560 KB |
Output is correct |
52 |
Correct |
1600 ms |
86012 KB |
Output is correct |
53 |
Correct |
1614 ms |
86816 KB |
Output is correct |
54 |
Correct |
1563 ms |
90924 KB |
Output is correct |
55 |
Correct |
1547 ms |
88204 KB |
Output is correct |
56 |
Correct |
1502 ms |
85252 KB |
Output is correct |
57 |
Correct |
1631 ms |
84452 KB |
Output is correct |
58 |
Correct |
1544 ms |
107748 KB |
Output is correct |
59 |
Correct |
1475 ms |
81520 KB |
Output is correct |
60 |
Correct |
10 ms |
19108 KB |
Output is correct |
61 |
Correct |
1598 ms |
89924 KB |
Output is correct |
62 |
Correct |
1475 ms |
120556 KB |
Output is correct |
63 |
Correct |
1650 ms |
88600 KB |
Output is correct |
64 |
Correct |
1629 ms |
90148 KB |
Output is correct |
65 |
Correct |
1618 ms |
88492 KB |
Output is correct |
66 |
Correct |
1632 ms |
90056 KB |
Output is correct |
67 |
Correct |
1669 ms |
88564 KB |
Output is correct |
68 |
Correct |
1656 ms |
89676 KB |
Output is correct |
69 |
Correct |
1717 ms |
93060 KB |
Output is correct |
70 |
Correct |
1767 ms |
91312 KB |
Output is correct |
71 |
Correct |
1715 ms |
87964 KB |
Output is correct |
72 |
Correct |
1929 ms |
88204 KB |
Output is correct |
73 |
Correct |
1752 ms |
107276 KB |
Output is correct |
74 |
Correct |
1578 ms |
85520 KB |
Output is correct |