Submission #733956

# Submission time Handle Problem Language Result Execution time Memory
733956 2023-05-01T12:22:50 Z loctildore Phone Plans (CCO22_day2problem2) C++17
6 / 25
461 ms 75900 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define endl '\n'
#define all(x) begin(x), end(x)
int n, ca, cb, k;
int u, v, l;
int ans = INT_MAX;
vector<tuple<int,int,int>> va, vb;
vector<pair<int,pair<int,int>>> lga[200069], lgb[200069];
int cnt, cnta[200069], cntb[200069];
vector<int> chd[200069];
int par[200069];
pair<int,int> rts[200069];
map<int,int> mp[200069];
void dsuclr() {
    for (int i = 0; i < n; i++) {
        par[i] = i;
        chd[i].clear();
        chd[i].push_back(i);
    }
    cnt = 0;
}
void merge(int x, int y, vector<pair<int,pair<int,int>>> &lg) {
    x = par[x]; y = par[y];
    if (x == y) return;
    if (chd[x].size() < chd[y].size()) swap(x, y);
    cnt += chd[x].size() * chd[y].size();
    for (auto i : chd[y]) {
        par[i] = x;
        chd[x].push_back(i);
        lg.push_back({i, {y, x}});
    }
    chd[y].clear();
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cin>>n>>ca>>cb>>k;
    dsuclr();
    for (int i = 0; i < ca; i++) {
        cin>>u>>v>>l; u--; v--;
        va.push_back({l, u, v});
    }
    sort(all(va));
    for (int i = 0; i < ca; i++) {
        tie(l, u, v) = va[i];
        merge(u, v, lga[i + 1]);
        cnta[i + 1] = cnt;
    }
    for (int i = 0; i < n; i++) {
        rts[i] = {par[i], i};
        mp[par[i]][i] = 1;
    }
    dsuclr();
    for (int i = 0; i < cb; i++) {
        cin>>u>>v>>l; u--; v--;
        vb.push_back({l, u, v});
    }
    sort(all(vb));
    for (int i = 0; i < cb; i++) {
        tie(l, u, v) = vb[i];
        merge(u, v, lgb[i + 1]);
        cntb[i + 1] = cnt;
    }
    cnt = 0;
    int tb = 0;
    for (int ta = ca; ~ta; ta--) {
        for (auto t : lga[ta + 1]) {
            int i = t.f;
            mp[rts[i].f][rts[i].s]--;
            cnt-=mp[rts[i].f][rts[i].s];
            rts[i].f = t.s.f;
            cnt+=mp[rts[i].f][rts[i].s];
            mp[rts[i].f][rts[i].s]++;
        }
        while (tb < cb && cnta[ta] + cntb[tb] - cnt < k) {
            for (auto t : lgb[tb + 1]) {
                int i = t.f;
                mp[rts[i].f][rts[i].s]--;
                cnt-=mp[rts[i].f][rts[i].s];
                rts[i].s = t.s.s;
                cnt+=mp[rts[i].f][rts[i].s];
                mp[rts[i].f][rts[i].s]++;
            }
            tb++;
        }
        if (cnta[ta] + cntb[tb] - cnt >= k) {
            int tmpa = (ta == 0 ? 0 : get<0>(va[ta - 1]));
            int tmpb = (tb == 0 ? 0 : get<0>(vb[tb - 1]));
            ans = min(ans, tmpa + tmpb);
        }
    }
    cout<<(ans == INT_MAX ? -1 : ans)<<endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23764 KB Output is correct
2 Correct 13 ms 23732 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 12 ms 23828 KB Output is correct
5 Correct 12 ms 23824 KB Output is correct
6 Correct 81 ms 31056 KB Output is correct
7 Correct 90 ms 31080 KB Output is correct
8 Correct 163 ms 38080 KB Output is correct
9 Correct 155 ms 38028 KB Output is correct
10 Correct 158 ms 38088 KB Output is correct
11 Correct 161 ms 38236 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 11 ms 24012 KB Output is correct
14 Correct 12 ms 23948 KB Output is correct
15 Correct 152 ms 38016 KB Output is correct
16 Correct 175 ms 38144 KB Output is correct
17 Correct 155 ms 38012 KB Output is correct
18 Correct 152 ms 38068 KB Output is correct
19 Correct 176 ms 38120 KB Output is correct
20 Correct 162 ms 38124 KB Output is correct
21 Correct 158 ms 38008 KB Output is correct
22 Correct 191 ms 38144 KB Output is correct
23 Correct 157 ms 38208 KB Output is correct
24 Correct 147 ms 38028 KB Output is correct
25 Correct 160 ms 38092 KB Output is correct
26 Correct 159 ms 38020 KB Output is correct
27 Correct 157 ms 38080 KB Output is correct
28 Correct 163 ms 38072 KB Output is correct
29 Correct 157 ms 38056 KB Output is correct
30 Correct 159 ms 38012 KB Output is correct
31 Correct 159 ms 38124 KB Output is correct
32 Correct 168 ms 38096 KB Output is correct
33 Correct 138 ms 36348 KB Output is correct
34 Correct 141 ms 36384 KB Output is correct
35 Correct 139 ms 36384 KB Output is correct
36 Correct 147 ms 36356 KB Output is correct
37 Correct 14 ms 24168 KB Output is correct
38 Correct 16 ms 24148 KB Output is correct
39 Correct 14 ms 24164 KB Output is correct
40 Correct 13 ms 24148 KB Output is correct
41 Correct 128 ms 36248 KB Output is correct
42 Correct 126 ms 36224 KB Output is correct
43 Correct 144 ms 37296 KB Output is correct
44 Correct 149 ms 37372 KB Output is correct
45 Correct 175 ms 38120 KB Output is correct
46 Correct 168 ms 38112 KB Output is correct
47 Correct 153 ms 38072 KB Output is correct
48 Correct 161 ms 36032 KB Output is correct
49 Correct 153 ms 35940 KB Output is correct
50 Correct 14 ms 24276 KB Output is correct
51 Correct 82 ms 31032 KB Output is correct
52 Correct 85 ms 31108 KB Output is correct
53 Correct 16 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 461 ms 75900 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23892 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 86 ms 29772 KB Output is correct
4 Correct 67 ms 29980 KB Output is correct
5 Correct 201 ms 59488 KB Output is correct
6 Incorrect 50 ms 45108 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23764 KB Output is correct
2 Correct 13 ms 23732 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 12 ms 23828 KB Output is correct
5 Correct 12 ms 23824 KB Output is correct
6 Correct 81 ms 31056 KB Output is correct
7 Correct 90 ms 31080 KB Output is correct
8 Correct 163 ms 38080 KB Output is correct
9 Correct 155 ms 38028 KB Output is correct
10 Correct 158 ms 38088 KB Output is correct
11 Correct 161 ms 38236 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 11 ms 24012 KB Output is correct
14 Correct 12 ms 23948 KB Output is correct
15 Correct 152 ms 38016 KB Output is correct
16 Correct 175 ms 38144 KB Output is correct
17 Correct 155 ms 38012 KB Output is correct
18 Correct 152 ms 38068 KB Output is correct
19 Correct 176 ms 38120 KB Output is correct
20 Correct 162 ms 38124 KB Output is correct
21 Correct 158 ms 38008 KB Output is correct
22 Correct 191 ms 38144 KB Output is correct
23 Correct 157 ms 38208 KB Output is correct
24 Correct 147 ms 38028 KB Output is correct
25 Correct 160 ms 38092 KB Output is correct
26 Correct 159 ms 38020 KB Output is correct
27 Correct 157 ms 38080 KB Output is correct
28 Correct 163 ms 38072 KB Output is correct
29 Correct 157 ms 38056 KB Output is correct
30 Correct 159 ms 38012 KB Output is correct
31 Correct 159 ms 38124 KB Output is correct
32 Correct 168 ms 38096 KB Output is correct
33 Correct 138 ms 36348 KB Output is correct
34 Correct 141 ms 36384 KB Output is correct
35 Correct 139 ms 36384 KB Output is correct
36 Correct 147 ms 36356 KB Output is correct
37 Correct 14 ms 24168 KB Output is correct
38 Correct 16 ms 24148 KB Output is correct
39 Correct 14 ms 24164 KB Output is correct
40 Correct 13 ms 24148 KB Output is correct
41 Correct 128 ms 36248 KB Output is correct
42 Correct 126 ms 36224 KB Output is correct
43 Correct 144 ms 37296 KB Output is correct
44 Correct 149 ms 37372 KB Output is correct
45 Correct 175 ms 38120 KB Output is correct
46 Correct 168 ms 38112 KB Output is correct
47 Correct 153 ms 38072 KB Output is correct
48 Correct 161 ms 36032 KB Output is correct
49 Correct 153 ms 35940 KB Output is correct
50 Correct 14 ms 24276 KB Output is correct
51 Correct 82 ms 31032 KB Output is correct
52 Correct 85 ms 31108 KB Output is correct
53 Correct 16 ms 23764 KB Output is correct
54 Incorrect 461 ms 75900 KB Output isn't correct
55 Halted 0 ms 0 KB -