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

静态字典树+并查集+欧拉回路 略超1s 差点爆内存 但代码风格较简洁

Posted by zengshiyuan at 2014-03-21 14:32:21 on Problem 2513
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int trie[510000][26],num[510000][26],degree[510001],f[500001],n,r;
//trie 字典树,num 记录当前这个color的序号,node记录每一种color的度,f 并查集 不用多说,n和r分别用于更新trie和num
int insert(char s[11])//插入一个节点(颜色),返回该种颜色的序号
{
	int i,j,l;
	j=0;
	l=strlen(s);
	for(i=0;i<l;i++)
		if(trie[j][s[i]-'a']) j=trie[j][s[i]-'a'];
		else j=trie[j][s[i]-'a']=++r;
	if (num[j][s[l-1]-'a']==0) num[j][s[l-1]-'a']=++n;
	return num[j][s[l-1]-'a'];
}
int find(int x)//并查集 
{
  if(x!=f[x]) f[x]=find(f[x]);
      return f[x];
}
int main()
{
	int i,j;
	char s1[11],s2[11];
	memset(trie,0,sizeof(trie));
	memset(num,0,sizeof(num));
	memset(degree,0,sizeof(degree));
	n=0;
	r=0;
	for(i=1;i<=500000;i++) f[i]=i;
	while(scanf("%s %s",&s1,&s2)!=EOF)
	{
		i=insert(s1);
		degree[i]++;
		j=insert(s2);
		degree[j]++;
		i=find(i);
		j=find(j);
		if(i!=j) f[j]=i;
	}
	int count=0;
	for(i=1;i<=n;i++)
	{
		if(i==find(i)) count++;
		if (count>1) break;
	}//判断图的连通性 即是否只有一个父亲
	if (count>1) printf("Impossible\n");
	else
	{
	    j=0;
	    for(i=1;i<=n;i++) if (degree[i]%2) j++;
	    if(j==0 || j==2) printf("Possible\n");//判断无向图欧拉回路的条件之一 度为奇数的点的个数要么0 要么2
	        else printf("Impossible\n");
	}
}

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