QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#876#580033#9374. EscapestavageLinkZeldaFailed.2024-09-21 23:46:062024-09-21 23:46:06

Details

Extra Test:

Invalid Input

input:

1
6 6 5
1 2
1 3
3 6
1 5
1 4
1 6
3 2 4

output:


result:

FAIL Unexpected character #10, but ' ' expected (test case 1, stdin, line 9)

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#580033#9374. EscapeLinkZeldaAC ✓130ms28876kbC++141.9kb2024-09-21 19:49:532024-09-21 19:49:53

answer

#include<bits/stdc++.h>

using namespace std;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

const int N=400005,M=2000005,INF=1e9;
int n,m,d;
int head[N],to[M],nxt[M],cnt;
void add(int u,int v)
{
	to[++cnt]=v;
	nxt[cnt]=head[u];
	head[u]=cnt;
} 

int dis[N],diss[N];
int num[2][N],tot;
int last[N];

void solve()
{
	n=read(),m=read(),d=read();
	tot=1;
	for(int i=0;i<=1;i++)
		for(int j=1;j<=n;j++)
			num[i][j]=0;
	for(int i=1;i<=n;i++)
		num[0][i]=++tot,num[1][i]=++tot;
	fill(dis,dis+tot+1,INF);
	fill(diss,diss+tot+1,INF);
	fill(last,last+tot+1,0);
	fill(head,head+tot+1,0);
	cnt=0;
	for(int i=1;i<=m;i++)
	{
		int u=read(),v=read();
		add(num[0][u],num[1][v]);
		add(num[0][v],num[1][u]);
		add(num[1][u],num[0][v]);
		add(num[1][v],num[0][u]); 
	}
	int k=read();
	queue<int>Q;
	while(k--)
	{
		int x=read();
		dis[num[0][x]]=0;
		Q.push(num[0][x]);
	}
	while(!Q.empty())
	{
		int now=Q.front();Q.pop();
		if(dis[now]+1>d)
			continue;
		for(int i=head[now];i;i=nxt[i])
		{
			int v=to[i];
			if(dis[v]>dis[now]+1)
			{
				dis[v]=dis[now]+1;
				Q.push(v);
			}
		}
	}
	diss[num[0][1]]=0;Q.push(num[0][1]);
	int id=0;
	while(!Q.empty())
	{
		int now=Q.front();Q.pop();
		bool ok=0;
		for(int i=head[now];i;i=nxt[i])
		{
			int v=to[i];
			if(dis[v]<=diss[now]+1)
				continue;
			if(diss[v]>diss[now]+1)
			{
				diss[v]=diss[now]+1;
				last[v]=now;
				Q.push(v);
				if(v/2==n)
				{
					id=v;
					ok=1;
					break;
				}
			}
		}
		if(ok)
			break;
	}
	if(!id)
		return puts("-1"),void();
	vector<int>ret;
	printf("%d\n",diss[id]);
	while(id)
	{
		ret.push_back(id/2);
		id=last[id];
	}
	reverse(ret.begin(),ret.end());
	for(int x:ret)
		printf("%d ",x); 
	puts("");
}

signed main()
{
	int Tests=read();
	while(Tests--)
		solve();
}