-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathGraph Girth.cpp
50 lines (43 loc) · 1007 Bytes
/
Graph Girth.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
#include <bits/stdc++.h>
using namespace std;
const int maxN = 2501;
bool vis[maxN];
int N, M, best, p[maxN], dist[maxN];
vector<int> G[maxN];
void reset(){
fill(vis+1, vis+N+1, false);
fill(dist+1, dist+N+1, 0);
fill(p+1, p+N+1, 0);
}
void bfs(int start){
queue<int> Q;
Q.push(start);
vis[start] = true;
while(!Q.empty()){
int u = Q.front(); Q.pop();
for(int v : G[u]){
if(!vis[v]){
dist[v] = dist[u]+1;
vis[v] = true;
p[v] = u;
Q.push(v);
} else if(v != u[p]){
best = min(best, dist[u]+dist[v]+1);
}
}
}
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 0, a, b; i < M; i++){
scanf("%d %d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
best = N+1;
for(int start = 1; start <= N; start++){
reset();
bfs(start);
}
printf("%d\n", best == N+1 ? -1 : best);
}