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 ...
随机推荐
- openVswitch(OVS)源码分析之工作流程(哈希桶结构体的解释)
这篇blog是专门解决前篇openVswitch(OVS)源码分析之工作流程(哈希桶结构体的疑惑)中提到的哈希桶结构flex_array结构体成员变量含义的问题. 引用下前篇blog中分析讨论得到的f ...
- hdu1280 前m大的数(数组下标排序)
前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- Broadleaf电商系统开发(一) - Broadleaf介绍
Broadleaf Commerce 是一个开源的 Java 电子商务平台,基于 Spring 框架开发.提供一个可靠.可扩展的架构,可进行深度的定制和高速开发. Broadleaf Commerce ...
- HDU 4572 Bottles Arrangement
具体的证明:点击打开链接 我的想法: 要想保证题目所说 构造最小行的和,仅仅能是这样的情况 ..... m-3 m-2 m-1 m | m m-1 m-2 m-3 ...
- BZOJ 4144 Dijkstra+Kruskal+倍增LCA
思路: 先把所有的加油站 push进按weight排序的优先队列里 对于每个不是加油站的点 找到到它的点的最短路以及它来源的加油站 如果x和y有边 且x和y加油站的来源不一样 则它可以连边 跑一边Kr ...
- 【VC++学习笔记五】SDI|MDI的全屏显示
一.Mainframe中添加一个记录是否全屏状态的变量BOOL m_bFullScreen. 二.工具栏添加一个按钮,进行全屏的操作,响应事件函数写在Mainframe中. 三.在响应函数中,添加如下 ...
- highcharts中的x轴如何显示时分秒时间格式
上一篇文章写道:三分钟上手Highcharts简易甘特图:https://www.jianshu.com/p/d669d451711b,在官方文档里面,x轴默认为年月日. 在项目需求中,x轴要表示24 ...
- MFC- OnIdle空闲处理
CWinApp::OnIdlevirtual BOOL OnIdle( LONG lCount );返回值: 如果要接收更多的空闲处理时间,则返回非零值:如果不需要更多的空闲时间则返回0.参数: lC ...
- 马上运行函数-$(function(){})篇
QQ:1187362408 欢迎技术交流和学习 马上运行函数-$(function(){})篇(jquery): TODO: 1.jquery:jQuery(function($){ }) 与 $(d ...
- CesiumJS - 3D Tiles BIM
CesiumJS - 3D Tiles BIM eryar@163.com 1. Introduction CesiumJS is an open-source JavaScript library ...