QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#121489 | #4272. Phone Plans | bashkort# | 0 | 198ms | 61272kb | C++20 | 3.3kb | 2023-07-08 12:46:56 | 2024-07-04 00:30:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, ma, mb;
ll k;
cin >> n >> ma >> mb >> k;
vector<int> fa1(n), fa2(n), sz1(n), sz2(n);
vector<vector<int>> sub1(n), sub2(n);
vector<map<int, int>> cnt(n);
ll ans = 0;
function<int(int)> find1 = [&](int x) {
return x == fa1[x] ? x : find1(fa1[x]);
};
function<int(int)> find2 = [&](int x) {
return x == fa2[x] ? x : find2(fa2[x]);
};
iota(fa1.begin(), fa1.end(), 0), iota(fa2.begin(), fa2.end(), 0);
fill(sz1.begin(), sz1.end(), 1), fill(sz2.begin(), sz2.end(), 1);
for (int i = 0; i < n; ++i) {
sub1[i] = sub2[i] = {i};
cnt[i][i] += 1;
}
vector<array<int, 3>> ea(ma), eb(mb);
for (int i = 0; i < ma; ++i) {
int u, v, w;
cin >> u >> v >> w;
ea[i] = {w, u - 1, v - 1};
}
for (int i = 0; i < mb; ++i) {
int u, v, w;
cin >> u >> v >> w;
eb[i] = {w, u - 1, v - 1};
}
sort(ea.begin(), ea.end()), sort(eb.begin(), eb.end());
vector<int> stkSz;
vector<pair<int, int>> stk;
auto f = [&](int &x, int d) {
ans += 1LL * x * (x - 1) / 2;
x += d;
ans -= 1LL * x * (x - 1) / 2;
};
for (auto [w, a, b] : eb) {
a = find2(a), b = find2(b);
stkSz.push_back(stk.size());
if (a == b) {
continue;
}
if (sz2[a] < sz2[b]) {
swap(a, b);
}
ans += 1LL * sz2[a] * sz2[b];
stk.push_back({a, b});
fa2[b] = a;
sz2[a] += sz2[b];
sub2[a].insert(sub2[a].end(), sub2[b].begin(), sub2[b].end());
for (int x : sub2[b]) {
f(cnt[x][b], -1);
f(cnt[x][a], 1);
}
}
auto rollback = [&]() {
assert(!stkSz.empty());
while (stk.size() > stkSz.back()) {
auto [a, b] = stk.back();
stk.pop_back();
sz2[a] -= sz2[b];
ans -= 1LL * sz2[a] * sz2[b];
fa2[b] = b;
sub2[a].erase(sub2[a].end() - sz2[b], sub2[a].end());
for (int x : sub2[b]) {
f(cnt[x][a], -1);
f(cnt[x][b], 1);
}
}
stkSz.pop_back();
};
int best = 2e9 + 7;
for (int i = 0; i < ma; ++i) {
auto relax = [&]() {
if (ans >= k) {
best = min(best, (i == 0 ? 0 : ea[i - 1][0]) + (stkSz.empty() ? 0 : eb[stkSz.size() - 1][0]));
}
};
while (!stkSz.empty() && ans >= k) {
relax();
rollback();
}
relax();
if (i < ma) {
auto [w, a, b] = ea[i];
a = find1(a), b = find1(b);
if (a == b) {
continue;
}
if (sz1[a] < sz1[b]) {
swap(a, b);
}
ans += 1LL * sz1[a] * sz1[b];
for (auto [fx, x] : cnt[b]) {
ans -= 1LL * cnt[a][fx] * x;
cnt[a][fx] += x;
}
cnt[b].clear();
fa1[b] = a;
sz1[a] += sz1[b];
}
}
cout << (best > 2e9 ? -1 : best) << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 6
Accepted
time: 0ms
memory: 3532kb
input:
6 4 4 9 1 2 1 2 3 2 1 4 3 3 4 4 5 6 40 1 5 30 2 6 20 3 6 10
output:
33
result:
ok single line: '33'
Test #2:
score: -6
Wrong Answer
time: 0ms
memory: 3528kb
input:
1 0 0 0
output:
-1
result:
wrong answer 1st lines differ - expected: '0', found: '-1'
Subtask #2:
score: 0
Wrong Answer
Test #53:
score: 5
Accepted
time: 198ms
memory: 59452kb
input:
200000 100000 100000 7628995 23677 113459 839167193 165893 15365 669621527 26287 109671 214795757 156871 136723 522277985 9957 100463 806718116 104783 161385 156110975 184577 92225 545172629 57373 130083 980035338 167231 49597 919886451 115601 24325 717233004 99413 141311 737488449 83437 62759 76873...
output:
502149991
result:
ok single line: '502149991'
Test #54:
score: 0
Accepted
time: 143ms
memory: 49380kb
input:
200000 200000 87 2694 197233 86229 181875035 85167 196363 328068482 177795 65479 693403443 119609 181977 588691030 115815 139981 486110961 92473 20483 199129328 166989 95385 210368646 98095 54673 357307451 122543 94377 907487846 46611 80735 71787832 158893 117071 521262491 92051 104395 365725125 142...
output:
11965880
result:
ok single line: '11965880'
Test #55:
score: -5
Wrong Answer
time: 141ms
memory: 61272kb
input:
200000 103 199999 1593 75203 150269 64 68675 175215 100 176335 11837 94 33623 63279 56 16617 86741 63 167219 52783 58 6575 134399 1 144065 114171 2 32625 99459 50 35311 152509 36 132975 12211 8 175275 107903 23 17477 21319 95 157759 66683 71 34577 78339 26 154003 26703 18 187863 90945 74 116071 1089...
output:
7043338
result:
wrong answer 1st lines differ - expected: '7037526', found: '7043338'
Subtask #3:
score: 0
Wrong Answer
Test #103:
score: 0
Wrong Answer
time: 0ms
memory: 3784kb
input:
1 0 0 0
output:
-1
result:
wrong answer 1st lines differ - expected: '0', found: '-1'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%