Submission #3211024


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define LB long double
#define ull unsigned long long
#define x first
#define y second
#define pb push_back
#define pf push_front
#define mp make_pair
#define Pair pair<int,int>
#define pLL pair<LL,LL>
#define pii pair<double,double>

const int INF=2e9;
const LL LINF=2e16;
const int magic=348;
const int MOD=998244353;
const double eps=1e-10;
const double pi=acos(-1);

struct fastio
{
	static const int S=1e7;
	char rbuf[S+48],wbuf[S+48];int rpos,wpos,len;
	fastio() {rpos=len=wpos=0;}
	inline char Getchar()
	{
		if (rpos==len) rpos=0,len=fread(rbuf,1,S,stdin);
		if (!len) return EOF;
		return rbuf[rpos++];
	}
	template <class T> inline void Get(T &x)
	{
		char ch;bool f;T res;
		while (!isdigit(ch=Getchar()) && ch!='-') {}
		if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
		while (isdigit(ch=Getchar())) res=res*10+ch-'0';
		x=(f?res:-res);
	}
	inline void getstring(char *s)
	{
		char ch;
		while ((ch=Getchar())<=32) {}
		for (;ch>32;ch=Getchar()) *s++=ch;
		*s='\0';
	}
	inline void flush() {fwrite(wbuf,1,wpos,stdout);fflush(stdout);wpos=0;}
	inline void Writechar(char ch)
	{
		if (wpos==S) flush();
		wbuf[wpos++]=ch;
	}
	template <class T> inline void Print(T x,char ch)
	{
		char s[20];int pt=0;
		if (x==0) s[++pt]='0';
		else
		{
			if (x<0) Writechar('-'),x=-x;
			while (x) s[++pt]='0'+x%10,x/=10;
		}
		while (pt) Writechar(s[pt--]);
		Writechar(ch);
	}
	inline void printstring(char *s)
	{
		int pt=1;
		while (s[pt]!='\0') Writechar(s[pt++]);
	}
}io;

const int MAXN=2e5;

int n,m;

struct node
{
	int A,B,C;
	inline void input() {io.Get(A);io.Get(B);io.Get(C);}
}a[MAXN+48];

inline Pair query(int id,int ti)
{
	if (ti==1) return mp(a[id].A,a[id].B);
	if ((a[id].A+1)%n==a[id].B && !(ti&1)) return mp(0,0);
	if (ti&1)
	{
		Pair res=mp(a[id].B+ti/2-1,a[id].B+ti/2);
		res.x%=n;res.y%=n;
		return res;
	}
	else
	{
		Pair res=mp(a[id].A+ti/2-1,a[id].A+ti/2);
		res.x%=n;res.y%=n;
		return res;
	}
}

namespace DSU
{
	int pre[MAXN+48];
	inline void init() {for (register int i=0;i<=n;i++) pre[i]=i;}
	inline int find_anc(int x) {if (pre[x]!=x) pre[x]=find_anc(pre[x]);return pre[x];}
	inline bool issame(int x,int y) {return find_anc(x)==find_anc(y);}
	inline void update(int x,int y) {x=find_anc(x);y=find_anc(y);pre[x]=y;}
}

priority_queue<pair<int,pair<Pair,bool> > > q;

int main ()
{
	// freopen ("a.in","r",stdin);
	// freopen ("a.out","w",stdout);
	io.Get(n);io.Get(m);
	for (register int i=1;i<=m;i++)
	{
		a[i].input();
		q.push(mp(-a[i].C,mp(mp(a[i].A,a[i].B),false)));
		if (a[i].A+1!=a[i].B) q.push(mp(-a[i].C-1,mp(mp(a[i].A,(a[i].A+1)%n),true)));
		q.push(mp(-a[i].C-2,mp(mp(a[i].B,(a[i].B+1)%n),true)));
	}
	LL ans=0;DSU::init();
	int cc=0;
	for (register int cnt=0;cnt<n-1;)
	{
		++cc;
		int len=q.top().x;Pair res=q.top().y.x;bool type=q.top().y.y;q.pop();
		if (DSU::issame(res.x,res.y)) continue;
		cnt++;ans-=len;
		DSU::update(res.x,res.y);
		if (type)
		{
			res.x=(res.x+1)%n;res.y=(res.y+1)%n;
			len-=2;
			q.push(mp(len,mp(res,type)));
		}
	}
	io.Print(ans,'\n');
	io.flush();return 0;
}

Submission Info

Submission Time
Task G - Zigzag MST
User IcePrincess_1968
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 3248 Byte
Status AC
Exec Time 176 ms
Memory 26088 KB

Judge Result

Set Name sample all
Score / Max Score 0 / 0 1300 / 1300
Status
AC × 3
AC × 36
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 2 ms 4352 KB
01-02.txt AC 25 ms 22120 KB
01-03.txt AC 53 ms 25192 KB
01-04.txt AC 9 ms 6528 KB
01-05.txt AC 9 ms 5760 KB
01-06.txt AC 11 ms 6016 KB
01-07.txt AC 13 ms 5376 KB
01-08.txt AC 21 ms 5376 KB
01-09.txt AC 33 ms 5880 KB
01-10.txt AC 56 ms 15080 KB
01-11.txt AC 50 ms 25320 KB
01-12.txt AC 73 ms 25448 KB
01-13.txt AC 73 ms 26088 KB
01-14.txt AC 72 ms 24552 KB
01-15.txt AC 72 ms 24296 KB
01-16.txt AC 73 ms 25576 KB
01-17.txt AC 73 ms 25704 KB
01-18.txt AC 105 ms 24680 KB
01-19.txt AC 9 ms 5376 KB
01-20.txt AC 33 ms 5376 KB
01-21.txt AC 80 ms 9452 KB
01-22.txt AC 166 ms 22376 KB
01-23.txt AC 172 ms 24040 KB
01-24.txt AC 12 ms 6004 KB
01-25.txt AC 121 ms 24424 KB
01-26.txt AC 8 ms 5120 KB
01-27.txt AC 28 ms 5752 KB
01-28.txt AC 114 ms 15720 KB
01-29.txt AC 159 ms 24296 KB
01-30.txt AC 176 ms 26088 KB
sample-01.txt AC 2 ms 4352 KB
sample-02.txt AC 2 ms 4352 KB
sample-03.txt AC 2 ms 4352 KB