Submission #6352834


Source Code Expand

Copy
//It is made by M_sea
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define re register
typedef long long ll;
using namespace std;

inline void chkmin(int& x,int y) { if (y<x) x=y; }
inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
    return X*w;
}

const int N=400000+10;

int n,m,Q;
int f[N],fa[N];

inline int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); }
inline void merge(int x,int y) {
    x=find(x),y=find(y);
    if (x!=y) fa[x]=y;
}

struct Edge { int u,v,w; } e[N];
bool operator <(Edge a,Edge b) { return a.w<b.w; }

int main() {
    n=read(),Q=read();
    memset(f,0x3f,sizeof(f));
    for (re int i=1;i<=Q;++i) {
        int a=read()+1,b=read()+1,c=read();
        chkmin(f[a%n+1],c+1),chkmin(f[b%n+1],c+2);
        e[++m]=(Edge){a,b,c};
    }
    int pos=1;
    for (re int i=2;i<=n;++i) if (f[i]<f[pos]) pos=i;
    for (re int i=pos%n+1,j=pos;i!=pos;j=i,i=i%n+1) chkmin(f[i],f[j]+2);
    for (re int i=1;i<n;++i) e[++m]=(Edge){i,i+1,f[i+1]};
    e[++m]=(Edge){n,1,f[1]};
    sort(e+1,e+m+1);
    for (re int i=1;i<=n;++i) fa[i]=i;
    int tot=0; ll ans=0;
    for (re int i=1;i<=m;++i) {
        int u=e[i].u,v=e[i].v,w=e[i].w;
        if (find(u)==find(v)) continue;
        ++tot,ans+=w,merge(u,v);
        if (tot==n-1) break;
    }
    printf("%lld\n",ans);
    return 0;
}
//qwqwq

Submission Info

Submission Time
Task G - Zigzag MST
User vjudge2
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 1495 Byte
Status
Exec Time 76 ms
Memory 9344 KB

Judge Result

Set Name Score / Max Score Test Cases
sample 0 / 0 sample-01.txt, sample-02.txt, sample-03.txt
all 1300 / 1300 sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01-01.txt 2 ms 3840 KB
01-02.txt 38 ms 7936 KB
01-03.txt 74 ms 8064 KB
01-04.txt 21 ms 9344 KB
01-05.txt 14 ms 8576 KB
01-06.txt 17 ms 8832 KB
01-07.txt 16 ms 8192 KB
01-08.txt 16 ms 8064 KB
01-09.txt 20 ms 7936 KB
01-10.txt 45 ms 7936 KB
01-11.txt 64 ms 7936 KB
01-12.txt 74 ms 7936 KB
01-13.txt 74 ms 7936 KB
01-14.txt 74 ms 7936 KB
01-15.txt 76 ms 7936 KB
01-16.txt 74 ms 7936 KB
01-17.txt 74 ms 7936 KB
01-18.txt 54 ms 7936 KB
01-19.txt 16 ms 8192 KB
01-20.txt 19 ms 7936 KB
01-21.txt 34 ms 7936 KB
01-22.txt 61 ms 7936 KB
01-23.txt 61 ms 7936 KB
01-24.txt 14 ms 7936 KB
01-25.txt 75 ms 7936 KB
01-26.txt 20 ms 7936 KB
01-27.txt 22 ms 7936 KB
01-28.txt 50 ms 7936 KB
01-29.txt 61 ms 7936 KB
01-30.txt 71 ms 7936 KB
sample-01.txt 2 ms 3840 KB
sample-02.txt 2 ms 3840 KB
sample-03.txt 2 ms 3840 KB