Submission #682395

# Submission time Handle Problem Language Result Execution time Memory
682395 2023-01-16T07:59:24 Z lam timeismoney (balkan11_timeismoney) C++14
90 / 100
578 ms 924 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
#define ff first
#define ss second

const int maxn = 1e6 + 10;
int u[maxn],v[maxn],t[maxn],c[maxn],w[maxn],id[maxn],used[maxn];
int n,m;
int p[maxn],s[maxn];

void reset()
{
    for (int i=1; i<=n; i++) p[i]=i, s[i]=1;
}

int get(int x)
{
    return p[x]=(p[x]==x)?x:get(p[x]);
}

bool cmp(int x, int y)
{
    return w[x]<w[y];
}

bool hop(int x, int y)
{
    x=get(x); y=get(y);
    if (x==y) return 0;
    if (s[x]>s[y]) swap(x,y);
    s[y]+=s[x];
    p[x]=y;
    return 1;
}

ii mst(int T, int C)
{
//    cout<<T<<" +++ "<<C<<'\n';
    for (int i=1; i<=m; i++) w[i]=T*t[i]+C*c[i];
    sort(id+1,id+m+1,cmp);
    fill_n(used,m+1,0);
    ii ans={0,0};
    reset();
    for (int i=1; i<=m; i++)
    {
        if (hop(u[id[i]],v[id[i]]))
        {
//            cout<<u[id[i]]<<" = "<<v[id[i]]<<" !!!!!!!!!!!!\n";
            used[id[i]]=1;
            ans.ff+=t[id[i]];
            ans.ss+=c[id[i]];
        }
    }
    return ans;
}

bool straight(ii x, ii y, ii z)
{
    return x.ff*(y.ss-z.ss) + y.ff*(z.ss-x.ss) + z.ff*(x.ss-y.ss) >=0;
}

iii res;

void dnc(ii x, ii y)
{
    if (x==y) return;
//    cout<<x.ff<<','<<x.ss<<" : "<<y.ff<<','<<y.ss<<'\n';
    ii z = mst(x.ss-y.ss, y.ff-x.ff);
    res=min(res,{z.ff*z.ss,{x.ss-y.ss, y.ff-x.ff}});
    if (x==z||y==z||straight(x,y,z)) return;
    dnc(x,z); dnc(z,y);
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cin>>n>>m;
    for (int i=1; i<=m; i++)
    {
        cin>>u[i]>>v[i]>>t[i]>>c[i];
        u[i]++; v[i]++;
    }
    for (int i=1; i<=m; i++) id[i]=i;
    ii x = mst(1,0); ii y=mst(0,1);
    res={x.ff*x.ss,{1,0}};
    res=min(res,{y.ff*y.ss,{0,1}});
    dnc(x,y);
    x = mst(res.ss.ff,res.ss.ss);
    cout<<x.ff<<' '<<x.ss<<'\n';
    for (int i=1; i<=m; i++)
    {
        if (used[i]) cout<<u[i]-1<<' '<<v[i]-1<<'\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 5 ms 852 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 4 ms 340 KB Output is correct
15 Incorrect 3 ms 412 KB Output isn't correct
16 Correct 60 ms 488 KB Output is correct
17 Correct 62 ms 468 KB Output is correct
18 Correct 58 ms 468 KB Output is correct
19 Correct 570 ms 852 KB Output is correct
20 Incorrect 578 ms 924 KB Output isn't correct