Submission #971569

#TimeUsernameProblemLanguageResultExecution timeMemory
971569CookiePhone Plans (CCO22_day2problem2)C++14
25 / 25
1625 ms159664 KiB
#include<bits/stdc++.h> #include<fstream> using namespace std; #define sz(a) (int)a.size() #define ALL(v) v.begin(), v.end() #define ALLR(v) v.rbegin(), v.rend() #define ll long long #define pb push_back #define forr(i, a, b) for(int i = a; i < b; i++) #define dorr(i, a, b) for(int i = a; i >= b; i--) #define ld long double #define vt vector #include<fstream> #define fi first #define se second #define pll pair<ll, ll> #define pii pair<int, int> #define mpp make_pair const ld PI = 3.14159265359, prec = 1e-9;; //using u128 = __uint128_t; //const int x[4] = {1, 0, -1, 0}; //const int y[4] = {0, -1, 0, 1}; const ll mod =1e9 + 7, pr = 31; const int mxn = 5e5 + 5, mxq = 1e5 + 5, sq = 500, mxv = 5e4 + 1; //const int base = (1 <<18); const ll inf = 1e13 + 5, neg = -69420, inf2 = 1e14; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); // have fun! struct E{ ll u, v, w; bool operator <(const E &other){ return(w < other.w); } }; struct EV{ int node, pre, curr; }; ll n, a, b, k; ll c2(ll n){ return(n * (n - 1) / 2); } struct DSU{ ll tot = 0; ll pa[mxn + 1], siz[mxn + 1]; vt<int>node[mxn + 1]; int fp(int u){ if(pa[u] == u)return(u); return(pa[u] = fp(pa[u])); } void init(){ for(int i = 1; i <= n; i++){ pa[i] = i; siz[i] = 1; node[i].clear(); node[i].pb(i); } tot = 0; } vt<EV>unon(int u, int v){ u = fp(u); v = fp(v); vt<EV>events; if(u == v)return(events); tot += siz[u] * siz[v]; if(siz[u] < siz[v])swap(u, v); siz[u] += siz[v]; pa[v] = u; for(auto i: node[v]){ node[u].pb(i); events.pb({i, v, u}); } return(events); } }; DSU dsu; vt<E>edgea, edgeb; vt<EV>eva[mxn + 1], evb[mxn + 1]; ll canda[mxn + 1], candb[mxn + 1]; int paa[mxn + 1], pab[mxn + 1]; map<pair<int, int>, ll>mp; void solve(){ cin >> n >> a >> b >> k; if(k == 0){ cout << 0; return; } for(int i = 0; i < a; i++){ int u, v, w; cin >> u >> v >> w; edgea.pb({u, v, w}); } for(int i = 0; i < b; i++){ int u, v, w; cin >> u >> v >> w; edgeb.pb({u, v, w}); } sort(ALL(edgea)); sort(ALL(edgeb)); dsu.init(); ll ans = inf; for(int i = 0; i < sz(edgea); i++){ eva[i + 1] = dsu.unon(edgea[i].u, edgea[i].v); canda[i + 1] = dsu.tot; if(dsu.tot >= k)ans = min(ans, edgea[i].w); } dsu.init(); for(int i = 0; i < sz(edgeb); i++){ evb[i + 1] = dsu.unon(edgeb[i].u, edgeb[i].v); candb[i + 1] = dsu.tot; if(dsu.tot >= k)ans = min(ans, edgeb[i].w); } for(int i = 1; i <= n; i++){ paa[i] = i; pab[i] = dsu.fp(i); mp[mpp(paa[i], pab[i])]++; } int rp = b; ll sme = 0; // belonging to both sets for(int i = 1; i <= a; i++){ for(auto [node, pre, curr]: eva[i]){ //cout << node << " " << pre << " " << curr << "\n"; mp[mpp(paa[node], pab[node])]--; sme -= mp[mpp(paa[node], pab[node])]; //assert(paa[node] == pre); paa[node] = curr; sme += mp[mpp(paa[node], pab[node])]; mp[mpp(paa[node], pab[node])]++; } while(canda[i] + candb[rp] - sme >= k && rp >= 1){ for(auto [node, pre, curr]: evb[rp]){ mp[mpp(paa[node], pab[node])]--; sme -= mp[mpp(paa[node], pab[node])]; //assert(pab[node] == curr); pab[node] = pre; sme += mp[mpp(paa[node], pab[node])]; mp[mpp(paa[node], pab[node])]++; } rp--; } if(rp != b){ ans = min(ans, edgea[i - 1].w + edgeb[rp].w); } } cout << ((ans == inf) ? -1 : ans); } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("THREE.inp", "r", stdin); //freopen("THREE.out", "w", stdout); int tt; tt = 1; while(tt--){ solve(); } return(0); }

Compilation message (stderr)

Main.cpp: In function 'void solve()':
Main.cpp:112:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  112 |         for(auto [node, pre, curr]: eva[i]){
      |                  ^
Main.cpp:122:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  122 |             for(auto [node, pre, curr]: evb[rp]){
      |                      ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...