답안 #733958

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
733958 2023-05-01T12:23:59 Z loctildore Phone Plans (CCO22_day2problem2) C++17
25 / 25
959 ms 131904 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 13 ms 23776 KB Output is correct
3 Correct 15 ms 23780 KB Output is correct
4 Correct 12 ms 23824 KB Output is correct
5 Correct 11 ms 23764 KB Output is correct
6 Correct 81 ms 30668 KB Output is correct
7 Correct 84 ms 30636 KB Output is correct
8 Correct 154 ms 39724 KB Output is correct
9 Correct 153 ms 39604 KB Output is correct
10 Correct 145 ms 39568 KB Output is correct
11 Correct 148 ms 39612 KB Output is correct
12 Correct 13 ms 24020 KB Output is correct
13 Correct 12 ms 24020 KB Output is correct
14 Correct 13 ms 24020 KB Output is correct
15 Correct 149 ms 39660 KB Output is correct
16 Correct 145 ms 39724 KB Output is correct
17 Correct 151 ms 39664 KB Output is correct
18 Correct 155 ms 39668 KB Output is correct
19 Correct 158 ms 39612 KB Output is correct
20 Correct 147 ms 39704 KB Output is correct
21 Correct 150 ms 39760 KB Output is correct
22 Correct 179 ms 39680 KB Output is correct
23 Correct 147 ms 39616 KB Output is correct
24 Correct 149 ms 39612 KB Output is correct
25 Correct 158 ms 39624 KB Output is correct
26 Correct 153 ms 39644 KB Output is correct
27 Correct 147 ms 39628 KB Output is correct
28 Correct 147 ms 39632 KB Output is correct
29 Correct 171 ms 39612 KB Output is correct
30 Correct 159 ms 39680 KB Output is correct
31 Correct 156 ms 39724 KB Output is correct
32 Correct 175 ms 39632 KB Output is correct
33 Correct 143 ms 39416 KB Output is correct
34 Correct 142 ms 39372 KB Output is correct
35 Correct 145 ms 39440 KB Output is correct
36 Correct 141 ms 39444 KB Output is correct
37 Correct 15 ms 24308 KB Output is correct
38 Correct 15 ms 24276 KB Output is correct
39 Correct 16 ms 24208 KB Output is correct
40 Correct 14 ms 24196 KB Output is correct
41 Correct 127 ms 39380 KB Output is correct
42 Correct 132 ms 39312 KB Output is correct
43 Correct 148 ms 39324 KB Output is correct
44 Correct 145 ms 39376 KB Output is correct
45 Correct 153 ms 39696 KB Output is correct
46 Correct 150 ms 39628 KB Output is correct
47 Correct 151 ms 39848 KB Output is correct
48 Correct 151 ms 39596 KB Output is correct
49 Correct 158 ms 39628 KB Output is correct
50 Correct 14 ms 24404 KB Output is correct
51 Correct 84 ms 30776 KB Output is correct
52 Correct 87 ms 30536 KB Output is correct
53 Correct 12 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 409 ms 84780 KB Output is correct
2 Correct 342 ms 78932 KB Output is correct
3 Correct 170 ms 66564 KB Output is correct
4 Correct 13 ms 23740 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23828 KB Output is correct
7 Correct 149 ms 44052 KB Output is correct
8 Correct 124 ms 44428 KB Output is correct
9 Correct 190 ms 69640 KB Output is correct
10 Correct 342 ms 79004 KB Output is correct
11 Correct 461 ms 88208 KB Output is correct
12 Correct 486 ms 88236 KB Output is correct
13 Correct 457 ms 88200 KB Output is correct
14 Correct 485 ms 88352 KB Output is correct
15 Correct 45 ms 47288 KB Output is correct
16 Correct 44 ms 47180 KB Output is correct
17 Correct 46 ms 47288 KB Output is correct
18 Correct 506 ms 97900 KB Output is correct
19 Correct 531 ms 98036 KB Output is correct
20 Correct 547 ms 98036 KB Output is correct
21 Correct 624 ms 97624 KB Output is correct
22 Correct 547 ms 98400 KB Output is correct
23 Correct 563 ms 98284 KB Output is correct
24 Correct 603 ms 101300 KB Output is correct
25 Correct 625 ms 103836 KB Output is correct
26 Correct 635 ms 107504 KB Output is correct
27 Correct 703 ms 110380 KB Output is correct
28 Correct 547 ms 97928 KB Output is correct
29 Correct 528 ms 98048 KB Output is correct
30 Correct 535 ms 98264 KB Output is correct
31 Correct 551 ms 97840 KB Output is correct
32 Correct 531 ms 97780 KB Output is correct
33 Correct 506 ms 97960 KB Output is correct
34 Correct 533 ms 100724 KB Output is correct
35 Correct 605 ms 102800 KB Output is correct
36 Correct 619 ms 105896 KB Output is correct
37 Correct 676 ms 108968 KB Output is correct
38 Correct 350 ms 71316 KB Output is correct
39 Correct 373 ms 71252 KB Output is correct
40 Correct 359 ms 71116 KB Output is correct
41 Correct 345 ms 71220 KB Output is correct
42 Correct 147 ms 44156 KB Output is correct
43 Correct 140 ms 43944 KB Output is correct
44 Correct 159 ms 45060 KB Output is correct
45 Correct 146 ms 45132 KB Output is correct
46 Correct 520 ms 99200 KB Output is correct
47 Correct 562 ms 103492 KB Output is correct
48 Correct 613 ms 110896 KB Output is correct
49 Correct 477 ms 98148 KB Output is correct
50 Correct 607 ms 107900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23732 KB Output is correct
2 Correct 13 ms 23824 KB Output is correct
3 Correct 86 ms 32624 KB Output is correct
4 Correct 76 ms 32636 KB Output is correct
5 Correct 204 ms 72092 KB Output is correct
6 Correct 461 ms 96240 KB Output is correct
7 Correct 276 ms 79140 KB Output is correct
8 Correct 37 ms 47252 KB Output is correct
9 Correct 308 ms 77680 KB Output is correct
10 Correct 279 ms 77748 KB Output is correct
11 Correct 276 ms 77744 KB Output is correct
12 Correct 274 ms 77576 KB Output is correct
13 Correct 41 ms 47188 KB Output is correct
14 Correct 37 ms 47180 KB Output is correct
15 Correct 38 ms 47304 KB Output is correct
16 Correct 201 ms 73120 KB Output is correct
17 Correct 209 ms 73088 KB Output is correct
18 Correct 196 ms 73212 KB Output is correct
19 Correct 198 ms 73084 KB Output is correct
20 Correct 209 ms 73200 KB Output is correct
21 Correct 208 ms 73640 KB Output is correct
22 Correct 264 ms 79944 KB Output is correct
23 Correct 319 ms 82956 KB Output is correct
24 Correct 388 ms 90552 KB Output is correct
25 Correct 478 ms 96340 KB Output is correct
26 Correct 206 ms 72900 KB Output is correct
27 Correct 205 ms 73028 KB Output is correct
28 Correct 202 ms 72988 KB Output is correct
29 Correct 200 ms 72980 KB Output is correct
30 Correct 205 ms 72900 KB Output is correct
31 Correct 206 ms 73172 KB Output is correct
32 Correct 289 ms 79716 KB Output is correct
33 Correct 339 ms 83484 KB Output is correct
34 Correct 499 ms 95220 KB Output is correct
35 Correct 562 ms 96504 KB Output is correct
36 Correct 211 ms 61304 KB Output is correct
37 Correct 208 ms 61292 KB Output is correct
38 Correct 200 ms 61276 KB Output is correct
39 Correct 204 ms 61444 KB Output is correct
40 Correct 222 ms 61576 KB Output is correct
41 Correct 218 ms 61460 KB Output is correct
42 Correct 209 ms 61428 KB Output is correct
43 Correct 216 ms 61312 KB Output is correct
44 Correct 235 ms 61372 KB Output is correct
45 Correct 223 ms 61360 KB Output is correct
46 Correct 78 ms 32932 KB Output is correct
47 Correct 72 ms 33076 KB Output is correct
48 Correct 80 ms 33584 KB Output is correct
49 Correct 80 ms 33528 KB Output is correct
50 Correct 296 ms 77312 KB Output is correct
51 Correct 437 ms 93000 KB Output is correct
52 Correct 498 ms 95956 KB Output is correct
53 Correct 195 ms 71724 KB Output is correct
54 Correct 390 ms 88496 KB Output is correct
55 Correct 211 ms 67896 KB Output is correct
56 Correct 38 ms 47256 KB Output is correct
57 Correct 40 ms 47188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 13 ms 23776 KB Output is correct
3 Correct 15 ms 23780 KB Output is correct
4 Correct 12 ms 23824 KB Output is correct
5 Correct 11 ms 23764 KB Output is correct
6 Correct 81 ms 30668 KB Output is correct
7 Correct 84 ms 30636 KB Output is correct
8 Correct 154 ms 39724 KB Output is correct
9 Correct 153 ms 39604 KB Output is correct
10 Correct 145 ms 39568 KB Output is correct
11 Correct 148 ms 39612 KB Output is correct
12 Correct 13 ms 24020 KB Output is correct
13 Correct 12 ms 24020 KB Output is correct
14 Correct 13 ms 24020 KB Output is correct
15 Correct 149 ms 39660 KB Output is correct
16 Correct 145 ms 39724 KB Output is correct
17 Correct 151 ms 39664 KB Output is correct
18 Correct 155 ms 39668 KB Output is correct
19 Correct 158 ms 39612 KB Output is correct
20 Correct 147 ms 39704 KB Output is correct
21 Correct 150 ms 39760 KB Output is correct
22 Correct 179 ms 39680 KB Output is correct
23 Correct 147 ms 39616 KB Output is correct
24 Correct 149 ms 39612 KB Output is correct
25 Correct 158 ms 39624 KB Output is correct
26 Correct 153 ms 39644 KB Output is correct
27 Correct 147 ms 39628 KB Output is correct
28 Correct 147 ms 39632 KB Output is correct
29 Correct 171 ms 39612 KB Output is correct
30 Correct 159 ms 39680 KB Output is correct
31 Correct 156 ms 39724 KB Output is correct
32 Correct 175 ms 39632 KB Output is correct
33 Correct 143 ms 39416 KB Output is correct
34 Correct 142 ms 39372 KB Output is correct
35 Correct 145 ms 39440 KB Output is correct
36 Correct 141 ms 39444 KB Output is correct
37 Correct 15 ms 24308 KB Output is correct
38 Correct 15 ms 24276 KB Output is correct
39 Correct 16 ms 24208 KB Output is correct
40 Correct 14 ms 24196 KB Output is correct
41 Correct 127 ms 39380 KB Output is correct
42 Correct 132 ms 39312 KB Output is correct
43 Correct 148 ms 39324 KB Output is correct
44 Correct 145 ms 39376 KB Output is correct
45 Correct 153 ms 39696 KB Output is correct
46 Correct 150 ms 39628 KB Output is correct
47 Correct 151 ms 39848 KB Output is correct
48 Correct 151 ms 39596 KB Output is correct
49 Correct 158 ms 39628 KB Output is correct
50 Correct 14 ms 24404 KB Output is correct
51 Correct 84 ms 30776 KB Output is correct
52 Correct 87 ms 30536 KB Output is correct
53 Correct 12 ms 23764 KB Output is correct
54 Correct 409 ms 84780 KB Output is correct
55 Correct 342 ms 78932 KB Output is correct
56 Correct 170 ms 66564 KB Output is correct
57 Correct 13 ms 23740 KB Output is correct
58 Correct 12 ms 23764 KB Output is correct
59 Correct 12 ms 23828 KB Output is correct
60 Correct 149 ms 44052 KB Output is correct
61 Correct 124 ms 44428 KB Output is correct
62 Correct 190 ms 69640 KB Output is correct
63 Correct 342 ms 79004 KB Output is correct
64 Correct 461 ms 88208 KB Output is correct
65 Correct 486 ms 88236 KB Output is correct
66 Correct 457 ms 88200 KB Output is correct
67 Correct 485 ms 88352 KB Output is correct
68 Correct 45 ms 47288 KB Output is correct
69 Correct 44 ms 47180 KB Output is correct
70 Correct 46 ms 47288 KB Output is correct
71 Correct 506 ms 97900 KB Output is correct
72 Correct 531 ms 98036 KB Output is correct
73 Correct 547 ms 98036 KB Output is correct
74 Correct 624 ms 97624 KB Output is correct
75 Correct 547 ms 98400 KB Output is correct
76 Correct 563 ms 98284 KB Output is correct
77 Correct 603 ms 101300 KB Output is correct
78 Correct 625 ms 103836 KB Output is correct
79 Correct 635 ms 107504 KB Output is correct
80 Correct 703 ms 110380 KB Output is correct
81 Correct 547 ms 97928 KB Output is correct
82 Correct 528 ms 98048 KB Output is correct
83 Correct 535 ms 98264 KB Output is correct
84 Correct 551 ms 97840 KB Output is correct
85 Correct 531 ms 97780 KB Output is correct
86 Correct 506 ms 97960 KB Output is correct
87 Correct 533 ms 100724 KB Output is correct
88 Correct 605 ms 102800 KB Output is correct
89 Correct 619 ms 105896 KB Output is correct
90 Correct 676 ms 108968 KB Output is correct
91 Correct 350 ms 71316 KB Output is correct
92 Correct 373 ms 71252 KB Output is correct
93 Correct 359 ms 71116 KB Output is correct
94 Correct 345 ms 71220 KB Output is correct
95 Correct 147 ms 44156 KB Output is correct
96 Correct 140 ms 43944 KB Output is correct
97 Correct 159 ms 45060 KB Output is correct
98 Correct 146 ms 45132 KB Output is correct
99 Correct 520 ms 99200 KB Output is correct
100 Correct 562 ms 103492 KB Output is correct
101 Correct 613 ms 110896 KB Output is correct
102 Correct 477 ms 98148 KB Output is correct
103 Correct 607 ms 107900 KB Output is correct
104 Correct 13 ms 23732 KB Output is correct
105 Correct 13 ms 23824 KB Output is correct
106 Correct 86 ms 32624 KB Output is correct
107 Correct 76 ms 32636 KB Output is correct
108 Correct 204 ms 72092 KB Output is correct
109 Correct 461 ms 96240 KB Output is correct
110 Correct 276 ms 79140 KB Output is correct
111 Correct 37 ms 47252 KB Output is correct
112 Correct 308 ms 77680 KB Output is correct
113 Correct 279 ms 77748 KB Output is correct
114 Correct 276 ms 77744 KB Output is correct
115 Correct 274 ms 77576 KB Output is correct
116 Correct 41 ms 47188 KB Output is correct
117 Correct 37 ms 47180 KB Output is correct
118 Correct 38 ms 47304 KB Output is correct
119 Correct 201 ms 73120 KB Output is correct
120 Correct 209 ms 73088 KB Output is correct
121 Correct 196 ms 73212 KB Output is correct
122 Correct 198 ms 73084 KB Output is correct
123 Correct 209 ms 73200 KB Output is correct
124 Correct 208 ms 73640 KB Output is correct
125 Correct 264 ms 79944 KB Output is correct
126 Correct 319 ms 82956 KB Output is correct
127 Correct 388 ms 90552 KB Output is correct
128 Correct 478 ms 96340 KB Output is correct
129 Correct 206 ms 72900 KB Output is correct
130 Correct 205 ms 73028 KB Output is correct
131 Correct 202 ms 72988 KB Output is correct
132 Correct 200 ms 72980 KB Output is correct
133 Correct 205 ms 72900 KB Output is correct
134 Correct 206 ms 73172 KB Output is correct
135 Correct 289 ms 79716 KB Output is correct
136 Correct 339 ms 83484 KB Output is correct
137 Correct 499 ms 95220 KB Output is correct
138 Correct 562 ms 96504 KB Output is correct
139 Correct 211 ms 61304 KB Output is correct
140 Correct 208 ms 61292 KB Output is correct
141 Correct 200 ms 61276 KB Output is correct
142 Correct 204 ms 61444 KB Output is correct
143 Correct 222 ms 61576 KB Output is correct
144 Correct 218 ms 61460 KB Output is correct
145 Correct 209 ms 61428 KB Output is correct
146 Correct 216 ms 61312 KB Output is correct
147 Correct 235 ms 61372 KB Output is correct
148 Correct 223 ms 61360 KB Output is correct
149 Correct 78 ms 32932 KB Output is correct
150 Correct 72 ms 33076 KB Output is correct
151 Correct 80 ms 33584 KB Output is correct
152 Correct 80 ms 33528 KB Output is correct
153 Correct 296 ms 77312 KB Output is correct
154 Correct 437 ms 93000 KB Output is correct
155 Correct 498 ms 95956 KB Output is correct
156 Correct 195 ms 71724 KB Output is correct
157 Correct 390 ms 88496 KB Output is correct
158 Correct 211 ms 67896 KB Output is correct
159 Correct 38 ms 47256 KB Output is correct
160 Correct 40 ms 47188 KB Output is correct
161 Correct 13 ms 23756 KB Output is correct
162 Correct 13 ms 23832 KB Output is correct
163 Correct 137 ms 44024 KB Output is correct
164 Correct 127 ms 44444 KB Output is correct
165 Correct 294 ms 79612 KB Output is correct
166 Correct 549 ms 96300 KB Output is correct
167 Correct 677 ms 94180 KB Output is correct
168 Correct 660 ms 95564 KB Output is correct
169 Correct 660 ms 95592 KB Output is correct
170 Correct 639 ms 93984 KB Output is correct
171 Correct 36 ms 47252 KB Output is correct
172 Correct 38 ms 47208 KB Output is correct
173 Correct 41 ms 47180 KB Output is correct
174 Correct 687 ms 121644 KB Output is correct
175 Correct 665 ms 121420 KB Output is correct
176 Correct 703 ms 122164 KB Output is correct
177 Correct 705 ms 121640 KB Output is correct
178 Correct 683 ms 122492 KB Output is correct
179 Correct 750 ms 122224 KB Output is correct
180 Correct 851 ms 129236 KB Output is correct
181 Correct 930 ms 131904 KB Output is correct
182 Correct 920 ms 131044 KB Output is correct
183 Correct 851 ms 116992 KB Output is correct
184 Correct 694 ms 121992 KB Output is correct
185 Correct 694 ms 121380 KB Output is correct
186 Correct 699 ms 121616 KB Output is correct
187 Correct 684 ms 121640 KB Output is correct
188 Correct 693 ms 122020 KB Output is correct
189 Correct 693 ms 121520 KB Output is correct
190 Correct 759 ms 124684 KB Output is correct
191 Correct 864 ms 130016 KB Output is correct
192 Correct 906 ms 121896 KB Output is correct
193 Correct 944 ms 116264 KB Output is correct
194 Correct 420 ms 76336 KB Output is correct
195 Correct 417 ms 77532 KB Output is correct
196 Correct 406 ms 77628 KB Output is correct
197 Correct 402 ms 77420 KB Output is correct
198 Correct 460 ms 80560 KB Output is correct
199 Correct 552 ms 80508 KB Output is correct
200 Correct 524 ms 80756 KB Output is correct
201 Correct 498 ms 80592 KB Output is correct
202 Correct 552 ms 80684 KB Output is correct
203 Correct 149 ms 44028 KB Output is correct
204 Correct 159 ms 43964 KB Output is correct
205 Correct 162 ms 45124 KB Output is correct
206 Correct 158 ms 45144 KB Output is correct
207 Correct 829 ms 125988 KB Output is correct
208 Correct 959 ms 126696 KB Output is correct
209 Correct 857 ms 117272 KB Output is correct
210 Correct 788 ms 124276 KB Output is correct
211 Correct 828 ms 114344 KB Output is correct
212 Correct 514 ms 88776 KB Output is correct
213 Correct 546 ms 96668 KB Output is correct
214 Correct 214 ms 73064 KB Output is correct