Advertisement
Josif_tepe

Untitled

Mar 2nd, 2025
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. const int maxn = 2e5 + 10;
  9. int n, m;
  10. vector<int> graph[maxn];
  11. int parent[maxn], sz[maxn];
  12.  
  13. void init() {
  14.     for(int i = 0; i < maxn; i++) {
  15.         parent[i] = i;
  16.         sz[i] = 1;
  17.     }
  18. }
  19. int find_root(int x) {
  20.     while(x != parent[x]) {
  21.         parent[x] = parent[parent[x]];
  22.         x = parent[x];
  23.     }
  24.     return x;
  25. }
  26. bool check(int A, int B) {
  27.     return find_root(A) == find_root(B);
  28. }
  29. void unite(int A, int B) {
  30.     int root_a = find_root(A);
  31.     int root_b = find_root(B);
  32.      
  33.     if(root_a != root_b) {
  34.         if(sz[root_a] < sz[root_b]) {
  35.             sz[root_b] += sz[root_a];
  36.             parent[root_a] = parent[root_b];
  37.         }
  38.         else {
  39.             sz[root_a] += sz[root_b];
  40.             parent[root_b] = parent[root_a];
  41.         }
  42.     }
  43. }
  44.  
  45. vector<int> or_graph[maxn];
  46. bool check_N(int S) {
  47.     queue<int> q;
  48.     q.push(S);
  49.     int vis = 0;
  50.     vector<bool> visited(n, false);
  51.     visited[S] = true;
  52.     while(!q.empty()) {
  53.         int c = q.front();
  54.         q.pop();
  55.         vis++;
  56.        
  57.         for(int i = 0; i < (int) or_graph[c].size(); i++) {
  58.             int neighbour = or_graph[c][i];
  59.             if(!visited[neighbour]) {
  60.                 visited[neighbour] = true;
  61.                 q.push(neighbour);
  62.             }
  63.         }
  64.     }
  65.     if(vis == n) {
  66.         return true;
  67.     }
  68.     return false;
  69. }
  70. int main() {
  71.     ios_base::sync_with_stdio(false);
  72. //    ifstream cin("in.txt");
  73.     cin >> n >> m;
  74.     init();
  75.    
  76.     vector<pair<int, pair<int, int>>> v;
  77.     for(int i = 0; i < m; i++) {
  78.         int a, b, c;
  79.         cin >> a >> b >> c;
  80.         a--; b--;
  81.         or_graph[a].push_back(b);
  82.         or_graph[b].push_back(a);
  83.        
  84.         v.push_back(make_pair(c, make_pair(a, b)));
  85.     }
  86.     if(!check_N(0)) {
  87.         cout << "N" << endl;
  88.         return 0;
  89.     }
  90.     sort(v.rbegin(), v.rend());
  91.     long long res = 0;
  92.     for(int i = 0; i < m; i++) {
  93.         int weight = v[i].first;
  94.         int a = v[i].second.first;
  95.         int b = v[i].second.second;
  96.        
  97.        
  98.         if(!check(a, b) or weight > 0) {
  99.             unite(a, b);
  100.             res += weight;
  101.         }
  102.        
  103.        
  104.     }
  105.     cout << res << endl;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement