Submission #1192645
Source Code Expand
#include <cstdio>
#define N 200003
#define IL inline
#define REP(a,b,c) for(a=b;a<=c;a++)
using namespace std;
typedef long long lol;
lol ans;
struct edge{int u,v,w;};
struct Heap{
int len;edge a[N<<1];
IL void up(int i){
int j=i>>1,x=a[i].w;edge y=a[i];
while(j && x<a[j].w)a[i]=a[j],i=j,j>>=1;
a[i]=y;
}
IL void down(int i){
int j=i<<1,x=a[i].w;edge y=a[i];
while(j<=len){
if(j<len && a[j+1].w<a[j].w)j++;
if(a[j].w<x)a[i]=a[j],i=j,j<<=1;else break;
}
a[i]=y;
}
IL void push(edge b){a[++len]=b;up(len);}
IL edge pop(){edge b=a[1];a[1]=a[len--];down(1);return b;}
}O,H;
int n,q,fa[N];
IL int rd(){
int res=0;char c;while((c=getchar())<'0'||c>'9');
while(c>='0'&&c<='9')res=res*10+c-'0',c=getchar();return res;
}
IL int getf(int a){return a==fa[a]?a:fa[a]=getf(fa[a]);}
IL bool link(int a,int b){a=getf(a),b=getf(b);if(a==b)return false;fa[b]=a;return true;}
int main(){
n=rd(),q=rd();int t,i,u,v,w;edge e;t=n-1;REP(i,1,t)fa[i]=i;
REP(i,1,q)u=rd(),v=rd(),w=rd(),O.push((edge){u,v,w});
REP(t,2,n){
while(1){
if(O.len &&(!H.len || O.a[1].w<H.a[1].w)){
e=O.pop();
H.push((edge){e.u,(e.u+1)%n,e.w+1});
H.push((edge){e.v,(e.v+1)%n,e.w+2});
if(link(e.u,e.v)){ans+=e.w;break;}
}else{
e=H.a[1];
if(!link(e.u,e.v))H.pop();
else{
ans+=e.w;e.u=e.v,e.v=(e.v+1)%n,e.w+=2,H.a[1]=e,H.down(1);break;
}
}
}
}
printf("%lld",ans);
return 0;
}
Submission Info
Submission Time |
|
Task |
G - Zigzag MST |
User |
mytryer |
Language |
C++14 (GCC 5.4.1) |
Score |
1300 |
Code Size |
1464 Byte |
Status |
AC |
Exec Time |
95 ms |
Memory |
8320 KB |
Judge Result
Set Name |
sample |
all |
Score / Max Score |
0 / 0 |
1300 / 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 |
AC |
1 ms |
2176 KB |
01-02.txt |
AC |
27 ms |
4224 KB |
01-03.txt |
AC |
42 ms |
4992 KB |
01-04.txt |
AC |
3 ms |
2944 KB |
01-05.txt |
AC |
3 ms |
2944 KB |
01-06.txt |
AC |
3 ms |
2944 KB |
01-07.txt |
AC |
4 ms |
2944 KB |
01-08.txt |
AC |
5 ms |
2944 KB |
01-09.txt |
AC |
8 ms |
2944 KB |
01-10.txt |
AC |
28 ms |
4992 KB |
01-11.txt |
AC |
45 ms |
4736 KB |
01-12.txt |
AC |
49 ms |
4992 KB |
01-13.txt |
AC |
49 ms |
4992 KB |
01-14.txt |
AC |
49 ms |
4992 KB |
01-15.txt |
AC |
49 ms |
4992 KB |
01-16.txt |
AC |
49 ms |
4992 KB |
01-17.txt |
AC |
49 ms |
4992 KB |
01-18.txt |
AC |
58 ms |
8320 KB |
01-19.txt |
AC |
4 ms |
2944 KB |
01-20.txt |
AC |
14 ms |
3072 KB |
01-21.txt |
AC |
38 ms |
8320 KB |
01-22.txt |
AC |
70 ms |
8320 KB |
01-23.txt |
AC |
73 ms |
8320 KB |
01-24.txt |
AC |
6 ms |
2944 KB |
01-25.txt |
AC |
61 ms |
4992 KB |
01-26.txt |
AC |
3 ms |
2944 KB |
01-27.txt |
AC |
14 ms |
3200 KB |
01-28.txt |
AC |
63 ms |
5888 KB |
01-29.txt |
AC |
85 ms |
5632 KB |
01-30.txt |
AC |
95 ms |
6144 KB |
sample-01.txt |
AC |
1 ms |
2176 KB |
sample-02.txt |
AC |
1 ms |
2176 KB |
sample-03.txt |
AC |
1 ms |
2176 KB |