Created
September 5, 2012 03:37
-
-
Save diego9627/3629961 to your computer and use it in GitHub Desktop.
Llegando en K pasos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<list> | |
#include<queue> | |
using namespace std; | |
list<int> aristas[5001]; | |
struct dat{ | |
int ver; | |
int mov; | |
}; | |
queue<dat> cam; | |
int main(){ | |
int N,a,b,M,i,j,P,t; | |
int pisado[5001]; | |
int movi[5001][2]; | |
cin >> N >> M; | |
dat aux,aux2; | |
list<int>::iterator it; | |
for(i=0;i<M;i++){ | |
cin >> a >> b; | |
aristas[a].push_front(b); | |
aristas[b].push_front(a); | |
} | |
for(i=0;i<=N+1;i++){ | |
movi[i][0]=-1; | |
movi[i][1]=-1; | |
} | |
cin >> P; | |
aux.ver=1; | |
aux.mov=0; | |
cam.push(aux); | |
movi[1][0]=0; | |
while(!cam.empty()){ | |
aux=cam.front(); | |
if(aux.mov<2*N){ | |
for(it=aristas[aux.ver].begin();it!=aristas[aux.ver].end();it++){ | |
if(movi[*it][(aux.mov+1)%2]==-1){ | |
aux2.ver=*it; | |
aux2.mov=aux.mov+1; | |
movi[aux2.ver][(aux2.mov)%2]=aux2.mov; | |
cam.push(aux2); | |
} | |
} | |
} | |
cam.pop(); | |
} | |
for(i=0;i<P;i++){ | |
t=0; | |
cin >> a >> b; | |
if(movi[a][b%2]>=0&&movi[a][b%2]<=b) t=1; | |
cout << t << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment