C - Cat Snuke and a Voyage


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands. For convenience, we will call them Island 1, Island 2, ..., Island N.

There are M kinds of regular boat services between these islands. Each service connects two islands. The i-th service connects Island ai and Island bi.

Cat Snuke is on Island 1 now, and wants to go to Island N. However, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services.

Help him.

Constraints

  • 3≤N≤200 000
  • 1≤M≤200 000
  • 1≤ai<biN
  • (ai,bi)≠(1,N)
  • If ij, (ai,bi)≠(aj,bj).

Input

Input is given from Standard Input in the following format:

N M
a1 b1
a2 b2
:
aM bM

Output

If it is possible to go to Island N by using two boat services, print POSSIBLE; otherwise, print IMPOSSIBLE.


Sample Input 1

Copy
3 2
1 2
2 3

Sample Output 1

Copy
POSSIBLE

Sample Input 2

Copy
4 3
1 2
2 3
3 4

Sample Output 2

Copy
IMPOSSIBLE

You have to use three boat services to get to Island 4.


Sample Input 3

Copy
100000 1
1 99999

Sample Output 3

Copy
IMPOSSIBLE

Sample Input 4

Copy
5 5
1 3
4 5
2 3
2 4
1 4

Sample Output 4

Copy
POSSIBLE

You can get to Island 5 by using two boat services: Island 1 -> Island 4 -> Island 5.

bfs搜索

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
vector<int>v[];
int n,m,x,y,vis[];
bool flag;
void bfs(int x,int y,int k)
{
if(x==y && k<=)
{
printf("POSSIBLE\n");
flag=true;
return;
}
if(k>) return;
vis[x]=;
for(int i=;i<v[x].size();i++)
{
if(!vis[v[x][i]])
{
vis[v[x][i]]=;
bfs(v[x][i],y,k+);
vis[v[x][i]]=;
}
}
vis[x]=;
}
int main()
{
scanf("%d%d",&n,&m);
flag=false;
memset(vis,,sizeof(vis));
while(m--)
{
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
bfs(,n,);
if(!flag) printf("IMPOSSIBLE\n");
return ;
}

再来个set简单方法,因为此题最多找两步,如果说步数更多的话就只能搜索了

    #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
set<int>s,v;
int n,m,x,y;
int main()
{
scanf("%d%d",&n,&m);
s.insert();
v.insert(n);
while(m--)
{
scanf("%d%d",&x,&y);
if(x==) s.insert(y);
if(y==) s.insert(x);
if(x==n) v.insert(y);
if(y==n) v.insert(x);
}
for(set<int>::iterator it=s.begin();it!=s.end();it++)
{
if(v.count(*it)==)
{
puts("POSSIBLE");
return ;
}
}
puts("IMPOSSIBLE");
return ;
}

Atcoder At Beginner Contest 068 C - Cat Snuke and a Voyage的更多相关文章

  1. Atcoder At Beginner Contest 068 D - Decrease (Contestant ver.)

    D - Decrease (Contestant ver.) Time limit : 2sec / Memory limit : 256MB Score : 600 points Problem S ...

  2. AtCoder Beginner Contest 068 ABCD题

    A - ABCxxx Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement This contes ...

  3. AtCoder Beginner Contest 068

    A - ABCxxx 题意: 给出n,输出“ABCn”就可以了,纯水题. B - Break Number 题意: 给出n,找出从1到n的闭区间内能够被2整除最多次的数. 思路: 直接模拟. 代码: ...

  4. Cat Snuke and a Voyage --AtCoder

    题目描述 In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands. For conve ...

  5. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  6. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  7. AtCoder Beginner Contest 154 题解

    人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...

  8. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  9. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

随机推荐

  1. 对spring默认的单列模式的理解

    我想大部分朋友对spring的单例模式都比較了解. 什么叫单例模式呢,顾名思义就是无论有多少个请求,都仅仅生成一个实例. 比方在spring中a,b请求都在调用同样的bean ,他们都是调用的同一个b ...

  2. iOS 实现QQ界面

    应师傅要求编写个QQ界面来不吝赐教下我的代码问题. 编写个QQ界面.有三个组,每一个组有人.并显示在线不在线. 先看一下效果图 这里省了事由于我的图片仅仅用了一张.假设要依据人的不同设置,仅仅要在ce ...

  3. Dom4j 查找节点或属性

    Dom4j  查找节点或属性 例如 1 查找下面xml中的student节点的age属性, xpathstr="/students/student/@age"; 2 查找下面xml ...

  4. vue的在子组件在使用iview库发现的问题

    正常调用没有问题 当写到子组件的时候 这样调用就出问题了 以下是写到子组件出的问题 解决方法  改成了两行写就不会报错了

  5. 分享一段wap框架样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. css3 scale的用法例子

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 用vuex构建单页

    原文地址:点我 前言:在最近学习 Vue.js 的时候,看到国外一篇讲述了如何使用 Vue.js 和 Vuex 来构建一个简单笔记的单页应用的文章.感觉收获挺多,自己在它的例子的基础上进行了一些优化和 ...

  8. BFC 和 haslayout

    在解释 BFC 是什么之前,需要先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位 Box 是 CSS 布局的对象和基本单位, 直观点来说,就是一个页面是由很 ...

  9. @Accessors

    @Accessors 作用:存取器,用于配置getter和setter方法的生成结果 三个属性:fluent.chain.prefix 1.fluent:流畅的,设置为true,getter和sett ...

  10. PHP设置30秒内对页面的访问次数

    <?php //Calculate 60 days in the future //seconds * minutes * hours * days + current time $intime ...