-
Notifications
You must be signed in to change notification settings - Fork 0
/
aac1.cpp
48 lines (48 loc) · 1.24 KB
/
aac1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<cstdio>
#include<set>
#include<queue>
#include<cstring>
using namespace std;
int main() {
int test;
scanf("%d", &test);
while(test--) {
int n, m;
scanf("%d %d", &n, &m);
set<int>* adjList = new set<int>[n+1];
bool* visited = new bool[n+1];
memset(visited, 0, sizeof(bool) * (n+1));
for(int i=0;i<m;++i) {
int x, y;
scanf("%d %d", &x, &y);
adjList[x].insert(y);
adjList[y].insert(x);
}
queue<int> q;
q.push(1);
q.push(-1);
int runningDist = 0;
int dist = -1;
while(!q.empty()) {
int val = q.front();
q.pop();
if(val == -1) {
runningDist++;
if(!q.empty())
q.push(-1);
}
else if(!visited[val]) {
visited[val] = true;
if(val == n) {
dist = runningDist;
break;
}
for(set<int>::iterator itr=adjList[val].begin();itr != adjList[val].end();++itr) {
q.push(*itr);
}
}
}
delete [] adjList;
printf("%d\n", dist);
}
}