Submission #617947

#TimeUsernameProblemLanguageResultExecution timeMemory
617947MohamedAliSaidaneRoad Closures (APIO21_roads)C++14
0 / 100
36 ms8316 KiB
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include "roads.h" using namespace __gnu_pbds; using namespace std; typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set; typedef long long ll; typedef long double ld; //#define int ll typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<ld,ld> pld; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pii> vpi; typedef vector<pll> vpl; #define pb push_back #define popb pop_back #define pp pop_back #define pf push_front #define popf pop_front #define all(x) (x).begin(),(x).end() #define ff first #define ss second int nx[4] = {0,0,1,-1}, ny[4] = {1,-1,0,0}; ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} const ll MOD = 998244353; const int nax = 204; vi p, rnk, setsz; int n; int findset(int x){return p[x] = p[x] == x? x: findset(p[x]);} void unite(int i, int j) { int x = findset(i); int y = findset(j); if(x == y) return ; if(rnk[x] > rnk[y]) swap(x,y); p[x] = y; setsz[y] += setsz[x]; if(rnk[x] == rnk[y]) rnk[y] ++ ; } vll minimum_closure_costs(int N, vi U, vi V, vi W) { n = N; vll ans; vector<pair<ll,pii>> edges; ll sum = 0ll; for(int i= 0 ; i < n - 1; i ++) sum += W[i]; for(int i = 0 ; i < n - 1; i++) edges.pb({W[i], {U[i], V[i]}}); sort(all(edges)); reverse(all(edges)); for(int k = 1; k <= n;k ++) { ll rep = 0ll; for(int i= 0 ; i < n;i ++) { p[i] = i; rnk[i] = 0; setsz[i] = 1; } for(auto e: edges) { ll cost = e.ff; int u = findset(e.ss.ff); int v = findset(e.ss.ss); if(u == v) continue; if(setsz[u] + setsz[v] > k) continue; unite(u, v); sum += cost; } ans.pb(sum - rep); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...