@@ -12,7 +12,7 @@ long long parent(long long node, vector<long long>& ancestor) {
12
12
return ancestor[node] = parent (ancestor[node], ancestor);
13
13
}
14
14
15
- void make_union (long long node1, long long node2, vector<long long >& ancestor) {
15
+ void makeUnion (long long node1, long long node2, vector<long long >& ancestor) {
16
16
long long p1 = parent (node1, ancestor);
17
17
long long p2 = parent (node2, ancestor);
18
18
ancestor[p1] = p2;
@@ -22,41 +22,41 @@ bool find(long long node1, long long node2, vector<long long>& ancestor) {
22
22
return parent (node1, ancestor) == parent (node2, ancestor);
23
23
}
24
24
25
- long long calculate_distance_all_node_pairs (unordered_map<long long , unordered_map<long long , long long >>& adjlist, long long node, long long parent, long long & summation_of_distances_between_all_node_pairs , unordered_map<long long , unordered_map<long long , long long >>& how_many_times_edge_used , long long total_node_count ) {
26
- long long subtree_node_count = 0 ;
25
+ long long calculateDistanceAllNodePairs (unordered_map<long long , unordered_map<long long , long long >>& adjlist, long long node, long long parent, long long & summationOfDistances , unordered_map<long long , unordered_map<long long , long long >>& howManyTimesUsed , long long totalNodeCount ) {
26
+ long long subtreeNodeCount = 0 ;
27
27
for (auto edge : adjlist[node]) {
28
28
if (edge.first != parent) {
29
- subtree_node_count += calculate_distance_all_node_pairs (
29
+ subtreeNodeCount += calculateDistanceAllNodePairs (
30
30
adjlist,
31
31
edge.first ,
32
32
node,
33
- summation_of_distances_between_all_node_pairs ,
34
- how_many_times_edge_used ,
35
- total_node_count );
33
+ summationOfDistances ,
34
+ howManyTimesUsed ,
35
+ totalNodeCount );
36
36
}
37
37
}
38
- subtree_node_count += 1 ;
38
+ subtreeNodeCount += 1 ;
39
39
40
40
if (parent != -1 ) {
41
- long long how_many_times_used = (subtree_node_count ) * (total_node_count - subtree_node_count );
42
- summation_of_distances_between_all_node_pairs += how_many_times_used * adjlist[node][parent];
43
- how_many_times_edge_used [node][parent] = how_many_times_used;
44
- how_many_times_edge_used [parent][node] = how_many_times_used;
41
+ long long how_many_times_used = (subtreeNodeCount ) * (totalNodeCount - subtreeNodeCount );
42
+ summationOfDistances += how_many_times_used * adjlist[node][parent];
43
+ howManyTimesUsed [node][parent] = how_many_times_used;
44
+ howManyTimesUsed [parent][node] = how_many_times_used;
45
45
}
46
46
47
- return subtree_node_count ;
47
+ return subtreeNodeCount ;
48
48
}
49
49
50
50
int main () {
51
51
ios_base::sync_with_stdio (false );
52
52
cin.tie (NULL );
53
53
54
54
// get input to a edge list
55
- long long node_count, edge_count ;
56
- cin >> node_count >> edge_count ;
55
+ long long nodeCount, edgeCount ;
56
+ cin >> nodeCount >> edgeCount ;
57
57
58
- vector<vector<long long >> edgeList (edge_count );
59
- for (long long i = 0 ; i < edge_count ; i++) {
58
+ vector<vector<long long >> edgeList (edgeCount );
59
+ for (long long i = 0 ; i < edgeCount ; i++) {
60
60
long long src, dest, cost;
61
61
cin >> src >> dest >> cost;
62
62
edgeList[i] = {cost, src, dest};
@@ -66,64 +66,63 @@ int main() {
66
66
sort (edgeList.begin (), edgeList.end ());
67
67
68
68
// initalize union find set
69
- vector<long long > ancestor (node_count );
69
+ vector<long long > ancestor (nodeCount );
70
70
71
71
// make everybody their own parent
72
72
iota (ancestor.begin (), ancestor.end (), 0 );
73
73
74
74
// find the mst costs
75
- vector<vector<long long >> mst_results ;
75
+ vector<vector<long long >> mstResults ;
76
76
for (auto & edge : edgeList) {
77
77
if (!find (edge[1 ], edge[2 ], ancestor)) {
78
- make_union (edge[1 ], edge[2 ], ancestor);
79
- mst_results .push_back ({edge[0 ], edge[1 ], edge[2 ]});
78
+ makeUnion (edge[1 ], edge[2 ], ancestor);
79
+ mstResults .push_back ({edge[0 ], edge[1 ], edge[2 ]});
80
80
}
81
81
}
82
82
83
- sort (mst_results .begin (), mst_results .end ());
83
+ sort (mstResults .begin (), mstResults .end ());
84
84
85
85
// assign id's to the edges to find them quickly later.
86
- unordered_map<long long , unordered_map<long long , long long >> mst_adjlist;
87
- unordered_map<long long , unordered_map<long long , long long >> how_many_times_edge_used;
88
-
89
- for (auto mst_result : mst_results) {
90
- long long cost = mst_result[0 ];
91
- long long adj1 = mst_result[1 ];
92
- long long adj2 = mst_result[2 ];
93
- mst_adjlist[adj1][adj2] = cost;
94
- mst_adjlist[adj2][adj1] = cost;
86
+ unordered_map<long long , unordered_map<long long , long long >> mstAdjList;
87
+ // holds the number of usage between all node pairs for each edge
88
+ unordered_map<long long , unordered_map<long long , long long >> howManyTimesUsed;
89
+
90
+ for (auto mstResult : mstResults) {
91
+ long long cost = mstResult[0 ];
92
+ long long adj1 = mstResult[1 ];
93
+ long long adj2 = mstResult[2 ];
94
+ mstAdjList[adj1][adj2] = cost;
95
+ mstAdjList[adj2][adj1] = cost;
95
96
}
96
97
97
- long long summation_of_distances_between_all_node_pairs = 0 ;
98
- calculate_distance_all_node_pairs (mst_adjlist , 0 , -1 , summation_of_distances_between_all_node_pairs, how_many_times_edge_used, node_count );
99
- // printf("summation_of_distances_between_all_node_pairs = %lli\n", summation_of_distances_between_all_node_pairs);
98
+ long long summationOfDistances = 0 ; // summation of distances between all node pairs
99
+ calculateDistanceAllNodePairs (mstAdjList , 0 , -1 , summationOfDistances, howManyTimesUsed, nodeCount );
100
+
100
101
// get the queries
101
- long long query_count ;
102
- cin >> query_count ;
102
+ long long queryCount ;
103
+ cin >> queryCount ;
103
104
104
105
// for every query
105
- for (long long i = 0 ; i < query_count ; i++) {
106
+ for (long long i = 0 ; i < queryCount ; i++) {
106
107
long long query;
107
108
cin >> query;
108
109
109
110
// find the maximum effect on cost, at least it should be 0.
110
- long long max_decrease_in_cost = 0 ;
111
+ long long maxDecreaseInCost = 0 ;
111
112
112
113
// look up to the all edges
113
- for (auto mst_result : mst_results ) {
114
- long long edge_cost = mst_result [0 ];
115
- long long adj1 = mst_result [1 ];
116
- long long adj2 = mst_result [2 ];
114
+ for (auto mstResult : mstResults ) {
115
+ long long edgeCost = mstResult [0 ];
116
+ long long adj1 = mstResult [1 ];
117
+ long long adj2 = mstResult [2 ];
117
118
118
119
// calculate the value and check the max
119
- // printf("Edge_Cost = %lli, Times = %lli\n", edge_cost, how_many_times_edge_used[adj1][adj2]);
120
- long long decrease_in_cost = (edge_cost - query) * how_many_times_edge_used[adj1][adj2];
121
- max_decrease_in_cost = max (max_decrease_in_cost, decrease_in_cost);
120
+ long long decreaseInCost = (edgeCost - query) * howManyTimesUsed[adj1][adj2];
121
+ maxDecreaseInCost = max (maxDecreaseInCost, decreaseInCost);
122
122
}
123
- // printf("Max Decrease = %lli\n", max_decrease_in_cost);
124
123
125
124
// print the result
126
- cout << summation_of_distances_between_all_node_pairs - max_decrease_in_cost << " \n " ;
125
+ cout << summationOfDistances - maxDecreaseInCost << " \n " ;
127
126
}
128
127
129
128
return 0 ;
0 commit comments