-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseq-bfs.cpp
More file actions
69 lines (53 loc) · 1.53 KB
/
seq-bfs.cpp
File metadata and controls
69 lines (53 loc) · 1.53 KB
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
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
int N, M;
vector<vector<int>> adj;
vector<int> dist;
void bfs() {
queue<int> q;
dist[0] = 0;
q.push(0);
while(!q.empty()) {
int u = q.front(), d = dist[u];
q.pop();
for(auto v: adj[u]) {
if(dist[v] == -1) {
dist[v] = d + 1;
q.push(v);
}
}
}
}
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int i, j;
FILE* f_in = fopen(argv[1], "r");
fscanf(f_in, "%d %d", &N, &M);
adj.resize(N);
dist.resize(N, -1);
for(i=0; i<M; i++) {
int x, y;
fscanf(f_in, "%d %d", &x, &y);
// x--; y--; ///////////////////////////////////////////// for sina weibo
// x-=101; y-=101; ///////////////////////////////////////////// for friendster
assert(x>=0 && y>=0);
if(x>=N || y>=N) continue;
adj[x].push_back(y);
adj[y].push_back(x);
}
fclose(f_in);
high_resolution_clock::time_point t1 = high_resolution_clock::now();
bfs();
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(t2 - t1).count();
FILE* f_out = fopen("seq-out.txt", "w");
for(auto d: dist) {
fprintf(f_out, "%d\n", d);
}
fprintf(f_out, "\n");
fprintf(f_out, "%d\n", *max_element(dist.begin(), dist.end()));
fclose(f_out);
cout << duration << "\n";
return 0;
}