题解 AT2660 【Cat Snuke and a Voyage】
一开始没有思路,一定是我太弱了直到翻开题解才恍然大悟就是太弱了
其实本来想跑一遍最短路然而看到数据果断放弃。
正解 :题意说只能坐两次船,那么肯定第一次从起点出发,第二次在终点结束
所以我们可以开两个数组A和B,
以下是我简洁丑陋的代码
时间复杂度
#include<cstdio>
int a[1000001],b[1000001];
int main()
{
int n,m,i;
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)
{
int st,ed;
scanf("%d%d",&st,&ed);
if(st==1)a[ed]=1;//记录第一次坐船的终点
if(ed==n)b[st]=1;//记录第二次坐船的起点
}
for(i=1;i<=m;i++)
if(a[i]&&b[i])//等价于a[i]==1&&b[1]==1
{
printf("POSSIBLE\n");return 0;//若有,直接退出
}
printf("IMPOSSIBLE\n");
return 0;
}