제출 #1135263

#제출 시각아이디문제언어결과실행 시간메모리
1135263mychecksedadSecurity Guard (JOI23_guard)C++20
50 / 100
234 ms49872 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> using namespace std; #define ll long long int #define MOD (1000000000+7) #define MOD1 (998244353) #define pb push_back #define all(x) x.begin(), x.end() #define en cout << '\n' #define ff first #define ss second #define pii pair<int,int> #define vi vector<int> const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30; struct Dsu { vector<int> s, p; int sz; Dsu(int n){ sz = n; s.resize(n+1, 1); p.resize(n+1); for(int i = 0; i <= n; ++i) p[i] = i; } int find(int v){ if(p[v] == v) return v; return (p[v] = find(p[v])); } void merge(int a, int b){ a = find(a); b = find(b); if(a != b){ if(s[a] > s[b]){ swap(a, b); } s[b] += s[a]; p[a] = b; sz--; } } }; int n, m, q; ll a[N], ans = 0; vi g[N]; void solve(){ cin >> n >> m >> q; int mx = 0, V = 0; for(int i = 1; i <= n; ++i){ cin >> a[i]; if(mx < a[i]){ mx = a[i]; V = i; } } vector<array<ll, 3>> E; for(int i = 1; i <= m; ++i){ int u, v; cin >> u >> v; E.pb({a[u] + a[v], u, v}); } sort(all(E)); Dsu d(n); for(int i = 0; i < m; ++i){ auto [w, u, v] = E[i]; if(d.find(u) != d.find(v)){ d.merge(u, v); g[u].pb(v); g[v].pb(u); } } // for(int i = 1; i <= n; ++i){ // sort(all(g[i]), [&](const int &x, const int &y){ // return a[x] > a[y]; // }); // } priority_queue<pair<ll, int>> Q; Q.push({-a[V], V}); vector<bool> vis(n + 1); vis[V] = 1; while(!Q.empty()){ int v = Q.top().ss; Q.pop(); // cout << v << ' '; if(g[v].empty()) continue; int u; do{ u = g[v].back(); g[v].pop_back(); } while(vis[u] && g[v].size()); if(vis[u]) continue; vis[u] = 1; if(!g[v].empty()) Q.push({-a[v], v}); Q.push({-a[u], u}); ans += a[v]; } cout << ans; } int main(){ cin.tie(0); ios::sync_with_stdio(0); int tt = 1, aa; // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(tt--){ solve(); en; } cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n"; return 0; }
#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...