QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#145144 | #4272. Phone Plans | Leasier | 0 | 323ms | 125676kb | C++98 | 5.4kb | 2023-08-21 22:26:42 | 2023-08-21 22:26:43 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef struct Edge_tag {
int nxt;
int start;
int end;
int dis;
Edge_tag(){}
Edge_tag(int start_, int end_, int dis_){
start = start_;
end = end_;
dis = dis_;
}
} Edge;
inline ll comb_2(int n){
return (ll)n * (n - 1) / 2;
}
typedef struct {
int id;
int root[400007];
int val[400007];
int ls[400007];
int rs[400007];
int size[400007];
ll sum[400007];
bool vis[400007];
Edge edge[400007];
vector<int> v[400007];
inline void init(int n){
for (register int i = 1; i <= n; i++){
root[i] = i;
}
}
int get_root(int x){
if (root[x] == x) return x;
return root[x] = get_root(root[x]);
}
inline void kruskal(int n, int m, int k){
id = n;
init(n * 2 - 1);
for (register int i = 1; i <= n; i++){
size[i] = 1;
vis[i] = true;
}
sort(edge + 1, edge + m + 1);
for (register int i = 1; i <= m; i++){
int x_root = get_root(edge[i].start), y_root = get_root(edge[i].end);
if (x_root != y_root){
id++;
root[x_root] = root[y_root] = id;
val[id] = edge[i].dis;
vis[id] = true;
vis[x_root] = vis[y_root] = false;
if (size[x_root] > size[y_root]){
ls[id] = x_root;
rs[id] = y_root;
} else {
ls[id] = y_root;
rs[id] = x_root;
}
size[id] = size[x_root] + size[y_root];
sum[val[id]] += comb_2(size[id]) - comb_2(size[x_root]) - comb_2(size[y_root]);
v[val[id]].push_back(id);
}
}
for (register int i = 1; i <= k; i++){
sum[i] += sum[i - 1];
}
}
} Kruskal;
int dfn[400007];
typedef struct {
bool operator ()(const int a, const int b){
return dfn[a] > dfn[b];
}
} Compare;
Kruskal kru1, kru2;
int w1[200007], w2[200007], pos1[400007], ref[400007], rnk[200007], pos2[400007];
bool vis1[200007], vis2[200007];
set<int> s1[200007];
set<int, Compare> s2[200007];
map<int, int> mp[200007];
bool operator <(const Edge a, const Edge b){
return a.dis < b.dis;
}
inline int read_int(){
int ans = 0;
char ch = getchar();
while (ch < '0' || ch > '9'){
ch = getchar();
}
while (ch >= '0' && ch <= '9'){
ans = ans * 10 + (ch ^ 48);
ch = getchar();
}
return ans;
}
inline ll read_ll(){
ll ans = 0;
char ch = getchar();
while (ch < '0' || ch > '9'){
ch = getchar();
}
while (ch >= '0' && ch <= '9'){
ans = ans * 10 + (ch ^ 48);
ch = getchar();
}
return ans;
}
void dfs(int u, int n, int &id){
if (u <= n){
id++;
dfn[u] = id;
rnk[id] = u;
return;
}
dfs(kru2.ls[u], n, id);
dfn[u] = dfn[kru2.ls[u]];
dfs(kru2.rs[u], n, id);
}
int main(){
int n = read_int(), a = read_int(), b = read_int(), cnt1 = 0, cnt2 = 0, dfn_id = 0, set_id = 0, lst = -1, ans = 0x7fffffff;
ll k = read_ll(), sum = 0;
w1[++cnt1] = 0;
for (register int i = 1; i <= a; i++){
int u = read_int(), v = read_int(), l = read_int();
w1[++cnt1] = l;
kru1.edge[i] = Edge(u, v, l);
}
sort(w1 + 1, w1 + cnt1 + 1);
for (register int i = 1; i <= a; i++){
int x = lower_bound(w1 + 1, w1 + cnt1 + 1, kru1.edge[i].dis) - w1;
while (vis1[x]) x++;
vis1[x] = true;
kru1.edge[i].dis = x;
}
kru1.kruskal(n, a, cnt1);
w2[++cnt2] = 0;
for (register int i = 1; i <= b; i++){
int u = read_int(), v = read_int(), l = read_int();
w2[++cnt2] = l;
kru2.edge[i] = Edge(u, v, l);
}
sort(w2 + 1, w2 + cnt2 + 1);
for (register int i = 1; i <= b; i++){
int x = lower_bound(w2 + 1, w2 + cnt2 + 1, kru2.edge[i].dis) - w2;
while (vis2[x]) x++;
vis2[x] = true;
kru2.edge[i].dis = x;
}
kru2.kruskal(n, b, cnt2);
dfs(kru2.id, n, dfn_id);
for (register int i = 1; i <= n; i++){
pos1[i] = ref[i] = i;
s1[i].insert(i);
}
for (register int i = 1; i <= kru2.id; i++){
if (kru2.vis[i]){
int t = dfn_id + 1;
dfs(i, n, dfn_id);
pos2[i] = ++set_id;
for (register int j = t; j <= dfn_id; j++){
s2[set_id].insert(rnk[j]);
mp[rnk[j]][set_id] = 1;
}
}
}
for (register int i = 1, j = cnt2; i <= cnt1; i++){
int size = kru1.v[i].size();
for (register int k = 0; k < size; k++){
int x = kru1.v[i][k];
pos1[x] = pos1[kru1.ls[x]];
for (register set<int>::iterator l = s1[pos1[kru1.rs[x]]].begin(); l != s1[pos1[kru1.rs[x]]].end(); l++){
ref[*l] = pos1[x];
s1[pos1[x]].insert(*l);
}
s1[pos1[kru1.rs[x]]].clear();
for (register map<int, int>::iterator l = mp[pos1[kru1.rs[x]]].begin(); l != mp[pos1[kru1.rs[x]]].end(); l++){
sum -= comb_2(mp[pos1[x]][l->first]);
mp[pos1[x]][l->first] += l->second;
sum += comb_2(mp[pos1[x]][l->first]);
}
mp[pos1[kru1.rs[x]]].clear();
}
while (j >= 1 && kru1.sum[i] + kru2.sum[j] - sum >= k){
lst = j;
size = kru2.v[j].size();
for (register int k = 0; k < size; k++){
int x = kru2.v[j][k];
pos2[kru2.ls[x]] = pos2[x];
pos2[kru2.rs[x]] = ++set_id;
while (true){
int cur = *s2[pos2[x]].begin();
if (dfn[cur] < dfn[kru2.rs[x]]) break;
if (--mp[ref[cur]][pos2[x]] == 0){
mp[ref[cur]].erase(pos2[x]);
} else {
sum -= --mp[ref[cur]][pos2[x]];
}
s2[pos2[x]].erase(cur);
sum += mp[ref[cur]][set_id]++;
s2[set_id].insert(cur);
}
}
j--;
}
if (lst != -1) ans = min(ans, w1[i] + w2[lst]);
}
if (ans == 0x7fffffff){
cout << -1;
} else {
cout << ans;
}
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: 3ms
memory: 85120kb
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: 0
Accepted
time: 7ms
memory: 68692kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 1ms
memory: 71868kb
input:
2 0 0 1
output:
-1
result:
ok single line: '-1'
Test #4:
score: 0
Accepted
time: 3ms
memory: 83704kb
input:
2 10 10 1 1 1 915886339 2 2 430624192 1 1 879808770 1 2 577221915 1 1 439429731 1 2 304911826 1 1 148009333 1 2 51122687 1 1 558282772 1 2 421196385 2 1 436692145 1 2 654020563 2 2 162573477 2 2 989531779 1 1 646687051 2 2 549037477 2 2 699532275 1 1 679375858 2 1 83352965 2 1 415698228
output:
51122687
result:
ok single line: '51122687'
Test #5:
score: 0
Accepted
time: 3ms
memory: 80592kb
input:
2 10 10 1 1 1 1000000000 1 2 1000000000 2 2 1000000000 2 1 1000000000 1 2 1000000000 1 1 1000000000 1 2 1000000000 2 2 1000000000 1 2 1000000000 1 2 1000000000 2 1 1000000000 1 2 1000000000 2 1 1000000000 2 2 1000000000 1 2 1000000000 2 2 1000000000 1 1 1000000000 1 1 1000000000 2 2 1000000000 1 2 1...
output:
1000000000
result:
ok single line: '1000000000'
Test #6:
score: 0
Accepted
time: 58ms
memory: 79416kb
input:
2000 0 200000 1199833 636 1231 120395621 689 1640 497332138 1861 1980 798839749 560 1283 560726905 1332 328 431171189 1203 1764 466367210 1102 347 317109768 1462 789 761470540 350 1093 551905741 1718 1047 548650524 51 546 56733461 58 1519 744207013 826 956 943929583 1969 207 238061756 99 47 99683567...
output:
9768257
result:
ok single line: '9768257'
Test #7:
score: 0
Accepted
time: 60ms
memory: 81620kb
input:
2000 200000 0 1937051 1325 1770 628367155 105 1670 728177982 358 778 959944062 826 1691 651665248 1119 1906 382208628 1684 1232 677646622 807 265 902880211 1685 1660 405567549 1853 1982 988679307 1241 1054 385406922 862 1049 356441384 1837 673 443580113 1082 1795 738355567 1703 663 221254144 1792 84...
output:
20263921
result:
ok single line: '20263921'
Test #8:
score: 0
Accepted
time: 126ms
memory: 93872kb
input:
2000 200000 200000 1999000 1303 1065 135024256 1400 1409 157921645 1208 515 422761224 466 1398 267944161 40 1202 560552418 722 1817 773826339 1022 1534 720452215 1512 200 145291518 538 230 98848391 434 529 911575234 470 1050 103133355 1800 351 180303134 1527 1871 779519820 258 1872 279904732 1 512 4...
output:
1999
result:
ok single line: '1999'
Test #9:
score: -6
Wrong Answer
time: 118ms
memory: 89008kb
input:
2000 200000 200000 1999000 1566 1720 828051227 411 209 634755980 293 258 80524979 1149 1694 253684780 71 597 899974207 1934 440 11614281 1846 870 794303074 800 1090 576722282 1022 1619 486756658 1135 1004 589873220 1300 326 565865513 114 341 308227260 310 134 735603010 437 291 714079414 930 1131 136...
output:
2000
result:
wrong answer 1st lines differ - expected: '1999', found: '2000'
Subtask #2:
score: 0
Wrong Answer
Test #53:
score: 5
Accepted
time: 323ms
memory: 125676kb
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: 257ms
memory: 119228kb
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: 184ms
memory: 119856kb
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:
124834841
result:
wrong answer 1st lines differ - expected: '7037526', found: '124834841'
Subtask #3:
score: 0
Time Limit Exceeded
Test #103:
score: 6
Accepted
time: 4ms
memory: 68516kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #104:
score: 0
Accepted
time: 12ms
memory: 68564kb
input:
2 0 0 1
output:
-1
result:
ok single line: '-1'
Test #105:
score: 0
Accepted
time: 58ms
memory: 82956kb
input:
2 10 200000 1 2 1 319832429 1 1 412617159 2 1 337734185 1 2 674652880 1 2 454610992 2 2 688717944 1 1 189208056 2 2 951221780 1 2 594191431 2 2 87592160 1 2 833491749 2 2 732961971 2 1 575969648 2 2 268359887 2 1 436382098 1 2 514959278 1 2 305446083 1 2 365989813 1 2 296840111 1 1 541558213 1 1 497...
output:
10104
result:
ok single line: '10104'
Test #106:
score: -6
Time Limit Exceeded
input:
2 10 200000 1 1 1 1000000000 1 1 1000000000 1 2 1000000000 1 2 1000000000 1 2 1000000000 1 1 1000000000 2 1 1000000000 2 2 1000000000 2 2 1000000000 2 2 1000000000 1 1 1000000000 1 2 1000000000 1 1 1000000000 2 1 1000000000 1 1 1000000000 1 1 1000000000 2 1 1000000000 1 1 1000000000 2 2 1000000000 2...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%