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[200006]; int n,m,x,y,vis[200006]; bool flag; void bfs(int x,int y,int k) { if(x==y && k<=2) { printf("POSSIBLE "); flag=true; return; } if(k>2) return; vis[x]=1; for(int i=0;i<v[x].size();i++) { if(!vis[v[x][i]]) { vis[v[x][i]]=1; bfs(v[x][i],y,k+1); vis[v[x][i]]=0; } } vis[x]=0; } int main() { scanf("%d%d",&n,&m); flag=false; memset(vis,0,sizeof(vis)); while(m--) { scanf("%d%d",&x,&y); v[x].push_back(y); v[y].push_back(x); } bfs(1,n,0); if(!flag) printf("IMPOSSIBLE "); return 0; }
再来个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(1); v.insert(n); while(m--) { scanf("%d%d",&x,&y); if(x==1) s.insert(y); if(y==1) 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)==1) { puts("POSSIBLE"); return 0; } } puts("IMPOSSIBLE"); return 0; }