Advertisement
shawon_majid

Wormhole sort USACO

Aug 15th, 2021
1,272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using vi = vector<int>;
  7. #define all(x) begin(x), end(x)
  8. using pi = pair<int,int>;
  9. #define F first
  10. #define S second
  11.  
  12. void setIO(string name = "") { // name is nonempty for USACO file I/O
  13.  
  14.     ios_base::sync_with_stdio(0); cin.tie(0); // see Fast Input & Output
  15.  
  16.     // alternatively, cin.tie(0)->sync_with_stdio(0);
  17.  
  18.     if (name.size()) {
  19.  
  20.         freopen((name+".in").c_str(), "r", stdin); // see Input & Output
  21.  
  22.         freopen((name+".out").c_str(), "w", stdout);
  23.  
  24.     }
  25.  
  26. }
  27.  
  28.  
  29. const int mx = 1e5 + 7;
  30.  
  31. vector <int> adj[mx];
  32.  
  33. bool cmp(pair <pair <int, int>, int > a, pair <pair <int, int> , int> b){
  34.     return a.second < b.second;
  35. }
  36.  
  37.  
  38.  
  39. int main() {
  40.  
  41.     setIO("wormsort");
  42.    
  43.     int n, m;
  44.     cin >> n >> m;
  45.     map <int, bool> necessary; // change necessary
  46.     vector <int> p(n+1);
  47.     for(int i = 1; i <= n; i++){
  48.         cin >> p[i];
  49.         if(p[i] != i){
  50.             necessary[i] = 1;
  51.         }
  52.     }  
  53.  
  54.     vector <pair < pair <int, int> , int> > edges;
  55.  
  56.     for(int i = 0; i < m; i++){
  57.         int u, v, c;
  58.         cin >> u >> v >> c;
  59.         edges.push_back({{u, v}, c});
  60.         adj[u].push_back(v);
  61.         adj[v].push_back(u);
  62.     }
  63.  
  64.     sort(all(edges), cmp);
  65.  
  66.     bool ok = 0;
  67.     int ans = edges[0].S;
  68.  
  69.     for(int i = 1; i <= n; i++){
  70.         if(p[i] != i){
  71.             ok = 1;
  72.         }
  73.     }
  74.     if(!ok){
  75.         cout << "-1" << endl;
  76.         return 0;
  77.     }
  78.    
  79.  
  80.     for(auto x: edges){
  81.         int u, v, c;
  82.         u = x.F.F, v = x.F.S, c = x.S;
  83.  
  84.         /*3 cases: 1. both are unnecessary
  85.         2. one necessary -> jeta necessary itar node size jodi 1 oy taile otau answer
  86.         3. both necessary. -> jekunu tar node size jodi 1 oy taile otaw answer
  87.         */
  88.         if((!necessary[u] and necessary[v]) or (necessary[u] and !necessary[v])){
  89.             if(necessary[u] and adj[u].size() <= 1){
  90.                 ok = 1;
  91.                 ans = c;
  92.                 break;
  93.             }
  94.             else if(necessary[v] and adj[v].size() <= 1){
  95.                 ok = 1;
  96.                 ans = c;
  97.                 break;
  98.             }
  99.         }
  100.         else if((necessary[u] and necessary[v]) and (adj[u].size() <= 1 or adj[v].size() <= 1) ){
  101.             ok = 1;
  102.             ans = c;
  103.             break;
  104.         }
  105.         else{
  106.             // we need to delete the edge from the graph
  107.             // delete v from adj[u]
  108.             int posv = 0;
  109.             for(auto x: adj[u]){
  110.                 if(x == v){
  111.                     break;
  112.                 }
  113.                 posv++;
  114.             }
  115.             adj[u].erase(adj[u].begin()+posv);
  116.             // delete u from adj[v]
  117.             int posu = 0;
  118.             for(auto x: adj[v]){
  119.                 if(x == u){
  120.                     break;
  121.                 }
  122.                 posu++;
  123.             }
  124.             adj[v].erase(adj[v].begin()+posu);
  125.         }
  126.     }
  127.  
  128.     cout << ans << endl;
  129.    
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement