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

我AC了,也过了

Posted by lyh0313 at 2019-01-06 09:42:27 on Problem 1417
In Reply To:Re:谁能过这个数据 Posted by:jxufebingone at 2014-08-22 00:38:29
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<iostream>
#include<queue>
#include<iomanip>
#include<algorithm>
using namespace std;
const int N=610;

int n,p1,p2,id,sum[N];
int f[N],c[N],l0[N],l1[N],dp[N][N];
bool flag,ans[N][2];

int find(int x)
{
	if(f[x]==x) return x;
	int t=find(f[x]);
	c[x]=(c[x]^c[f[x]]);
	f[x]=t;
	return f[x];
}

bool toge(int x,int y,int k)
{
	int fx=find(x),fy=find(y);
	if(fx==fy)
	{
		if((c[x]^c[y])!=k) return false;
	}
	else
	{
		f[fy]=fx;
		c[fy]=(c[x]^c[y]^k);
	}
	return true;
}

void pri(int i,int j)
{
	if(i==0) return;
	if(j>=l0[i]&&dp[i-1][j-l0[i]]==1)
	{
		ans[i][0]=true;
		pri(i-1,j-l0[i]);
	}
	else
	{
		ans[i][1]=true;
		pri(i-1,j-l1[i]);
	}
}

int main()
{
	int i,j,m,k,a,b;
	char s[10];
	while(1)
	{
		scanf("%d%d%d\n",&m,&p1,&p2);
		n=p1+p2;
		if(m==0&&n==0) break;
		flag=true;
		for(i=1;i<=n;i++)
		{
			f[i]=i;
			c[i]=0;
		}
		for(i=1;i<=m;i++)
		{
			scanf("%d %d %s\n",&a,&b,s);
			if(!flag) continue;
			if(s[0]=='y') k=0;
			else k=1;
			if(!toge(a,b,k))
			{
				printf("no\n");
				flag=false;
			}
		}
		if(!flag) continue;
		id=0;
		for(i=1;i<=n;i++)
		{
			a=find(i);
			if(f[i]==i)
			{
				id++;
				sum[i]=id;
				l0[id]=1;l1[id]=0;
			}
		}
		for(i=1;i<=n;i++)
		if(f[i]!=i)
		{
			sum[i]=sum[f[i]];
			if(c[i]==0) l0[sum[i]]++;
			else l1[sum[i]]++;
		}
		memset(dp,0,sizeof(dp));
		dp[0][0]=1;dp[1][l0[1]]=1;dp[1][l1[1]]+=1;
		for(i=2;i<=id;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(j>=l0[i]) dp[i][j]=dp[i-1][j-l0[i]];
				if(j>=l1[i]) dp[i][j]+=dp[i-1][j-l1[i]];
			}
		}
		if(dp[id][p1]>1) printf("no\n");
		else
		{
			memset(ans,false,sizeof(ans));
			pri(id,p1);
			for(i=1;i<=n;i++)
				if(ans[sum[i]][c[i]]) printf("%d\n",i);
			printf("end\n");
		}
		
	}
	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