-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBFS.cpp
151 lines (125 loc) · 2.81 KB
/
BFS.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <climits>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
#define FOR(i, L, U) for(int i=(int)L; i<=(int)U; i++)
#define FORD(i, U, L) for(int i=(int)U; i>=(int)L; i--)
#define READ(x) freopen(x, "r", stdin)
#define WRITE(x) freopen(x, "w", stdout)
#define PQ priority_queue
#define PB push_back
#define SZ size()
#define ff first
#define ss second
#define EPS 1e-9
#define SQR(x) ((x)*(x))
#define INF INT_MAX
#define ALL_BITS ((1 << 31) - 1)
#define NEG_BITS(mask) (mask ^= ALL_BITS)
#define TEST_BIT(mask, i) (mask & (1 << i))
#define ON_BIT(mask, i) (mask |= (1 << i))
#define OFF_BIT(mask, i) (mask &= NEG_BITS(1 << i))
typedef long long LL;
typedef vector<char> VC;
typedef vector<vector<char> > VVC;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector< vector<bool> > VVB;
typedef pair<int, int> PII;
typedef map<int, int> MII;
typedef map<char, int> MCI;
typedef map<string, int> MSI;
typedef map<int, string> MIS;
int GCD(int a,int b){ while(b)b^=a^=b^=a%=b; return a; }
#define WHITE 1
#define GRAY 2
#define BLACK 3
#define MAX_NODE 100
#define NIL -1
VVI g;
queue<int> q;
VI path;
VI dist,pre,color;
int nodes, edges;
// s means startNode
void BFS(int s)
{
int u, v;
dist = VI(nodes+1,INF);
pre = VI(nodes+1, NIL);
color = VI(nodes+1,WHITE);
color[s] = GRAY;
dist[s] = 0;
pre[s] = NIL;
q.push(s);
while(!q.empty()) {
u = q.front(); q.pop();
FOR(i, 0, g[u].size()-1) {
v = g[u][i];
if(color[v] == WHITE) {
color[v] = GRAY;
dist[v] = dist[u] + 1;
pre[v] = u;
q.push(v);
}
}
color[u] = BLACK;
}
}
void CreatePath(int s, int v)
{
if(v == s)
path.push_back(s);
else if(pre[v] == NIL)
path.clear();
else {
CreatePath(s, pre[v]);
path.push_back(v);
}
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int u, v;
while(scanf("%d %d", &nodes, &edges)==2){
g = VVI(nodes+1);
FOR(i, 1, edges) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
BFS(1);
cout << "color\tdist\tparent\n";
FOR(i, 1, nodes) {
cout << color[i];
cout << "\t";
cout << dist[i];
cout << "\t";
cout << pre[i];
cout << "\n";
}
CreatePath(1, 5);
FOR(i, 0, path.size()-1)
cout << path[i] << " ";
cout << "\n";
}
return 0;
}