# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
125161 | 2019-07-04T19:29:38 Z | Sorting | Simurgh (IOI17_simurgh) | C++14 | 0 ms | 0 KB |
#include <bits/stdc++.h> using namespace std; int count_common_roads(vector<int> &r); const int N = 507; const int M = N * (N - 1) / 2 + 1; mt19937 mt; int rnd(int mod){ return mt() % mod; } struct edge{ int from, to, idx; edge(); edge(int from, int to, int idx){ this->from = from; this->to = to; this->idx = idx; } }; vector<int> adj[N]; vector<edge> ev; int m, par[N], sz[N]; void find_par(int u){ if(u == par[u]){ return; } find_par(par[u]); par[u] = par[par[u]]; } bool unite(int u, int v){ find_par(u); find_par(v); if(par[u] == par[v]){ return false; } if(sz[par[u]] < sz[par[v]]){ swap(u, v); } sz[par[u]] += sz[par[v]]; par[par[v]] = par[u]; return true; } vector<int> find_roads(int n, vector<int> u, vector<int> v){ m = (int)u.size(); for(int i = 0; i < m; i++){ adj[ u[i] ].push_back(v[i]); adj[ v[i] ].push_back(u[i]); ev.push_back(edge(u[i], v[i], i)); } while(true){ for(int i = 0; i < n; i++){ par[i] = i; sz[i] = 1; } random_shuffle(ev.begin(), ev.end(), rnd); vector<int> v; for(edge e: ev){ if(unite(e.from, e.to)){ v.push_back(e.idx); if(v.size() == n - 1){ break; } } } if(count_common_roads(v) == n - 1){ return v; } } }