#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
typedef long double ld;
using namespace std;
struct edge
{
int u, v;
ll cuv, cvu, c2;
};
vector<vector<edge> > g;
vector<ll> dps, dp, d, v;
void dfs1a(int u, int p = -1)
{
for (edge e : g[u]) if (e.v != p)
{
dfs1a(e.v, u);
dps[u] += dps[e.v] + e.cvu;
}
}
void dfs1b(int u, int p = -1, ll nad = 0)
{
dp[u] = dps[u] + nad;
for (edge e : g[u]) if (e.v != p)
{
dfs1b(e.v, u, nad + dps[u] - dps[e.v] - e.cvu + e.cuv);
}
}
ll dfs(int u, int p = -1)
{
vector<ll> my;
for (edge e : g[u]) if (e.v != p)
{
my.push_back(dfs(e.v, u) + e.cuv);
}
sort(my.begin(), my.end(), greater<ll>());
for (int i = 1; i < my.size(); i++) v.push_back(my[i]);
if (my.empty()) my.push_back(0);
return my[0];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
g.assign(n, {}), dps.assign(n, 0), dp.assign(n, 0), d.assign(n, 0), v.clear();
ll sum = 0;
for (int i = 0; i < n - 1; i++)
{
edge e;
cin >> e.u >> e.v >> e.cuv >> e.cvu;
e.u--, e.v--, e.c2 = e.cuv + e.cvu;
g[e.u].push_back(e);
swap(e.u, e.v), swap(e.cuv, e.cvu);
g[e.u].push_back(e);
sum += e.c2;
}
dfs1a(0);
dfs1b(0);
vector<ll> ans(n + 1, 0);
ans[1] = *max_element(dp.begin(), dp.end());
bool f = false;
int q;
cin >> q;
while (q--)
{
int x;
cin >> x;
if (x == 1) cout << (sum - ans[x]) << "\n";
else
{
if (!f)
{
for (int a = 0; a < n; a++) if (g[a].size() == 1)
{
v.clear();
v.push_back(dfs(a));
sort(v.begin(), v.end(), greater<ll>());
ll s = dp[a];
for (int i = 0; i < v.size(); i++) s += v[i], ans[i + 2] = max(ans[i + 2], s);
}
for (int i = 1; i <= n; i++) ans[i] = max(ans[i], ans[i - 1]);
f = true;
}
cout << (sum - ans[x]) << "\n";
}
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'll dfs(int, int)':
designated_cities.cpp:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 1; i < my.size(); i++) v.push_back(my[i]);
| ~~^~~~~~~~~~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:94:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int i = 0; i < v.size(); i++) s += v[i], ans[i + 2] = max(ans[i + 2], s);
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
145 ms |
34832 KB |
Output is correct |
3 |
Correct |
175 ms |
47860 KB |
Output is correct |
4 |
Correct |
122 ms |
33480 KB |
Output is correct |
5 |
Correct |
113 ms |
34752 KB |
Output is correct |
6 |
Correct |
127 ms |
36948 KB |
Output is correct |
7 |
Correct |
129 ms |
33980 KB |
Output is correct |
8 |
Correct |
194 ms |
47956 KB |
Output is correct |
9 |
Correct |
85 ms |
33452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2093 ms |
36136 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
92 ms |
808 KB |
Output is correct |
14 |
Correct |
2 ms |
856 KB |
Output is correct |
15 |
Correct |
85 ms |
600 KB |
Output is correct |
16 |
Correct |
91 ms |
604 KB |
Output is correct |
17 |
Correct |
94 ms |
848 KB |
Output is correct |
18 |
Correct |
92 ms |
812 KB |
Output is correct |
19 |
Correct |
100 ms |
604 KB |
Output is correct |
20 |
Correct |
101 ms |
828 KB |
Output is correct |
21 |
Correct |
93 ms |
600 KB |
Output is correct |
22 |
Correct |
72 ms |
600 KB |
Output is correct |
23 |
Correct |
155 ms |
820 KB |
Output is correct |
24 |
Correct |
302 ms |
856 KB |
Output is correct |
25 |
Correct |
25 ms |
860 KB |
Output is correct |
26 |
Correct |
430 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
145 ms |
34832 KB |
Output is correct |
3 |
Correct |
175 ms |
47860 KB |
Output is correct |
4 |
Correct |
122 ms |
33480 KB |
Output is correct |
5 |
Correct |
113 ms |
34752 KB |
Output is correct |
6 |
Correct |
127 ms |
36948 KB |
Output is correct |
7 |
Correct |
129 ms |
33980 KB |
Output is correct |
8 |
Correct |
194 ms |
47956 KB |
Output is correct |
9 |
Correct |
85 ms |
33452 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Execution timed out |
2093 ms |
36136 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
145 ms |
34832 KB |
Output is correct |
14 |
Correct |
175 ms |
47860 KB |
Output is correct |
15 |
Correct |
122 ms |
33480 KB |
Output is correct |
16 |
Correct |
113 ms |
34752 KB |
Output is correct |
17 |
Correct |
127 ms |
36948 KB |
Output is correct |
18 |
Correct |
129 ms |
33980 KB |
Output is correct |
19 |
Correct |
194 ms |
47956 KB |
Output is correct |
20 |
Correct |
85 ms |
33452 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Execution timed out |
2093 ms |
36136 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |