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

深搜水过 132K 16MS

Posted by 322019281 at 2018-08-25 08:28:05 on Problem 1847
#include<cstdio>
using namespace std;

int e[101][101],b,ans=2000000000;
bool vis[101];

void dfs(int v,int step)
{
	if(step>=ans)return;
	if(v==b)
	{
		ans=step;
		return;
	}
	vis[v]=true;
	if(e[v][0]&&vis[e[v][1]]==false)dfs(e[v][1],step);
	for(int i=2;i<=e[v][0];i++)
		if(vis[e[v][i]]==false)dfs(e[v][i],step+1);
	vis[v]=false;
}

int main()
{
	int n,a,i,j;
	scanf("%d%d%d",&n,&a,&b);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&e[i][0]);
		for(j=1;j<=e[i][0];j++)scanf("%d",&e[i][j]);
	}
	dfs(a,0);
	if(ans==2000000000)printf("-1\n");
	else 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