QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#145343 | #4272. Phone Plans | Leasier | 0 | 303ms | 106504kb | C++98 | 5.4kb | 2023-08-22 08:51:43 | 2023-08-22 08:51:44 |
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 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[200007];
bool vis[400007];
Edge edge[200007];
vector<int> v[200007];
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[200007], rnk[200007], pos2[400007];
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);
cnt1 = unique(w1 + 1, w1 + cnt1 + 1) - w1 - 1;
for (register int i = 1; i <= a; i++){
kru1.edge[i].dis = lower_bound(w1 + 1, w1 + cnt1 + 1, kru1.edge[i].dis) - w1;
}
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);
cnt2 = unique(w2 + 1, w2 + cnt2 + 1) - w2 - 1;
for (register int i = 1; i <= b; i++){
kru2.edge[i].dis = lower_bound(w2 + 1, w2 + cnt2 + 1, kru2.edge[i].dis) - w2;
}
kru2.kruskal(n, b, cnt2);
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]) + comb_2(l->second);
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;
for (register int k = (int)kru2.v[j].size() - 1; k >= 0; 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 {
cerr << ans;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 8ms
memory: 71700kb
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:
result:
wrong answer 1st lines differ - expected: '33', found: ''
Subtask #2:
score: 0
Wrong Answer
Test #53:
score: 0
Wrong Answer
time: 303ms
memory: 106504kb
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:
result:
wrong answer 1st lines differ - expected: '502149991', found: ''
Subtask #3:
score: 0
Wrong Answer
Test #103:
score: 0
Wrong Answer
time: 6ms
memory: 59964kb
input:
1 0 0 0
output:
result:
wrong answer 1st lines differ - expected: '0', found: ''
Subtask #4:
score: 0
Skipped
Dependency #1:
0%