-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique.cpp
128 lines (101 loc) · 2.62 KB
/
unique.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <limits.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <bitset>
//#include <unordered_map>
using namespace std;
typedef pair<int, int> ii;
#define traverse(c, it) \
for (vector<int>::iterator it = c.begin(); it != c.end(); it++)
int N, M;
vector<vector<int> > AdjList;
vector<vector<bool> > blocked;
int A, B;
vector<bool> visited;
vector<int> parent;
bool bfs(int start, int dest, int type) {
// cout << blocked[0][1] << endl;
bool found = false;
queue<int> q;
q.push(start);
visited.clear();
visited.resize(N, false);
visited[start] = true;
parent.clear();
parent.resize(N, -1);
while(!q.empty()) {
int front = q.front();
q.pop();
if(front == dest) {
found = true;
break;
}
for(int i = 0; i < AdjList[front].size(); i++) {
int next = AdjList[front][i];
if(!visited[next]) {
int aux;
for(int j = 0; j < AdjList[front].size(); j++) {
if(AdjList[front][j] == next) {
aux = j;
break;
}
}
if(!blocked[front][aux]) {
//cout << next + 1 << endl;
q.push(next);
visited[next] = true;
parent[next] = front;
}
}
}
}
int index = dest;
while(parent[index] != -1) {
int src = parent[index];
int dst = index;
int aux;
for(int i = 0; i < AdjList[src].size(); i++) {
if(AdjList[src][i] == dst) {
aux = i;
break;
}
}
//cout << src + 1 << " " << AdjList[src][aux] + 1 << endl;
blocked[src][aux] = true;
index = parent[index];
}
return found;
}
int main() {
int F;
cin >> F;
while(F--) {
cin >> N >> M;
AdjList.clear();
AdjList.resize(N);
blocked.clear();
blocked.resize(N);
int start, end;
for(int i = 0; i < M; i++) {
cin >> start >> end;
start--; end--;
AdjList[start].push_back(end);
AdjList[end].push_back(start);
blocked[start].push_back(false);
blocked[end].push_back(false);
}
cin >> A >> B;
A--; B--;
bfs(A, B, 0);
cout << 1 - bfs(A, B, 1) << endl;
}
return 0;
}