Skip to content

Commit 1c3d822

Browse files
Merge pull request #1 from ituacm/solution-review
AmongUs, Area 44 and Area61 reviewed
2 parents 79dcf58 + d855dcf commit 1c3d822

File tree

3 files changed

+79
-74
lines changed

3 files changed

+79
-74
lines changed

Among Us/solution.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ int main() {
2626
grid[i][j]--;
2727
}
2828
}
29+
// If starting cell or ending cell is blocked, the answer will be 0
30+
if (grid[0][0] == -1 || grid[r - 1][c - 1] == -1) {
31+
cout << "0\n";
32+
return 0;
33+
}
34+
2935
// Initialize starting cell as 1
3036
grid[0][0] = 1;
3137

@@ -37,7 +43,7 @@ int main() {
3743
vents[row2 * c + col2].push_back(row1 * c + col1);
3844
}
3945

40-
// iterate all cells
46+
// iterate over all cells
4147
// 0 1 ... (c - 1)
4248
// c (c + 1) ... (2c - 1)
4349
// ...
@@ -54,7 +60,7 @@ int main() {
5460
grid[row][col] += grid[row - 1][col];
5561
grid[row][col] %= MOD;
5662
}
57-
// if there is a left cell and if it is unblocked add it
63+
// if there is a cell at left and if it is unblocked add it
5864
if (col > 0 && grid[row][col - 1] != -1) {
5965
grid[row][col] += grid[row][col - 1];
6066
grid[row][col] %= MOD;
@@ -63,9 +69,9 @@ int main() {
6369
// take vent entrances which are ended with cell i = [row, col]
6470
// add the total ways of reaching entrance to the exit
6571
for (auto vent : vents[i]) {
66-
int vent_col = vent % c;
67-
int vent_row = vent / c;
68-
grid[row][col] += grid[vent_row][vent_col];
72+
int ventCol = vent % c;
73+
int ventRow = vent / c;
74+
grid[row][col] += grid[ventRow][ventCol];
6975
grid[row][col] %= MOD;
7076
}
7177
}

Area 44/solution.cpp

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ long long parent(long long node, vector<long long>& ancestor) {
1212
return ancestor[node] = parent(ancestor[node], ancestor);
1313
}
1414

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) {
1616
long long p1 = parent(node1, ancestor);
1717
long long p2 = parent(node2, ancestor);
1818
ancestor[p1] = p2;
@@ -22,41 +22,41 @@ bool find(long long node1, long long node2, vector<long long>& ancestor) {
2222
return parent(node1, ancestor) == parent(node2, ancestor);
2323
}
2424

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;
2727
for (auto edge : adjlist[node]) {
2828
if (edge.first != parent) {
29-
subtree_node_count += calculate_distance_all_node_pairs(
29+
subtreeNodeCount += calculateDistanceAllNodePairs(
3030
adjlist,
3131
edge.first,
3232
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);
3636
}
3737
}
38-
subtree_node_count += 1;
38+
subtreeNodeCount += 1;
3939

4040
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;
4545
}
4646

47-
return subtree_node_count;
47+
return subtreeNodeCount;
4848
}
4949

5050
int main() {
5151
ios_base::sync_with_stdio(false);
5252
cin.tie(NULL);
5353

5454
// 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;
5757

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++) {
6060
long long src, dest, cost;
6161
cin >> src >> dest >> cost;
6262
edgeList[i] = {cost, src, dest};
@@ -66,64 +66,63 @@ int main() {
6666
sort(edgeList.begin(), edgeList.end());
6767

6868
// initalize union find set
69-
vector<long long> ancestor(node_count);
69+
vector<long long> ancestor(nodeCount);
7070

7171
// make everybody their own parent
7272
iota(ancestor.begin(), ancestor.end(), 0);
7373

7474
// find the mst costs
75-
vector<vector<long long>> mst_results;
75+
vector<vector<long long>> mstResults;
7676
for (auto& edge : edgeList) {
7777
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]});
8080
}
8181
}
8282

83-
sort(mst_results.begin(), mst_results.end());
83+
sort(mstResults.begin(), mstResults.end());
8484

8585
// 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;
9596
}
9697

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+
100101
// get the queries
101-
long long query_count;
102-
cin >> query_count;
102+
long long queryCount;
103+
cin >> queryCount;
103104

