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

Re:1A纪念

Posted by 909475573 at 2013-05-31 23:00:49 on Problem 1112
In Reply To:1A纪念 Posted by:909475573 at 2013-05-31 23:00:36
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
bool g[110][110];
int n,cont;
bool f[110][210],h[110][210];
bool d[110];
short v[110],w[110][2][110];
int a[110],b[110],x,y;
bool divide(int st)
{
	queue<int> q;
	int s,d,i;x=y=0;
	q.push(st);
	v[st]=1;x++;w[cont][1][x]=st;
	while(q.size())
	{
		s=q.front();
		q.pop();
		for(i=1;i<=n;i++)
		{
			if(g[s][i])
			{
				if(v[i]==-1)
				{
					v[i]=!v[s];
					if(v[i])
						x++,w[cont][1][x]=i;
					else
						y++,w[cont][0][y]=i;
					q.push(i);
				}
				else if(v[i]==v[s])
					return 0;
			}
		}
	}
	return 1;
}
void dfs(int num,int st)
{
	//cout<<num<<'-'<<st<<endl;
	if(num==0)
	{
		printf("%d ",cont);
		return;
	}
	if(h[num][st])
	{
		cont+=a[num];
		dfs(num-1,st-a[num]+b[num]);
		for(int i=1;i<=a[num];i++)
		{
			printf("%d ",w[num][1][i]);
			v[w[num][1][i]]=1;
		}
	}
	else
	{
		cont+=b[num];
		dfs(num-1,st+a[num]-b[num]);
		for(int i=1;i<=b[num];i++)
		{
			printf("%d ",w[num][0][i]);
			v[w[num][0][i]]=1;
		}
	}
}
int main()
{
	cin>>n;
	memset(v,-1,sizeof(v));
	int i,j,k;
	for(i=1;i<=n;i++)
	{
		memset(d,0,sizeof(d));
		while(scanf("%d",&j)&&j!=0)
		{
			d[j]=1;
		}
		for(j=1;j<=n;j++)
		{
			if(j!=i&&!d[j])
				g[i][j]=g[j][i]=1;
		}
	}
	for(i=1;i<=n;i++)
	{
		if(v[i]==-1)
		{
			cont++;
			if(divide(i))a[cont]=x,b[cont]=y;
			else
			{
				printf("No solution");
				return 0;
			}
		}
	}
	f[0][105]=1;
	for(i=0;i<cont;i++)
	{
		for(j=0;j<=209;j++)
		{
			if(f[i][j])
				f[i+1][j+a[i+1]-b[i+1]]=f[i+1][j-a[i+1]+b[i+1]]=1,h[i+1][j+a[i+1]-b[i+1]]=1,h[i+1][j-a[i+1]+b[i+1]]=0;
		}
	}
	memset(v,0,sizeof(v));
	for(i=105;i>=0;i--)
	{
		if(f[cont][i])
		{j=cont;
			cont=0;
			dfs(j,i);
			printf("\n");
			break;
		}
	}
	printf("%d ",n-cont);
	for(i=1;i<=n;i++)
	{
		if(!v[i])
			printf("%d ",i);
	}
	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