//
// ROIGold.cpp
// Main calisma
//
// Created by Rakhman on 05/02/2019.
// Copyright © 2019 Rakhman. All rights reserved.
//
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iterator>
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define FOR(i, start, finish, k) for(int i = start; i <= finish; i += k)
const int MXN = 2e5 + 1;
const long long MNN = 1e6 + 200;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const int OO = 1e9 + 500;
typedef long long llong;
typedef unsigned long long ullong;
using namespace std;
void istxt(bool yes){
if(yes == 1){
freopen("balancing.in", "r", stdin);
//freopen("/Users/rakhmanabdirashov/Desktop/folder/Programming/Road2Master/Road2Master/e.txt", "w", stdout);
}else{
freopen("/Users/rakhmanabdirashov/Desktop/folder/Programming/Road2Master/Road2Master/input.txt", "r", stdin);
}
}
llong n, q, deq[MXN];
llong res[MXN];
llong sum, ans, roads;
vector<pair<llong, pair<llong, llong> > > g[MXN];
bool used[MXN];
void dfs(int v, int p){
for(int i = 0; i < g[v].size(); i++){
int to = g[v][i].F, cost = g[v][i].S.F;
if(to == p) {
roads += cost;
continue;
}
dfs(to, v);
}
}
void dfs2(int v, int p){
int cost = 0;
for(int i = 0; i < g[v].size(); i++){
int to = g[v][i].F;
if(to == p){
cost = g[v][i].S.F;
continue;
}
}
roads -= cost;
ans = max(ans, roads);
for(int i = 0; i < g[v].size(); i++){
int to = g[v][i].F;
if(to == p){
continue;
}
roads += g[v][i].S.F;
dfs2(to, v);
roads -= g[v][i].S.F;
}
roads += cost;
}
int main () {
ios;
//istxt(0);
cin >> n;
for(int i = 1; i < n; i++){
int u, v, a, b;
cin >> u >> v >> a >> b;
g[u].pb({v, {a, b}});
g[v].pb({u, {b, a}});
deq[v]++;
deq[u]++;
sum += a + b;
}
set<pair<llong, llong> > st;
for(int i = 1; i <= n; i++){
if(deq[i] == 1)
st.insert({g[i][0].S.S, i});
}
while(st.size() > 2){
llong v = st.begin() -> S;
llong cost = st.begin() -> F;
used[v] = 1;
st.erase(st.begin());
int batya = -1;
for(int i = 0; i < g[v].size(); i++){
if(used[g[v][i].F] == 0){
batya = g[v][i].F;
break;
}
}
if(batya != -1){
deq[batya]--;
if(deq[batya] == 1){
for(auto it : g[batya]){
if(used[it.F] == false){
st.insert({cost + it.S.S, batya});
break;
}
}
continue;
}
}
res[st.size()] = res[st.size() + 1] + cost;
}
dfs(1, 0);
ans = roads;
dfs2(1, 0);
res[1] = sum - ans;
cin >> q;
while(q--){
int x;
cin >> x;
cout << res[x] << nl;
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'void dfs(int, int)':
designated_cities.cpp:69:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs2(int, int)':
designated_cities.cpp:81:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
designated_cities.cpp:90:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:126:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
designated_cities.cpp: In function 'void istxt(bool)':
designated_cities.cpp:55:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("balancing.in", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("/Users/rakhmanabdirashov/Desktop/folder/Programming/Road2Master/Road2Master/input.txt", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
9 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
4984 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
4984 KB |
Output is correct |
10 |
Correct |
9 ms |
4984 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
511 ms |
25728 KB |
Output is correct |
3 |
Correct |
359 ms |
32504 KB |
Output is correct |
4 |
Correct |
455 ms |
25676 KB |
Output is correct |
5 |
Correct |
518 ms |
26280 KB |
Output is correct |
6 |
Correct |
493 ms |
27640 KB |
Output is correct |
7 |
Correct |
480 ms |
27684 KB |
Output is correct |
8 |
Correct |
365 ms |
33912 KB |
Output is correct |
9 |
Correct |
388 ms |
32028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4988 KB |
Output is correct |
2 |
Correct |
513 ms |
25848 KB |
Output is correct |
3 |
Correct |
366 ms |
40952 KB |
Output is correct |
4 |
Correct |
468 ms |
30712 KB |
Output is correct |
5 |
Correct |
511 ms |
32684 KB |
Output is correct |
6 |
Correct |
492 ms |
34296 KB |
Output is correct |
7 |
Correct |
428 ms |
37104 KB |
Output is correct |
8 |
Correct |
421 ms |
39032 KB |
Output is correct |
9 |
Correct |
398 ms |
38040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
9 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
4984 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
4984 KB |
Output is correct |
10 |
Correct |
9 ms |
4984 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
12 |
Correct |
8 ms |
5112 KB |
Output is correct |
13 |
Correct |
10 ms |
5368 KB |
Output is correct |
14 |
Correct |
10 ms |
5368 KB |
Output is correct |
15 |
Correct |
10 ms |
5368 KB |
Output is correct |
16 |
Correct |
10 ms |
5368 KB |
Output is correct |
17 |
Correct |
10 ms |
5368 KB |
Output is correct |
18 |
Correct |
10 ms |
5368 KB |
Output is correct |
19 |
Correct |
10 ms |
5368 KB |
Output is correct |
20 |
Correct |
10 ms |
5368 KB |
Output is correct |
21 |
Correct |
10 ms |
5368 KB |
Output is correct |
22 |
Correct |
10 ms |
5368 KB |
Output is correct |
23 |
Correct |
10 ms |
5368 KB |
Output is correct |
24 |
Correct |
10 ms |
5368 KB |
Output is correct |
25 |
Correct |
10 ms |
5368 KB |
Output is correct |
26 |
Correct |
10 ms |
5368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
511 ms |
25728 KB |
Output is correct |
3 |
Correct |
359 ms |
32504 KB |
Output is correct |
4 |
Correct |
455 ms |
25676 KB |
Output is correct |
5 |
Correct |
518 ms |
26280 KB |
Output is correct |
6 |
Correct |
493 ms |
27640 KB |
Output is correct |
7 |
Correct |
480 ms |
27684 KB |
Output is correct |
8 |
Correct |
365 ms |
33912 KB |
Output is correct |
9 |
Correct |
388 ms |
32028 KB |
Output is correct |
10 |
Correct |
7 ms |
4988 KB |
Output is correct |
11 |
Correct |
513 ms |
25848 KB |
Output is correct |
12 |
Correct |
366 ms |
40952 KB |
Output is correct |
13 |
Correct |
468 ms |
30712 KB |
Output is correct |
14 |
Correct |
511 ms |
32684 KB |
Output is correct |
15 |
Correct |
492 ms |
34296 KB |
Output is correct |
16 |
Correct |
428 ms |
37104 KB |
Output is correct |
17 |
Correct |
421 ms |
39032 KB |
Output is correct |
18 |
Correct |
398 ms |
38040 KB |
Output is correct |
19 |
Correct |
8 ms |
5112 KB |
Output is correct |
20 |
Correct |
514 ms |
32016 KB |
Output is correct |
21 |
Correct |
376 ms |
41264 KB |
Output is correct |
22 |
Correct |
458 ms |
30840 KB |
Output is correct |
23 |
Correct |
505 ms |
32504 KB |
Output is correct |
24 |
Correct |
495 ms |
31108 KB |
Output is correct |
25 |
Correct |
493 ms |
32248 KB |
Output is correct |
26 |
Correct |
506 ms |
31224 KB |
Output is correct |
27 |
Correct |
520 ms |
32612 KB |
Output is correct |
28 |
Correct |
490 ms |
34168 KB |
Output is correct |
29 |
Correct |
510 ms |
31992 KB |
Output is correct |
30 |
Correct |
490 ms |
32168 KB |
Output is correct |
31 |
Correct |
459 ms |
35132 KB |
Output is correct |
32 |
Correct |
407 ms |
39160 KB |
Output is correct |
33 |
Correct |
387 ms |
38428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
9 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
4984 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
4984 KB |
Output is correct |
10 |
Correct |
9 ms |
4984 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
12 |
Correct |
7 ms |
4984 KB |
Output is correct |
13 |
Correct |
511 ms |
25728 KB |
Output is correct |
14 |
Correct |
359 ms |
32504 KB |
Output is correct |
15 |
Correct |
455 ms |
25676 KB |
Output is correct |
16 |
Correct |
518 ms |
26280 KB |
Output is correct |
17 |
Correct |
493 ms |
27640 KB |
Output is correct |
18 |
Correct |
480 ms |
27684 KB |
Output is correct |
19 |
Correct |
365 ms |
33912 KB |
Output is correct |
20 |
Correct |
388 ms |
32028 KB |
Output is correct |
21 |
Correct |
7 ms |
4988 KB |
Output is correct |
22 |
Correct |
513 ms |
25848 KB |
Output is correct |
23 |
Correct |
366 ms |
40952 KB |
Output is correct |
24 |
Correct |
468 ms |
30712 KB |
Output is correct |
25 |
Correct |
511 ms |
32684 KB |
Output is correct |
26 |
Correct |
492 ms |
34296 KB |
Output is correct |
27 |
Correct |
428 ms |
37104 KB |
Output is correct |
28 |
Correct |
421 ms |
39032 KB |
Output is correct |
29 |
Correct |
398 ms |
38040 KB |
Output is correct |
30 |
Correct |
8 ms |
5112 KB |
Output is correct |
31 |
Correct |
10 ms |
5368 KB |
Output is correct |
32 |
Correct |
10 ms |
5368 KB |
Output is correct |
33 |
Correct |
10 ms |
5368 KB |
Output is correct |
34 |
Correct |
10 ms |
5368 KB |
Output is correct |
35 |
Correct |
10 ms |
5368 KB |
Output is correct |
36 |
Correct |
10 ms |
5368 KB |
Output is correct |
37 |
Correct |
10 ms |
5368 KB |
Output is correct |
38 |
Correct |
10 ms |
5368 KB |
Output is correct |
39 |
Correct |
10 ms |
5368 KB |
Output is correct |
40 |
Correct |
10 ms |
5368 KB |
Output is correct |
41 |
Correct |
10 ms |
5368 KB |
Output is correct |
42 |
Correct |
10 ms |
5368 KB |
Output is correct |
43 |
Correct |
10 ms |
5368 KB |
Output is correct |
44 |
Correct |
10 ms |
5368 KB |
Output is correct |
45 |
Correct |
8 ms |
5112 KB |
Output is correct |
46 |
Correct |
514 ms |
32016 KB |
Output is correct |
47 |
Correct |
376 ms |
41264 KB |
Output is correct |
48 |
Correct |
458 ms |
30840 KB |
Output is correct |
49 |
Correct |
505 ms |
32504 KB |
Output is correct |
50 |
Correct |
495 ms |
31108 KB |
Output is correct |
51 |
Correct |
493 ms |
32248 KB |
Output is correct |
52 |
Correct |
506 ms |
31224 KB |
Output is correct |
53 |
Correct |
520 ms |
32612 KB |
Output is correct |
54 |
Correct |
490 ms |
34168 KB |
Output is correct |
55 |
Correct |
510 ms |
31992 KB |
Output is correct |
56 |
Correct |
490 ms |
32168 KB |
Output is correct |
57 |
Correct |
459 ms |
35132 KB |
Output is correct |
58 |
Correct |
407 ms |
39160 KB |
Output is correct |
59 |
Correct |
387 ms |
38428 KB |
Output is correct |
60 |
Correct |
7 ms |
4984 KB |
Output is correct |
61 |
Correct |
567 ms |
34956 KB |
Output is correct |
62 |
Correct |
410 ms |
40696 KB |
Output is correct |
63 |
Correct |
501 ms |
33352 KB |
Output is correct |
64 |
Correct |
563 ms |
35016 KB |
Output is correct |
65 |
Correct |
542 ms |
33652 KB |
Output is correct |
66 |
Correct |
571 ms |
34900 KB |
Output is correct |
67 |
Correct |
580 ms |
33656 KB |
Output is correct |
68 |
Correct |
556 ms |
35248 KB |
Output is correct |
69 |
Correct |
536 ms |
36472 KB |
Output is correct |
70 |
Correct |
538 ms |
34168 KB |
Output is correct |
71 |
Correct |
514 ms |
34984 KB |
Output is correct |
72 |
Correct |
510 ms |
37668 KB |
Output is correct |
73 |
Correct |
479 ms |
40316 KB |
Output is correct |
74 |
Correct |
432 ms |
42520 KB |
Output is correct |