Submission #993701

# Submission time Handle Problem Language Result Execution time Memory
993701 2024-06-06T10:24:49 Z Trisanu_Das Phone Plans (CCO22_day2problem2) C++17
25 / 25
615 ms 131792 KB
#include <bits/stdc++.h>
using namespace std;
#define int 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();
}
signed 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 9 ms 23900 KB Output is correct
2 Correct 9 ms 23900 KB Output is correct
3 Correct 9 ms 23960 KB Output is correct
4 Correct 9 ms 23900 KB Output is correct
5 Correct 9 ms 23784 KB Output is correct
6 Correct 65 ms 34492 KB Output is correct
7 Correct 63 ms 34284 KB Output is correct
8 Correct 114 ms 46000 KB Output is correct
9 Correct 113 ms 46004 KB Output is correct
10 Correct 111 ms 45812 KB Output is correct
11 Correct 108 ms 45872 KB Output is correct
12 Correct 10 ms 24152 KB Output is correct
13 Correct 10 ms 24156 KB Output is correct
14 Correct 10 ms 24156 KB Output is correct
15 Correct 113 ms 46012 KB Output is correct
16 Correct 112 ms 46000 KB Output is correct
17 Correct 112 ms 46004 KB Output is correct
18 Correct 107 ms 46000 KB Output is correct
19 Correct 113 ms 45876 KB Output is correct
20 Correct 109 ms 45944 KB Output is correct
21 Correct 114 ms 46000 KB Output is correct
22 Correct 108 ms 46000 KB Output is correct
23 Correct 109 ms 46004 KB Output is correct
24 Correct 115 ms 45952 KB Output is correct
25 Correct 109 ms 46048 KB Output is correct
26 Correct 113 ms 45996 KB Output is correct
27 Correct 109 ms 46000 KB Output is correct
28 Correct 127 ms 46004 KB Output is correct
29 Correct 121 ms 46004 KB Output is correct
30 Correct 108 ms 46000 KB Output is correct
31 Correct 116 ms 46008 KB Output is correct
32 Correct 108 ms 46108 KB Output is correct
33 Correct 106 ms 44164 KB Output is correct
34 Correct 109 ms 44212 KB Output is correct
35 Correct 107 ms 44208 KB Output is correct
36 Correct 108 ms 44208 KB Output is correct
37 Correct 11 ms 24412 KB Output is correct
38 Correct 11 ms 24412 KB Output is correct
39 Correct 11 ms 24412 KB Output is correct
40 Correct 12 ms 24412 KB Output is correct
41 Correct 104 ms 44016 KB Output is correct
42 Correct 98 ms 43944 KB Output is correct
43 Correct 105 ms 45236 KB Output is correct
44 Correct 112 ms 45236 KB Output is correct
45 Correct 114 ms 45960 KB Output is correct
46 Correct 117 ms 46004 KB Output is correct
47 Correct 113 ms 45964 KB Output is correct
48 Correct 112 ms 43704 KB Output is correct
49 Correct 116 ms 43696 KB Output is correct
50 Correct 13 ms 24408 KB Output is correct
51 Correct 65 ms 34484 KB Output is correct
52 Correct 64 ms 34224 KB Output is correct
53 Correct 12 ms 23896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 326 ms 89284 KB Output is correct
2 Correct 241 ms 78876 KB Output is correct
3 Correct 119 ms 66552 KB Output is correct
4 Correct 10 ms 23736 KB Output is correct
5 Correct 10 ms 23900 KB Output is correct
6 Correct 10 ms 23900 KB Output is correct
7 Correct 114 ms 44124 KB Output is correct
8 Correct 85 ms 44468 KB Output is correct
9 Correct 147 ms 69744 KB Output is correct
10 Correct 249 ms 79028 KB Output is correct
11 Correct 312 ms 88364 KB Output is correct
12 Correct 298 ms 88360 KB Output is correct
13 Correct 324 ms 88364 KB Output is correct
14 Correct 309 ms 88364 KB Output is correct
15 Correct 30 ms 47336 KB Output is correct
16 Correct 29 ms 47196 KB Output is correct
17 Correct 30 ms 47184 KB Output is correct
18 Correct 318 ms 98096 KB Output is correct
19 Correct 347 ms 98088 KB Output is correct
20 Correct 315 ms 97828 KB Output is correct
21 Correct 328 ms 97704 KB Output is correct
22 Correct 320 ms 98344 KB Output is correct
23 Correct 326 ms 98288 KB Output is correct
24 Correct 379 ms 101364 KB Output is correct
25 Correct 384 ms 103720 KB Output is correct
26 Correct 411 ms 107560 KB Output is correct
27 Correct 421 ms 110376 KB Output is correct
28 Correct 362 ms 97836 KB Output is correct
29 Correct 344 ms 98088 KB Output is correct
30 Correct 330 ms 98344 KB Output is correct
31 Correct 338 ms 97832 KB Output is correct
32 Correct 320 ms 97836 KB Output is correct
33 Correct 336 ms 98144 KB Output is correct
34 Correct 351 ms 100656 KB Output is correct
35 Correct 374 ms 102852 KB Output is correct
36 Correct 411 ms 106024 KB Output is correct
37 Correct 430 ms 109100 KB Output is correct
38 Correct 217 ms 71220 KB Output is correct
39 Correct 213 ms 71472 KB Output is correct
40 Correct 226 ms 71188 KB Output is correct
41 Correct 212 ms 71320 KB Output is correct
42 Correct 107 ms 43956 KB Output is correct
43 Correct 102 ms 43960 KB Output is correct
44 Correct 106 ms 45188 KB Output is correct
45 Correct 106 ms 45172 KB Output is correct
46 Correct 340 ms 99228 KB Output is correct
47 Correct 369 ms 103536 KB Output is correct
48 Correct 418 ms 110948 KB Output is correct
49 Correct 355 ms 98080 KB Output is correct
50 Correct 425 ms 107848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23896 KB Output is correct
2 Correct 9 ms 23864 KB Output is correct
3 Correct 55 ms 32956 KB Output is correct
4 Correct 51 ms 33208 KB Output is correct
5 Correct 142 ms 73908 KB Output is correct
6 Correct 316 ms 96176 KB Output is correct
7 Correct 193 ms 79284 KB Output is correct
8 Correct 30 ms 47196 KB Output is correct
9 Correct 197 ms 77748 KB Output is correct
10 Correct 195 ms 77640 KB Output is correct
11 Correct 213 ms 77748 KB Output is correct
12 Correct 193 ms 77748 KB Output is correct
13 Correct 31 ms 47188 KB Output is correct
14 Correct 28 ms 47192 KB Output is correct
15 Correct 29 ms 47196 KB Output is correct
16 Correct 131 ms 73136 KB Output is correct
17 Correct 131 ms 73136 KB Output is correct
18 Correct 130 ms 73140 KB Output is correct
19 Correct 140 ms 73088 KB Output is correct
20 Correct 130 ms 73144 KB Output is correct
21 Correct 152 ms 73760 KB Output is correct
22 Correct 217 ms 79792 KB Output is correct
23 Correct 214 ms 83124 KB Output is correct
24 Correct 291 ms 90796 KB Output is correct
25 Correct 304 ms 96436 KB Output is correct
26 Correct 140 ms 72880 KB Output is correct
27 Correct 133 ms 73136 KB Output is correct
28 Correct 136 ms 73004 KB Output is correct
29 Correct 135 ms 73140 KB Output is correct
30 Correct 152 ms 73140 KB Output is correct
31 Correct 137 ms 73132 KB Output is correct
32 Correct 196 ms 79764 KB Output is correct
33 Correct 235 ms 83632 KB Output is correct
34 Correct 293 ms 95156 KB Output is correct
35 Correct 334 ms 96412 KB Output is correct
36 Correct 143 ms 61364 KB Output is correct
37 Correct 147 ms 61364 KB Output is correct
38 Correct 137 ms 61360 KB Output is correct
39 Correct 161 ms 61380 KB Output is correct
40 Correct 144 ms 61556 KB Output is correct
41 Correct 150 ms 61616 KB Output is correct
42 Correct 138 ms 61620 KB Output is correct
43 Correct 150 ms 61344 KB Output is correct
44 Correct 147 ms 61560 KB Output is correct
45 Correct 148 ms 61356 KB Output is correct
46 Correct 55 ms 33096 KB Output is correct
47 Correct 54 ms 33088 KB Output is correct
48 Correct 59 ms 33720 KB Output is correct
49 Correct 59 ms 33692 KB Output is correct
50 Correct 167 ms 77440 KB Output is correct
51 Correct 281 ms 92912 KB Output is correct
52 Correct 317 ms 96172 KB Output is correct
53 Correct 135 ms 71856 KB Output is correct
54 Correct 254 ms 88408 KB Output is correct
55 Correct 136 ms 68032 KB Output is correct
56 Correct 31 ms 47188 KB Output is correct
57 Correct 34 ms 47452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 23900 KB Output is correct
2 Correct 9 ms 23900 KB Output is correct
3 Correct 9 ms 23960 KB Output is correct
4 Correct 9 ms 23900 KB Output is correct
5 Correct 9 ms 23784 KB Output is correct
6 Correct 65 ms 34492 KB Output is correct
7 Correct 63 ms 34284 KB Output is correct
8 Correct 114 ms 46000 KB Output is correct
9 Correct 113 ms 46004 KB Output is correct
10 Correct 111 ms 45812 KB Output is correct
11 Correct 108 ms 45872 KB Output is correct
12 Correct 10 ms 24152 KB Output is correct
13 Correct 10 ms 24156 KB Output is correct
14 Correct 10 ms 24156 KB Output is correct
15 Correct 113 ms 46012 KB Output is correct
16 Correct 112 ms 46000 KB Output is correct
17 Correct 112 ms 46004 KB Output is correct
18 Correct 107 ms 46000 KB Output is correct
19 Correct 113 ms 45876 KB Output is correct
20 Correct 109 ms 45944 KB Output is correct
21 Correct 114 ms 46000 KB Output is correct
22 Correct 108 ms 46000 KB Output is correct
23 Correct 109 ms 46004 KB Output is correct
24 Correct 115 ms 45952 KB Output is correct
25 Correct 109 ms 46048 KB Output is correct
26 Correct 113 ms 45996 KB Output is correct
27 Correct 109 ms 46000 KB Output is correct
28 Correct 127 ms 46004 KB Output is correct
29 Correct 121 ms 46004 KB Output is correct
30 Correct 108 ms 46000 KB Output is correct
31 Correct 116 ms 46008 KB Output is correct
32 Correct 108 ms 46108 KB Output is correct
33 Correct 106 ms 44164 KB Output is correct
34 Correct 109 ms 44212 KB Output is correct
35 Correct 107 ms 44208 KB Output is correct
36 Correct 108 ms 44208 KB Output is correct
37 Correct 11 ms 24412 KB Output is correct
38 Correct 11 ms 24412 KB Output is correct
39 Correct 11 ms 24412 KB Output is correct
40 Correct 12 ms 24412 KB Output is correct
41 Correct 104 ms 44016 KB Output is correct
42 Correct 98 ms 43944 KB Output is correct
43 Correct 105 ms 45236 KB Output is correct
44 Correct 112 ms 45236 KB Output is correct
45 Correct 114 ms 45960 KB Output is correct
46 Correct 117 ms 46004 KB Output is correct
47 Correct 113 ms 45964 KB Output is correct
48 Correct 112 ms 43704 KB Output is correct
49 Correct 116 ms 43696 KB Output is correct
50 Correct 13 ms 24408 KB Output is correct
51 Correct 65 ms 34484 KB Output is correct
52 Correct 64 ms 34224 KB Output is correct
53 Correct 12 ms 23896 KB Output is correct
54 Correct 326 ms 89284 KB Output is correct
55 Correct 241 ms 78876 KB Output is correct
56 Correct 119 ms 66552 KB Output is correct
57 Correct 10 ms 23736 KB Output is correct
58 Correct 10 ms 23900 KB Output is correct
59 Correct 10 ms 23900 KB Output is correct
60 Correct 114 ms 44124 KB Output is correct
61 Correct 85 ms 44468 KB Output is correct
62 Correct 147 ms 69744 KB Output is correct
63 Correct 249 ms 79028 KB Output is correct
64 Correct 312 ms 88364 KB Output is correct
65 Correct 298 ms 88360 KB Output is correct
66 Correct 324 ms 88364 KB Output is correct
67 Correct 309 ms 88364 KB Output is correct
68 Correct 30 ms 47336 KB Output is correct
69 Correct 29 ms 47196 KB Output is correct
70 Correct 30 ms 47184 KB Output is correct
71 Correct 318 ms 98096 KB Output is correct
72 Correct 347 ms 98088 KB Output is correct
73 Correct 315 ms 97828 KB Output is correct
74 Correct 328 ms 97704 KB Output is correct
75 Correct 320 ms 98344 KB Output is correct
76 Correct 326 ms 98288 KB Output is correct
77 Correct 379 ms 101364 KB Output is correct
78 Correct 384 ms 103720 KB Output is correct
79 Correct 411 ms 107560 KB Output is correct
80 Correct 421 ms 110376 KB Output is correct
81 Correct 362 ms 97836 KB Output is correct
82 Correct 344 ms 98088 KB Output is correct
83 Correct 330 ms 98344 KB Output is correct
84 Correct 338 ms 97832 KB Output is correct
85 Correct 320 ms 97836 KB Output is correct
86 Correct 336 ms 98144 KB Output is correct
87 Correct 351 ms 100656 KB Output is correct
88 Correct 374 ms 102852 KB Output is correct
89 Correct 411 ms 106024 KB Output is correct
90 Correct 430 ms 109100 KB Output is correct
91 Correct 217 ms 71220 KB Output is correct
92 Correct 213 ms 71472 KB Output is correct
93 Correct 226 ms 71188 KB Output is correct
94 Correct 212 ms 71320 KB Output is correct
95 Correct 107 ms 43956 KB Output is correct
96 Correct 102 ms 43960 KB Output is correct
97 Correct 106 ms 45188 KB Output is correct
98 Correct 106 ms 45172 KB Output is correct
99 Correct 340 ms 99228 KB Output is correct
100 Correct 369 ms 103536 KB Output is correct
101 Correct 418 ms 110948 KB Output is correct
102 Correct 355 ms 98080 KB Output is correct
103 Correct 425 ms 107848 KB Output is correct
104 Correct 10 ms 23896 KB Output is correct
105 Correct 9 ms 23864 KB Output is correct
106 Correct 55 ms 32956 KB Output is correct
107 Correct 51 ms 33208 KB Output is correct
108 Correct 142 ms 73908 KB Output is correct
109 Correct 316 ms 96176 KB Output is correct
110 Correct 193 ms 79284 KB Output is correct
111 Correct 30 ms 47196 KB Output is correct
112 Correct 197 ms 77748 KB Output is correct
113 Correct 195 ms 77640 KB Output is correct
114 Correct 213 ms 77748 KB Output is correct
115 Correct 193 ms 77748 KB Output is correct
116 Correct 31 ms 47188 KB Output is correct
117 Correct 28 ms 47192 KB Output is correct
118 Correct 29 ms 47196 KB Output is correct
119 Correct 131 ms 73136 KB Output is correct
120 Correct 131 ms 73136 KB Output is correct
121 Correct 130 ms 73140 KB Output is correct
122 Correct 140 ms 73088 KB Output is correct
123 Correct 130 ms 73144 KB Output is correct
124 Correct 152 ms 73760 KB Output is correct
125 Correct 217 ms 79792 KB Output is correct
126 Correct 214 ms 83124 KB Output is correct
127 Correct 291 ms 90796 KB Output is correct
128 Correct 304 ms 96436 KB Output is correct
129 Correct 140 ms 72880 KB Output is correct
130 Correct 133 ms 73136 KB Output is correct
131 Correct 136 ms 73004 KB Output is correct
132 Correct 135 ms 73140 KB Output is correct
133 Correct 152 ms 73140 KB Output is correct
134 Correct 137 ms 73132 KB Output is correct
135 Correct 196 ms 79764 KB Output is correct
136 Correct 235 ms 83632 KB Output is correct
137 Correct 293 ms 95156 KB Output is correct
138 Correct 334 ms 96412 KB Output is correct
139 Correct 143 ms 61364 KB Output is correct
140 Correct 147 ms 61364 KB Output is correct
141 Correct 137 ms 61360 KB Output is correct
142 Correct 161 ms 61380 KB Output is correct
143 Correct 144 ms 61556 KB Output is correct
144 Correct 150 ms 61616 KB Output is correct
145 Correct 138 ms 61620 KB Output is correct
146 Correct 150 ms 61344 KB Output is correct
147 Correct 147 ms 61560 KB Output is correct
148 Correct 148 ms 61356 KB Output is correct
149 Correct 55 ms 33096 KB Output is correct
150 Correct 54 ms 33088 KB Output is correct
151 Correct 59 ms 33720 KB Output is correct
152 Correct 59 ms 33692 KB Output is correct
153 Correct 167 ms 77440 KB Output is correct
154 Correct 281 ms 92912 KB Output is correct
155 Correct 317 ms 96172 KB Output is correct
156 Correct 135 ms 71856 KB Output is correct
157 Correct 254 ms 88408 KB Output is correct
158 Correct 136 ms 68032 KB Output is correct
159 Correct 31 ms 47188 KB Output is correct
160 Correct 34 ms 47452 KB Output is correct
161 Correct 10 ms 23900 KB Output is correct
162 Correct 10 ms 23928 KB Output is correct
163 Correct 106 ms 44024 KB Output is correct
164 Correct 90 ms 44464 KB Output is correct
165 Correct 199 ms 79696 KB Output is correct
166 Correct 374 ms 96224 KB Output is correct
167 Correct 415 ms 94004 KB Output is correct
168 Correct 420 ms 95476 KB Output is correct
169 Correct 416 ms 95528 KB Output is correct
170 Correct 428 ms 93992 KB Output is correct
171 Correct 28 ms 47192 KB Output is correct
172 Correct 28 ms 47196 KB Output is correct
173 Correct 30 ms 47196 KB Output is correct
174 Correct 480 ms 121644 KB Output is correct
175 Correct 475 ms 121384 KB Output is correct
176 Correct 498 ms 122152 KB Output is correct
177 Correct 482 ms 121632 KB Output is correct
178 Correct 495 ms 122592 KB Output is correct
179 Correct 484 ms 122224 KB Output is correct
180 Correct 577 ms 129324 KB Output is correct
181 Correct 594 ms 131792 KB Output is correct
182 Correct 615 ms 130856 KB Output is correct
183 Correct 548 ms 117068 KB Output is correct
184 Correct 468 ms 121896 KB Output is correct
185 Correct 451 ms 121388 KB Output is correct
186 Correct 478 ms 121644 KB Output is correct
187 Correct 473 ms 121712 KB Output is correct
188 Correct 490 ms 122152 KB Output is correct
189 Correct 487 ms 121636 KB Output is correct
190 Correct 507 ms 124708 KB Output is correct
191 Correct 570 ms 130088 KB Output is correct
192 Correct 593 ms 121764 KB Output is correct
193 Correct 520 ms 116184 KB Output is correct
194 Correct 261 ms 76456 KB Output is correct
195 Correct 261 ms 77616 KB Output is correct
196 Correct 266 ms 77612 KB Output is correct
197 Correct 260 ms 77456 KB Output is correct
198 Correct 316 ms 80684 KB Output is correct
199 Correct 295 ms 80680 KB Output is correct
200 Correct 318 ms 80948 KB Output is correct
201 Correct 310 ms 80680 KB Output is correct
202 Correct 310 ms 80820 KB Output is correct
203 Correct 100 ms 43952 KB Output is correct
204 Correct 100 ms 43952 KB Output is correct
205 Correct 107 ms 45036 KB Output is correct
206 Correct 109 ms 45236 KB Output is correct
207 Correct 527 ms 125992 KB Output is correct
208 Correct 609 ms 126732 KB Output is correct
209 Correct 550 ms 117288 KB Output is correct
210 Correct 524 ms 124264 KB Output is correct
211 Correct 545 ms 114364 KB Output is correct
212 Correct 322 ms 88544 KB Output is correct
213 Correct 367 ms 96948 KB Output is correct
214 Correct 136 ms 73136 KB Output is correct