Submission #3561913
Source Code Expand
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #define RG register #define LL long long using namespace std; const int N=2e5+10; struct node{ int a,b; LL c; }pt[N]; int n,m,cnt,tot,fa[N]; LL minn[N],ans; inline bool cmp(node a,node b){return a.c<b.c;} inline int read(){ char ch=getchar(); int x=0, f=1; while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){x=x*10+ch-'0'; ch=getchar(); } return x*f;} inline LL mi(LL a,LL b){return a<b ? a : b;} inline int f(int son){return fa[son]= fa[son]==son ? son : f(fa[son]);} int main() { //freopen("Zigzag MST.in","r",stdin); //freopen(".out","w",stdout); n=read(); m=read(); memset(minn,0x3f,sizeof minn); for(int i=0;i<n;i++) fa[i]=i; for(int i=1,a,b,c;i<=m;i++) { a=read()%n; b=read()%n; c=read(); pt[++cnt]={a,b,c}; minn[a]=mi(minn[a],c+1); minn[b]=mi(minn[b],c+2); } for(int i=1;i<2*n;i++) minn[i%n]=mi(minn[i%n],minn[(i-1)%n]+2); for(int i=0;i<n;i++) pt[++cnt]=(node){i%n,(i+1)%n,minn[i]}; sort(pt+1,pt+1+cnt,cmp); for(int i=1;i<=cnt;i++) { int fx=f(pt[i].a), fy=f(pt[i].b); if(fx==fy) continue; fa[fx]=fy; ans+=pt[i].c; if(++tot==n-1) return printf("%lld\n",ans); } //fclose(stdin); fclose(stdout); return 0; }
Submission Info
Submission Time | |
---|---|
Task | G - Zigzag MST |
User | vjudge4 |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 1256 Byte |
Status | RE |
Exec Time | 161 ms |
Memory | 7040 KB |
Judge Result
Set Name | sample | all | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 1300 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
sample | sample-01.txt, sample-02.txt, sample-03.txt |
all | 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 | RE | 2 ms | 3840 KB |
01-02.txt | RE | 44 ms | 5248 KB |
01-03.txt | RE | 161 ms | 5760 KB |
01-04.txt | RE | 33 ms | 7040 KB |
01-05.txt | RE | 23 ms | 6400 KB |
01-06.txt | RE | 28 ms | 6656 KB |
01-07.txt | RE | 23 ms | 5888 KB |
01-08.txt | RE | 113 ms | 5760 KB |
01-09.txt | RE | 114 ms | 5760 KB |
01-10.txt | RE | 132 ms | 5760 KB |
01-11.txt | RE | 149 ms | 5632 KB |
01-12.txt | RE | 159 ms | 5760 KB |
01-13.txt | RE | 158 ms | 5760 KB |
01-14.txt | RE | 158 ms | 5760 KB |
01-15.txt | RE | 155 ms | 5760 KB |
01-16.txt | RE | 155 ms | 5760 KB |
01-17.txt | RE | 158 ms | 5760 KB |
01-18.txt | RE | 146 ms | 5760 KB |
01-19.txt | RE | 23 ms | 5888 KB |
01-20.txt | RE | 111 ms | 5760 KB |
01-21.txt | RE | 124 ms | 5760 KB |
01-22.txt | RE | 145 ms | 5760 KB |
01-23.txt | RE | 144 ms | 5760 KB |
01-24.txt | RE | 116 ms | 5760 KB |
01-25.txt | RE | 147 ms | 5760 KB |
01-26.txt | RE | 32 ms | 5760 KB |
01-27.txt | RE | 114 ms | 5760 KB |
01-28.txt | RE | 138 ms | 5760 KB |
01-29.txt | RE | 148 ms | 5632 KB |
01-30.txt | RE | 156 ms | 5760 KB |
sample-01.txt | RE | 2 ms | 3840 KB |
sample-02.txt | RE | 2 ms | 3840 KB |
sample-03.txt | RE | 2 ms | 3840 KB |