104105
// for every query
105-
for (long long i = 0; i < query_count; i++) {
106+
for (long long i = 0; i < queryCount; i++) {
106107
long long query;
107108
cin >> query;
108109

109110
// 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;
111112

112113
// 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];
117118

118119
// 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);
122122
}
123-
// printf("Max Decrease = %lli\n", max_decrease_in_cost);
124123

125124
// print the result
126-
cout << summation_of_distances_between_all_node_pairs - max_decrease_in_cost << "\n";
125+
cout << summationOfDistances - maxDecreaseInCost << "\n";
127126
}
128127

129128
return 0;

Area 61/solution.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ long long parent(long long node, vector<long long>& ancestor) {
88
return ancestor[node] = parent(ancestor[node], ancestor);
99
}
1010

11-
void make_union(long long node1, long long node2, vector<long long>& ancestor) {
11+
void makeUnion(long long node1, long long node2, vector<long long>& ancestor) {
1212
long long p1 = parent(node1, ancestor);
1313
long long p2 = parent(node2, ancestor);
1414
ancestor[p1] = p2;
@@ -23,11 +23,11 @@ int main() {
2323
cin.tie(NULL);
2424

2525
// get input to a edge list
26-
long long node_count, edge_count;
27-
cin >> node_count >> edge_count;
26+
long long nodeCount, edgeCount;
27+
cin >> nodeCount >> edgeCount;
2828

29-
vector<vector<long long>> edgeList(edge_count);
30-
for (long long i = 0; i < edge_count; i++) {
29+
vector<vector<long long>> edgeList(edgeCount);
30+
for (long long i = 0; i < edgeCount; i++) {
3131
long long src, dest, cost;
3232
cin >> src >> dest >> cost;
3333
edgeList[i] = {cost, src, dest};
@@ -37,49 +37,49 @@ int main() {
3737
sort(edgeList.begin(), edgeList.end());
3838

3939
// initalize union find set
40-
vector<long long> ancestor(node_count);
40+
vector<long long> ancestor(nodeCount);
4141

4242
// make everybody their own parent
4343
iota(ancestor.begin(), ancestor.end(), 0);
4444

4545
// find the mst costs
46-
vector<long long> mst_results;
46+
vector<long long> mstResults;
4747
for (auto& edge : edgeList) {
4848
if (!find(edge[1], edge[2], ancestor)) {
49-
make_union(edge[1], edge[2], ancestor);
50-
mst_results.push_back(edge[0]);
49+
makeUnion(edge[1], edge[2], ancestor);
50+
mstResults.push_back(edge[0]);
5151
}
5252
}
5353

5454
// get the queries
55-
long long query_count;
56-
cin >> query_count;
57-
vector<long long> query_list(query_count);
55+
long long queryCount;
56+
cin >> queryCount;
57+
vector<long long> queryList(queryCount);
5858

59-
for (long long i = 0; i < query_count; i++) {
60-
cin >> query_list[i];
59+
for (long long i = 0; i < queryCount; i++) {
60+
cin >> queryList[i];
6161
}
6262

6363
// to use in binary search
64-
sort(mst_results.begin(), mst_results.end());
64+
sort(mstResults.begin(), mstResults.end());
6565

6666
// prefix sum for fast summation
67-
vector<long long> prefix(node_count - 1, 0);
67+
vector<long long> prefix(nodeCount - 1, 0);
6868

69-
for (long long i = 0; i < node_count - 1; i++) {
69+
for (long long i = 0; i < nodeCount - 1; i++) {
7070
if (i) {
71-
prefix[i] = mst_results[i] + prefix[i - 1];
71+
prefix[i] = mstResults[i] + prefix[i - 1];
7272
} else {
73-
prefix[i] = mst_results[i];
73+
prefix[i] = mstResults[i];
7474
}
7575
}
7676

7777
// print the binary search results
78-
for (long long i = 0; i < query_count; i++) {
79-
long long index = upper_bound(mst_results.begin(), mst_results.end(), query_list[i]) - mst_results.begin();
78+
for (long long i = 0; i < queryCount; i++) {
79+
long long index = upper_bound(mstResults.begin(), mstResults.end(), queryList[i]) - mstResults.begin();
8080
long long ans = 0;
8181
if (index) ans += prefix[index - 1];
82-
ans += (node_count - 1 - index) * query_list[i];
82+
ans += (nodeCount - 1 - index) * queryList[i];
8383
cout << ans << "\n";
8484
}
8585

0 commit comments

Comments
 (0)