Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我的加读入优化后才30多行

Posted by xzy_xzy at 2019-06-22 20:06:36 on Problem 1182
#include <cstdio>

int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0' || c>'9'){if (c=='-')f=-1;c=getchar();}
	while (c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-48;c=getchar();}
	return x*f;
}

const int MAXN=150005;
int n,k,ans;
int f[MAXN];

inline int find(int x)
{
	return f[x]==x?x:f[x]=find(f[x]);
}

int main()
{
	n=read();k=read();
	for (int i=1;i<=3*n;i++) f[i]=i;
	int id,x,y;
	for (int i=1;i<=k;i++){
		id=read();x=read();y=read();
		if ((x>n||y>n)&&++ans)continue;
		if (id==1){
			if ((find(x)==find(y+n)||find(x+n)==find(y))&&++ans)continue;
			f[find(x)]=find(y);f[find(x+n)]=find(y+n);f[find(x+2*n)]=find(y+2*n);
		}
		else if (id==2){
			if ((find(x)==find(y)||find(x)==find(y+2*n))&&++ans)continue;
			f[find(x)]=find(y+n);f[find(x+2*n)]=find(y);f[find(x+n)]=find(y+2*n);
		}
	}
	printf("%d\n",ans);
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator