Submission #3210979


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--;
			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 0
Code Size 3247 Byte
Status WA
Exec Time 175 ms
Memory 26216 KB

Judge Result

Set Name sample all
Score / Max Score 0 / 0 0 / 1300
Status
AC × 2
WA × 1
AC × 6
WA × 30
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 26 ms 23528 KB
01-03.txt WA 54 ms 26216 KB
01-04.txt WA 9 ms 6528 KB
01-05.txt WA 10 ms 5760 KB
01-06.txt WA 12 ms 6400 KB
01-07.txt WA 14 ms 5376 KB
01-08.txt WA 17 ms 5376 KB
01-09.txt WA 29 ms 5880 KB
01-10.txt WA 52 ms 15084 KB
01-11.txt WA 49 ms 25960 KB
01-12.txt WA 69 ms 25320 KB
01-13.txt WA 69 ms 24936 KB
01-14.txt WA 69 ms 25832 KB
01-15.txt WA 69 ms 24552 KB
01-16.txt WA 69 ms 24936 KB
01-17.txt WA 69 ms 26216 KB
01-18.txt WA 106 ms 25448 KB
01-19.txt WA 10 ms 5376 KB
01-20.txt WA 32 ms 5376 KB
01-21.txt WA 78 ms 9580 KB
01-22.txt WA 165 ms 22120 KB
01-23.txt WA 168 ms 23400 KB
01-24.txt WA 14 ms 6004 KB
01-25.txt WA 91 ms 24552 KB
01-26.txt WA 8 ms 5120 KB
01-27.txt WA 28 ms 5752 KB
01-28.txt WA 114 ms 15848 KB
01-29.txt WA 162 ms 24808 KB
01-30.txt WA 175 ms 25960 KB
sample-01.txt WA 2 ms 4352 KB
sample-02.txt AC 2 ms 4352 KB
sample-03.txt AC 2 ms 4352 KB