Advertisement
Guest User

Untitled

a guest
Aug 9th, 2024
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5. class DSU {
  6. private: int n;
  7. vector < int > parent;
  8. public: DSU(int n) {
  9. this - > n = n;
  10. parent.assign(n, 0);
  11. for(int i = 0; i < n; i++) {
  12. parent[i] = i;
  13. }
  14. }
  15. int find(int node) {
  16. if(node == parent[node]) {
  17. return node;
  18. }
  19. return parent[node] = find(parent[node]);
  20. }
  21. bool unite(int node1, int node2) {
  22. int root1 = find(node1);
  23. int root2 = find(node2);
  24. if(root1 != root2) {
  25. parent[root2] = root1;
  26. return true;
  27. }
  28. return false;
  29. }
  30. };
  31. vector < vector < pair < ll, ll >>> adj;
  32. vector < ll > childCount;
  33. ll dfs(ll node, ll parent) {
  34. ll count = 1;
  35. for(auto[nbr, cost]: adj[node]) {
  36. if(nbr == parent)
  37. continue;
  38. count += dfs(nbr, node);
  39. }
  40. childCount[node] = count;
  41. return count;
  42. }
  43. const int MAXN = 1e6;
  44. vector < ll > powers(MAXN, 0);
  45. void solve() {
  46. ll n, m;
  47. cin >> n >> m;
  48. adj.resize(n);
  49. childCount.assign(n, 0);
  50. vector < array < ll, 3 >> edges(m);
  51. vector < array < ll, 3 >> goodEdges;
  52. for(int i = 1; i <= m; i++) {
  53. ll a, b, c;
  54. cin >> a >> b >> c;
  55. a -= 1;
  56. b -= 1;
  57. edges[i - 1] = {
  58. c,
  59. a,
  60. b
  61. };
  62. }
  63. sort(edges.begin(), edges.end());
  64. DSU dsu(n);
  65. for(auto edge: edges) {
  66. auto[c, a, b] = edge;
  67. if(dsu.unite(a, b)) {
  68. adj[a].push_back({
  69. b,
  70. c
  71. });
  72. adj[b].push_back({
  73. a,
  74. c
  75. });
  76. goodEdges.push_back({
  77. a,
  78. b,
  79. c
  80. });
  81. }
  82. }
  83. dfs(0, 0);
  84. ll ans = 0;
  85. for(auto edge: goodEdges) {
  86. auto[a, b, c] = edge;
  87. ll mul = min(childCount[a], childCount[b]) * (n - min(childCount[a], childCount[b]));
  88. powers[c] += mul;
  89. }
  90. for(int i = 0; i < MAXN; i++) {
  91. if(powers[i] <= 1)
  92. continue;
  93. if(powers[i] & 1) {
  94. ll extra = powers[i] - 1;
  95. powers[i + 1] += (extra / 2);
  96. powers[i] = 1;
  97. continue;
  98. }
  99. ll extra = powers[i];
  100. powers[i + 1] += (extra / 2);
  101. powers[i] = 0;
  102. continue;
  103. }
  104. bool ok = false;
  105. for(int i = MAXN; i >= 0; i--) {
  106. if(powers[i]) {
  107. ok = true;
  108. }
  109. if(ok) {
  110. cout << powers[i];
  111. }
  112. }
  113. cout << "\n";
  114. }
  115. int main() {
  116. ios_base::sync_with_stdio(false);
  117. cin.tie(nullptr);
  118. solve();
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement