#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, int>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = 14;
const ll inf = 1e17;
const int base = 350;
const ll mod = 998244353;
int n, t, m, k, q, b[N], c[N], deg[N];
ll ans, tong, a[N], d[N];
vector<int> gr[N], ask[N];
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
string s;
struct BIT
{
int n;
vector<int> fe;
BIT(int _n)
{
n = _n;
fe.resize(n+1, 0);
}
void add(int id, int x)
{
for(; id <= n; id += id & -id)fe[id] += x;
}
int get(int id)
{
int total = 0;
for(; id; id -= id & -id)total += fe[id];
return total;
}
};
struct node
{
int v, c, d;
node(){}
node(int _v, int _c, int _d)
{
v = _v;
c = _c;
d = _d;
}
};
vector<node> vi, adj[N];
void dfs(int u, int p = 0)
{
for(node x: adj[u])
{
if(x.v == p)continue;
dfs(x.v, u);
d[u] += d[x.v] + x.c;
}
}
void dfs1(int u, ll sum, int p = 0)
{
sum += d[u];
a[1] = min(a[1], sum);
for(node x: adj[u])
{
if(x.v == p)continue;
dfs1(x.v, sum-d[x.v]-x.c+x.d, u);
}
}
void sol()
{
cin >> n;
for(int i = 1; i < n; i ++)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
adj[a].pb(node(b, c, d));
adj[b].pb(node(a, d, c));
++deg[a];
++deg[b];
}
a[1] = inf;
dfs(1);
dfs1(1, 0);
priority_queue< pll, vector<pll>, greater<pll> > pq;
for(int i = 1; i <= n; i ++)
{
if(deg[i] == 1)
{
pq.push({adj[i][0].d, i});
}
}
while(pq.size() > 2)
{
pll u = pq.top();
pq.pop();
int cnt = 0;
node par = node(-1, 0, 0);
//cout << u.se <<" "<<u.fi<<'\n';
for(node x: adj[u.se])
{
if(deg[x.v] > 0)
{
--deg[x.v];
--deg[u.se];
++cnt;
if(deg[x.v] == 1)par = x;
else
{
//cout <<"res "<< pq.size() <<" "<<a[pq.size()+1]+u.fi<<'\n';
a[pq.size()] = a[pq.size()+1]+u.fi;
}
}
}
//assert(cnt <= 1);
//cout << "par " << par.v<<" "<<par.d << '\n';
if(par.v != -1)
for(node x: adj[par.v])
if(deg[x.v] > 0)
pq.push({u.fi+x.d, par.v});
}
cin >> m;
while(m -- > 0)
{
cin >> k;
cout << a[k] << '\n';
}
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
return 0;
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:140:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:141:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14416 KB |
Output is correct |
3 |
Correct |
8 ms |
14336 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14408 KB |
Output is correct |
6 |
Correct |
8 ms |
14408 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14380 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14416 KB |
Output is correct |
2 |
Correct |
266 ms |
27388 KB |
Output is correct |
3 |
Correct |
229 ms |
36108 KB |
Output is correct |
4 |
Correct |
254 ms |
27372 KB |
Output is correct |
5 |
Correct |
267 ms |
27764 KB |
Output is correct |
6 |
Correct |
260 ms |
29152 KB |
Output is correct |
7 |
Correct |
233 ms |
28996 KB |
Output is correct |
8 |
Correct |
216 ms |
37008 KB |
Output is correct |
9 |
Correct |
193 ms |
30792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
280 ms |
27452 KB |
Output is correct |
3 |
Correct |
251 ms |
44208 KB |
Output is correct |
4 |
Correct |
268 ms |
32192 KB |
Output is correct |
5 |
Correct |
303 ms |
33944 KB |
Output is correct |
6 |
Correct |
268 ms |
35512 KB |
Output is correct |
7 |
Correct |
236 ms |
37736 KB |
Output is correct |
8 |
Correct |
247 ms |
40420 KB |
Output is correct |
9 |
Correct |
192 ms |
36628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14416 KB |
Output is correct |
3 |
Correct |
8 ms |
14336 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14408 KB |
Output is correct |
6 |
Correct |
8 ms |
14408 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14380 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14412 KB |
Output is correct |
13 |
Correct |
10 ms |
14668 KB |
Output is correct |
14 |
Correct |
9 ms |
14680 KB |
Output is correct |
15 |
Correct |
9 ms |
14540 KB |
Output is correct |
16 |
Correct |
9 ms |
14540 KB |
Output is correct |
17 |
Correct |
9 ms |
14552 KB |
Output is correct |
18 |
Correct |
9 ms |
14540 KB |
Output is correct |
19 |
Correct |
10 ms |
14560 KB |
Output is correct |
20 |
Correct |
10 ms |
14568 KB |
Output is correct |
21 |
Correct |
10 ms |
14668 KB |
Output is correct |
22 |
Correct |
9 ms |
14540 KB |
Output is correct |
23 |
Correct |
10 ms |
14668 KB |
Output is correct |
24 |
Correct |
9 ms |
14684 KB |
Output is correct |
25 |
Correct |
9 ms |
14676 KB |
Output is correct |
26 |
Correct |
9 ms |
14592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14416 KB |
Output is correct |
2 |
Correct |
266 ms |
27388 KB |
Output is correct |
3 |
Correct |
229 ms |
36108 KB |
Output is correct |
4 |
Correct |
254 ms |
27372 KB |
Output is correct |
5 |
Correct |
267 ms |
27764 KB |
Output is correct |
6 |
Correct |
260 ms |
29152 KB |
Output is correct |
7 |
Correct |
233 ms |
28996 KB |
Output is correct |
8 |
Correct |
216 ms |
37008 KB |
Output is correct |
9 |
Correct |
193 ms |
30792 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
280 ms |
27452 KB |
Output is correct |
12 |
Correct |
251 ms |
44208 KB |
Output is correct |
13 |
Correct |
268 ms |
32192 KB |
Output is correct |
14 |
Correct |
303 ms |
33944 KB |
Output is correct |
15 |
Correct |
268 ms |
35512 KB |
Output is correct |
16 |
Correct |
236 ms |
37736 KB |
Output is correct |
17 |
Correct |
247 ms |
40420 KB |
Output is correct |
18 |
Correct |
192 ms |
36628 KB |
Output is correct |
19 |
Correct |
8 ms |
14412 KB |
Output is correct |
20 |
Correct |
270 ms |
33564 KB |
Output is correct |
21 |
Correct |
214 ms |
44356 KB |
Output is correct |
22 |
Correct |
257 ms |
32216 KB |
Output is correct |
23 |
Correct |
280 ms |
33516 KB |
Output is correct |
24 |
Correct |
281 ms |
32488 KB |
Output is correct |
25 |
Correct |
275 ms |
33504 KB |
Output is correct |
26 |
Correct |
272 ms |
32448 KB |
Output is correct |
27 |
Correct |
259 ms |
33600 KB |
Output is correct |
28 |
Correct |
272 ms |
35384 KB |
Output is correct |
29 |
Correct |
273 ms |
33484 KB |
Output is correct |
30 |
Correct |
260 ms |
33420 KB |
Output is correct |
31 |
Correct |
232 ms |
36464 KB |
Output is correct |
32 |
Correct |
249 ms |
40892 KB |
Output is correct |
33 |
Correct |
208 ms |
36968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14416 KB |
Output is correct |
3 |
Correct |
8 ms |
14336 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14408 KB |
Output is correct |
6 |
Correct |
8 ms |
14408 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14380 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
7 ms |
14416 KB |
Output is correct |
13 |
Correct |
266 ms |
27388 KB |
Output is correct |
14 |
Correct |
229 ms |
36108 KB |
Output is correct |
15 |
Correct |
254 ms |
27372 KB |
Output is correct |
16 |
Correct |
267 ms |
27764 KB |
Output is correct |
17 |
Correct |
260 ms |
29152 KB |
Output is correct |
18 |
Correct |
233 ms |
28996 KB |
Output is correct |
19 |
Correct |
216 ms |
37008 KB |
Output is correct |
20 |
Correct |
193 ms |
30792 KB |
Output is correct |
21 |
Correct |
8 ms |
14412 KB |
Output is correct |
22 |
Correct |
280 ms |
27452 KB |
Output is correct |
23 |
Correct |
251 ms |
44208 KB |
Output is correct |
24 |
Correct |
268 ms |
32192 KB |
Output is correct |
25 |
Correct |
303 ms |
33944 KB |
Output is correct |
26 |
Correct |
268 ms |
35512 KB |
Output is correct |
27 |
Correct |
236 ms |
37736 KB |
Output is correct |
28 |
Correct |
247 ms |
40420 KB |
Output is correct |
29 |
Correct |
192 ms |
36628 KB |
Output is correct |
30 |
Correct |
8 ms |
14412 KB |
Output is correct |
31 |
Correct |
10 ms |
14668 KB |
Output is correct |
32 |
Correct |
9 ms |
14680 KB |
Output is correct |
33 |
Correct |
9 ms |
14540 KB |
Output is correct |
34 |
Correct |
9 ms |
14540 KB |
Output is correct |
35 |
Correct |
9 ms |
14552 KB |
Output is correct |
36 |
Correct |
9 ms |
14540 KB |
Output is correct |
37 |
Correct |
10 ms |
14560 KB |
Output is correct |
38 |
Correct |
10 ms |
14568 KB |
Output is correct |
39 |
Correct |
10 ms |
14668 KB |
Output is correct |
40 |
Correct |
9 ms |
14540 KB |
Output is correct |
41 |
Correct |
10 ms |
14668 KB |
Output is correct |
42 |
Correct |
9 ms |
14684 KB |
Output is correct |
43 |
Correct |
9 ms |
14676 KB |
Output is correct |
44 |
Correct |
9 ms |
14592 KB |
Output is correct |
45 |
Correct |
8 ms |
14412 KB |
Output is correct |
46 |
Correct |
270 ms |
33564 KB |
Output is correct |
47 |
Correct |
214 ms |
44356 KB |
Output is correct |
48 |
Correct |
257 ms |
32216 KB |
Output is correct |
49 |
Correct |
280 ms |
33516 KB |
Output is correct |
50 |
Correct |
281 ms |
32488 KB |
Output is correct |
51 |
Correct |
275 ms |
33504 KB |
Output is correct |
52 |
Correct |
272 ms |
32448 KB |
Output is correct |
53 |
Correct |
259 ms |
33600 KB |
Output is correct |
54 |
Correct |
272 ms |
35384 KB |
Output is correct |
55 |
Correct |
273 ms |
33484 KB |
Output is correct |
56 |
Correct |
260 ms |
33420 KB |
Output is correct |
57 |
Correct |
232 ms |
36464 KB |
Output is correct |
58 |
Correct |
249 ms |
40892 KB |
Output is correct |
59 |
Correct |
208 ms |
36968 KB |
Output is correct |
60 |
Correct |
10 ms |
14360 KB |
Output is correct |
61 |
Correct |
318 ms |
36072 KB |
Output is correct |
62 |
Correct |
253 ms |
43784 KB |
Output is correct |
63 |
Correct |
310 ms |
34908 KB |
Output is correct |
64 |
Correct |
303 ms |
36032 KB |
Output is correct |
65 |
Correct |
314 ms |
34736 KB |
Output is correct |
66 |
Correct |
296 ms |
36036 KB |
Output is correct |
67 |
Correct |
303 ms |
34880 KB |
Output is correct |
68 |
Correct |
309 ms |
36412 KB |
Output is correct |
69 |
Correct |
305 ms |
37572 KB |
Output is correct |
70 |
Correct |
300 ms |
35356 KB |
Output is correct |
71 |
Correct |
300 ms |
36192 KB |
Output is correct |
72 |
Correct |
275 ms |
38676 KB |
Output is correct |
73 |
Correct |
275 ms |
41952 KB |
Output is correct |
74 |
Correct |
234 ms |
41228 KB |
Output is correct |