-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily11.cpp
More file actions
72 lines (63 loc) · 2.02 KB
/
Copy pathdaily11.cpp
File metadata and controls
72 lines (63 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Solution 1
class Solution {
public:
long long maximumImportance(int n, vector<vector<int>>& roads) {
// importance = city, [total incomign and outgoing edges, importance]
// amount of total in/out edges is importance?
auto importance = std::unordered_map<int, int>{};
auto in_out_edges = std::vector<std::pair<int,int>>(n, std::pair<int, int>{0 ,0});
for (int i = 0; i < n; ++i) {
in_out_edges[i].first = i;
importance[i] = 0;
}
for (auto i = roads.begin(); i != roads.end(); ++i) {
auto road = (*i)[0];
auto dest = (*i)[1];
in_out_edges[road].second++;
in_out_edges[dest].second++;
}
std::sort(in_out_edges.begin(), in_out_edges.end(), [](std::pair<int, int>& a, std::pair<int, int>& b) {
if (b.second == a.second) return b.first <= a.first;
return b.second < a.second;
});
auto curr_imp = n;
auto prev = int{0};
for (auto a = in_out_edges.begin(); a != in_out_edges.end(); ++a) {
importance[(*a).first] = curr_imp;
curr_imp--;
// std::cout << (*a).first << " ";
}
// std::cout << "\n";
auto total = long{0};
for (auto i = 0; i < roads.size(); ++ i) {
total += importance[roads[i][0]] + importance[roads[i][1]];
}
return total;
}
};
auto init = []() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 'c';
}();
// Solution 2
class Solution {
public:
long long maximumImportance(int n, vector<vector<int>>& roads) {
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// cout.tie(nullptr);
vector<long long> deg(n, 0);
for (auto &a : roads) {
deg[a[0]]++;
deg[a[1]]++;
}
sort(deg.begin(), deg.end());
long long ans = 0;
for (long long i=0;i<n;i++) {
ans += ( (i+1)*deg[i] );
}
return ans;
}
};