Atcoder At Beginner Contest 068 C - Cat Snuke and a Voyage
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<bi≤N
- (ai,bi)≠(1,N)
- If i≠j, (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
3 2
1 2
2 3
Sample Output 1
POSSIBLE
Sample Input 2
4 3
1 2
2 3
3 4
Sample Output 2
IMPOSSIBLE
You have to use three boat services to get to Island 4.
Sample Input 3
100000 1
1 99999
Sample Output 3
IMPOSSIBLE
Sample Input 4
5 5
1 3
4 5
2 3
2 4
1 4
Sample Output 4
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的更多相关文章
- Atcoder At Beginner Contest 068 D - Decrease (Contestant ver.)
D - Decrease (Contestant ver.) Time limit : 2sec / Memory limit : 256MB Score : 600 points Problem S ...
- AtCoder Beginner Contest 068 ABCD题
A - ABCxxx Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement This contes ...
- AtCoder Beginner Contest 068
A - ABCxxx 题意: 给出n,输出“ABCn”就可以了,纯水题. B - Break Number 题意: 给出n,找出从1到n的闭区间内能够被2整除最多次的数. 思路: 直接模拟. 代码: ...
- Cat Snuke and a Voyage --AtCoder
题目描述 In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands. For conve ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
随机推荐
- silverlight wcf mvvm
近期工作比較忙.也没有时间发表新内容,今天有点时间,就顺便写点,说说近期开发的一套系统心得. 我刚去这个公司已经将前端确定要用Silverlight,我不知道为什么要选择这个,或许是为以后转C/S系统 ...
- 【图像配准】基于互信息的图像配准算法:MI、EMI、ECC算法
简单介绍: 基于互信息的图像配准算法以其较高的配准精度和广泛的适用性而成为图像配准领域研究的热点之中的一个.而基于互信息的医学图像配准方法被觉得是最好的配准方法之中的一个.基于此.本文将介绍简单的基于 ...
- css3 scale的用法例子
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Flex 集合 ArrayCollection 的使用
转:http://keren.iteye.com/blog/380847 转:http://callan.iteye.com/blog/335551 集合是ActionScript 中功能强大的基于索 ...
- 参考分享《Python深度学习》高清中文版pdf+高清英文版pdf+源代码
学习深度学习时,我想<Python深度学习>应该是大多数机器学习爱好者必读的书.书最大的优点是框架性,能提供一个"整体视角",在脑中建立一个完整的地图,知道哪些常用哪些 ...
- pwd---以绝对路径的方式显示用户当前工作目录
pwd命令以绝对路径的方式显示用户当前工作目录.命令将当前目录的全路径名称(从根目录)写入标准输出.全部目录使用/分隔.第一个/表示根目录,最后一个目录是当前目录.执行pwd命令可立刻得知您目前所在的 ...
- 【TC SRM 718 DIV 2 A】RelativeHeights
[Link]: [Description] 给你n个数字组成原数列; 然后,让你生成n个新的数列a 其中第i个数列ai为删掉原数列中第i个数字后剩余的数字组成的数列; 然后问你这n个数列组成的排序数组 ...
- Vim插件使用技巧(转)
在 IDEA Intellij小技巧和插件 一文中简单介绍了一下IdeaVim插件.在这里详细总结一下这个插件在日常编程中的一些常用小技巧.供有兴趣使用这个插件,但对Vim还不十分熟悉的朋友参考.当然 ...
- [Python] Use Python Classes
Object oriented classes work much like classes in other languages. Learn how to create them and use ...
- 欢天喜地迎国庆,国产开源编程语言 RPP 1.87 公布!
更新例如以下: 1.支持超级宏 2.修复bug 下载地址: https://github.com/roundsheep/rpp 超级宏能够随意定义语法,制约你的仅仅有想象力: void main() ...