Problem:
Endless BFS
Mr. Endo wanted to write the code that performs breadth-first search (BFS), which is a search algorithm to explore all vertices on an undirected graph. An example of pseudo code of BFS is as follows:
1: $current \leftarrow \{start\_vertex\}$
2: $visited \leftarrow current$
3: while $visited \ne $ the set of all the vertices
4:   $found \leftarrow \{\}$
5:   for $v$ in $current$
6:     for each $u$ adjacent to $v$
7:       $found \leftarrow found \cup\{u\}$
8:   $current \leftarrow found \setminus visited$
9:   $visited \leftarrow visited \cup found$
However, Mr. Endo apparently forgot to manage visited vertices in his code. More precisely, he wrote the following
code:
1: $current \leftarrow \{start\_vertex\}$
2: while $current \ne $ the set of all the vertices
3:   $found \leftarrow \{\}$
4:   for $v$ in $current$
5:     for each $u$ adjacent to $v$
6:       $found \leftarrow found \cup \{u\}$
7:   $current \leftarrow found$
You may notice that for some graphs, Mr. Endo's program will not stop because it keeps running infinitely. Notice that it does not necessarily mean the program cannot explore all the vertices within finite steps. See example 2 below for more details.Your task here is to make a program that determines whether Mr. Endo's program will stop within finite steps for a given graph in order to point out the bug to him. Also, calculate the minimum number of loop iterations required for the program to stop if it is finite.
Input
The input consists of a single test case formatted as follows.
$N$ $M$
$U_1$ $V_1$
...
$U_M$ $V_M$
The first line consists of two integers $N$ ($2 \leq N \leq 100,000$) and $M$ ($1 \leq M \leq 100,000$), where $N$ is the number of vertices and $M$ is the number of edges in a given undirected graph, respectively. The $i$-th line of the following $M$ lines consists of two integers $U_i$ and $V_i$ ($1 \leq U_i, V_i \leq N$), which means the vertices $U_i$ and $V_i$ are adjacent in the given graph. The vertex 1 is the start vertex, i.e. $start\_vertex$ in the pseudo codes. You can assume that the given graph also meets the following conditions.
The graph has no self-loop, i.e., $U_i \ne V_i$ for all $1 \leq i \leq M$.
The graph has no multi-edge, i.e., $\{Ui,Vi\} \ne \{U_j,V_j\}$ for all $1 \leq i < j \leq M$.
The graph is connected, i.e., there is at least one path from $U$ to $V$ (and vice versa) for all vertices $1 \leq U, V \leq N$
Output
If Mr. Endo's wrong BFS code cannot stop within finite steps for the given input graph, print -1 in a line. Otherwise, print the minimum number of loop iterations required to stop.
Sample Input 1
3 3
1 2
1 3
2 3
Output for Sample Input 1
2
Sample Input 2
4 3
1 2
2 3
3 4
Output for Sample Input 2
-1
Transition of $current$ is $\{1\} \rightarrow \{2\} \rightarrow \{1,3\} \rightarrow \{2,4\} \rightarrow \{1,3\} \rightarrow \{2,4\} \rightarrow ... $. Although Mr. Endo's program will achieve to visit all the vertices (in 3 steps), will never become the same set as all t
...(truncated)

Submitted solutions:

code_snippet: #include <algorithm>
#include <iostream>
#include <vector>
#include <utility>
#include <queue>
using namespace std;
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int N, M;
    cin >> N >> M;
    vector<vector<int>> g(N);
    vector<pair<int, int>> E(M);
    for (int i = 0 ; i < M ; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        E[i] = {u, v};
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<int> dist = [&]() {
        vector<int> res(N, -1), que{0};
        res[0] = 0;
        for (int t = 0 ; t < ssize(que) ; t++) {
            const int v = que[t];
            for (int x : g[v])
                if (res[x] == -1) {
                    res[x] = res[v] + 1;
                    que.push_back(x);
                }
        }
        return res;
    }();
    using qt = pair<int, int>;
    const int INF = (int)1e9;
    vector<int> paint(N, INF);
    priority_queue<qt, vector<qt>, greater<qt>> que;
    for (auto [u, v] : E)
        if (dist[u] == dist[v]) {
            // cout << dist[u] << ' ' << u + 1 << ' ' << v + 1 << endl;
            paint[u] = min(paint[u], dist[u]);
            paint[v] = min(paint[v], dist[v]);
            que.push({dist[u], u});
            que.push({dist[v], v});
        }
    while (que.size()) {
        const auto [d, v] = que.top();
        que.pop();
        if (paint[v] < d)
            continue;
        for (int x : g[v])
            if (paint[x] > d + 1) {
                paint[x] = d + 1;
                que.push({paint[x], x});
            }
    }
    int res = *max_element(paint.begin(), paint.end());
    cout << (res == INF ? -1 : res) << '\n'; 
}
accuracy: 1
time_ms: 20
memory_kb: 11172
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define loop(i, a, b) for (ll i = a; i <= b; ++i)
constexpr ll INF = 9009009009009009009LL;

int main() {
    ll n, m;
    cin >> n >> m;
    vector<vector<ll>> G(n);
    rep(i, m) {
        ll u, v;
        cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    // visited[i][0] = i番目の奇数
    // visited[i][1] = i番目の偶数
    vector<vector<ll>> visited(n,vector<ll>(2,-1));
    //vector<ll> visited(n, -1);
    queue<pair<ll, ll>> q;
    visited[0][0] = 0;
    q.push({0, 0});
    ll ans = 0;
    while (!q.empty()) {
        ll now = q.front().first;
        ll bit = q.front().second;
        q.pop();
        for (ll next : G[now]) {
            if(visited[next][bit ^ 1] == -1){
                q.push({next,bit ^ 1});
                visited[next][bit ^ 1] = visited[now][bit] + 1;
            }
        }
    }


    bool flag0 = false;
    bool flag1 = false;
    ll mx0 = -INF, mx1 = -INF;
    rep(i, n){
        if(visited[i][0] == -1) flag0 = true;
        if(visited[i][1] == -1) flag1 = true;
        mx0 = max(mx0, visited[i][0]);
        mx1 = max(mx1, visited[i][1]);
    }
    if (flag0 && flag1) {
        cout << -1 << endl;
        return 0;
    }

    if (flag0) mx0 = INF;
    if (flag1) mx1 = INF;
    cout << min(mx0, mx1) << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 16568
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

int main() {
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    for (int i = 0; i < M; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--, G[u].push_back(v), G[v].push_back(u);
    }

    vector<vector<int>> dst(N, vector<int>(2, INF));
    dst[0][0] = -1;
    queue<pair<int, int>> que;
    que.push({0, 0});
    while (!que.empty()) {
        auto [now, fl] = que.front();
        que.pop();

        int nfl = 1 - fl;
        for (int nxt : G[now]) {
            if (dst[nxt][nfl] > dst[now][fl] + 1) {
                dst[nxt][nfl] = dst[now][fl] + 1;
                que.push({nxt, nfl});
            }
        }
    }

    int ans = 0;
    for (int i = 0; i < N; i++) ans = max(ans, max(dst[i][0], dst[i][1]));
    if (ans == INF) ans = -1;
    cout << ans << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 15092
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main(){
	int N, M;
	cin >> N >> M;
	vector<vector<int>> G(N);
	for(int i = 0; i < M; i++){
		int u, v; cin >> u >> v;
		u--; v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}


	queue<pair<int, int>> que;
	que.push({0, 0});
	vector<vector<int>> dst(N, vector<int>(2, 1e9));
	dst[0][0] = 0;
	while(!que.empty()){
		auto [now, pr] = que.front(); que.pop();
		int npr = 1-pr;
		for(int nxt : G[now]){
			if(dst[nxt][npr] > dst[now][pr]+1){
				dst[nxt][npr] = dst[now][pr]+1;
				que.push({nxt, npr});
			}
		}
	}

	int ans = 0;
	for(int i = 0; i < N; i++) ans = max(ans, max(dst[i][0], dst[i][1]));
	if(ans == (int)1e9) ans = 0;
	cout << --ans << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 15008
final_rank: 14

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    rep(i,0,M) {
        int A, B;
        cin >> A >> B;
        A--, B--;
        G[A].push_back(B);
        G[B].push_back(A);
    }
    vector Dist(N,vector<int>(2,inf));
    Dist[0][0] = 0;
    queue<pair<int,int>> Q;
    Q.push({0,0});
    while(!Q.empty()) {
        auto [X,P] = Q.front();
        Q.pop();
        for (int NX : G[X]) {
            int NP = 1-P;
            if (chmin(Dist[NX][NP],Dist[X][P]+1)) {
                Q.push({NX,NP});
            }
        }
    }
    int ANS0 = 0, ANS1 = 0;
    rep(i,0,N) chmax(ANS0,Dist[i][0]), chmax(ANS1,Dist[i][1]);
    int ANS = min(ANS0,ANS1);
    cout << (ANS == inf ? -1 : ANS) << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 15100
final_rank: 16

code_snippet: // competitive-verifier: PROBLEM
#ifdef ATCODER
#pragma GCC target("sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2")
#endif
#pragma GCC optimize("Ofast,fast-math,unroll-all-loops")
#include <bits/stdc++.h>
#ifndef ATCODER
#pragma GCC target("sse4.2,avx2,bmi2")
#endif
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = 3.14159265358979323846;
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }
    constexpr void operator()() const {}
} sonic;
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? "" : " ") << *it;
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
void Yes(bool is_correct = true) { std::cout << (is_correct ? "Yes\n" : "No\n"); }
void No(bool is_not_correct = true) { Yes(!is_not_correct); }
void YES(bool is_correct = true) { std::cout << (is_correct ? "YES\n" : "NO\n"); }
void NO(bool is_not_correct = true) { YES(!is_not_correct); }
void Takahashi(bool is_correct = true) { std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n'; }
void Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }
/**
 * @brief 重み付きグラフ
 *
 * @tparam T 辺の重みの型
 */
template <class T>
struct Graph {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to(), _weight() {}
        constexpr _edge(int from, int to, T weight) : _from(from), _to(to), _weight(weight) {}
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }
        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr T weight() const { return _weight; }
      private:
        int _from, _to;
        T _weight;
    };
  public:
    using edge_type = typename Graph<T>::_edge;
    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}
    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }
    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to, T weight = T(1)) { edges[from].emplace_back(from, to, weight); }
    void add_edges(int from, int to, T weight = T(1)) {
        edges[from].emplace_back(from, to, weight);
        edges[to].emplace_back(to, from, weight);
    }
    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edge(from - base, to - base, weight);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edges(from - base, to - base, weight);
        }
    }
  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};
template <>
struct Graph<void> {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to() {}
        constexpr _edge(int from, int to) : _from(from), _to(to) {}
        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr int weight() const { return 1; }
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }
      private:
        int _from, _to;
    };
  public:
    using edge_type = typename Graph<void>::_edge;
    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}
    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }
    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to) { edges[from].emplace_back(from, to); }
    void add_edges(int from, int to) {
        edges[from].emplace_back(from, to);
        edges[to].emplace_back(to, from);
    }
    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edge(from - base, to - base);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edges(from - base, to - base);
        }
    }
  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};
/**
 * @brief 素数ライブラリ
 *
 * @tparam N
 */
template <int N = 1 << 22>
struct prime_number {
    prime_number() : is_not_prime(), data() { init(); }
    /**
     * @brief 素数判定
     *
     * @param n
     * @return bool
     */
    bool is_prime(std::int64_t n) const {
        assert(n >= 0);
        if (n < N) return !is_not_prime[n];
        for (auto i : data) {
            if ((std::int64_t)i * i > n) break;
            if (n % i == 0) return false;
        }
        return true;
    }
    std::vector<int> prime_numbers(int x) const {
        std::vector<int> res;
        for (auto i : data) {
            if (i > x) break;
            res.emplace_back(i);
        }
        return res;
    }
    /**
     * @brief 素因数分解
     *
     * @tparam T
     * @param x
     * @return std::vector<std::pair<T, int>>
     */
    template <class T>
    std::vector<std::pair<T, int>> prime_factorization(T x) const {
        if (x == 1) return std::vector<std::pair<T, int>>();
        std::vector<std::pair<T, int>> res;
        for (auto p : data) {
            int cnt = 0;
            for (; x % p == 0; x /= p) ++cnt;
            if (cnt) res.emplace_back(p, cnt);
            if ((std::int64_t)p * p > x) break;
        }
        if (x != 1) res.emplace_back(x, 1);
        return res;
    }
    /**
     * @brief 約数列挙
     *
     * @tparam T
     * @param x
     * @return std::vector<T>
     */
    template <class T>
    std::vector<T> divisors(T x) const {
        if (x == 1) return std::vector<T>(1, 1);
        auto v = prime_factorization(x);
        std::vector<T> res;
        res.emplace_back(1);
        for (auto p : v) {
            int n = res.size();
            res.resize(n * (p.second + 1));
            for (int i = 0; i < n * p.second; ++i) res[n + i] = res[i] * p.first;
            for (int i = 1; i <= p.second; ++i) {
                std::inplace_merge(res.begin(), res.begin() + n * i, res.begin() + n * (i + 1));
            }
        }
        return res;
    }
    /**
     * @brief 因数分解列挙
     *
     * @tparam T
     * @param x
     * @return std::vector<std::vector<T>>
     */
    template <class T>
    std::vector<std::vector<T>> factorization(T x) const {
        std::vector<std::vector<T>> res;
        auto f = [&](auto self, std::vector<T> v, T a) -> void {
            if (a == 1) res.emplace_back(v);
            for (auto i : this->divisors(a)) {
                if (i == 1 || (!v.empty() && v.back() > i)) continue;
                v.emplace_back(i);
                self(self, v, a / i);
                v.pop_back();
            }
        };
        f(f, std::vector<T>(), x);
        return res;
    }
  private:
    std::bitset<N> is_not_prime;
    std::vector<int> data;
    void init() {
        is_not_prime[0] = is_not_prime[1] = true;
        for (int i = 2; i < N; ++i) {
            if (!is_not_prime[i]) {
                data.emplace_back(i);
                if ((std::int64_t)i * i >= N) continue;
                if (i == 2) {
                    for (int j = i * i; j < N; j += i) is_not_prime[j] = true;
                } else {
                    for (int j = i * i; j < N; j += i << 1) is_not_prime[j] = true;
                }
            }
        }
    }
};
prime_number pn;
int main(void)
{
    int n, m;
    cin >> n >> m;
    Graph<void> g(n);
    g.input_edges(m);
    vector dp(n, vector(2, Inf));
    queue<pair<int, int>> que;
    dp[0][0] = 0;
    que.emplace(0, 0);
    while (!que.empty())
    {
        auto [x, y] = que.front();
        que.pop();
        for (auto e : g[x])
        {
            if (chmin(dp[e.to()][!y], dp[x][y] + 1))
            {
                que.emplace(e.to(), !y);
            }
        }
    }
    int ans = Inf;
    int m0 = 0, m1 = 0;
    rep(i, n)
    {
        chmax(m0, dp[i][0]);
    }
    rep(i, n)
    {
        chmax(m1, dp[i][1]);
    }
    chmin(ans, m0);
    chmin(ans, m1);
    if (ans == Inf)
        ans = -1;
    co(ans);
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 17144
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

bool isBipartite(vector<vector<int>>& G){
    int N = G.size();
    vector<int> col(N, -1);
    auto dfs = [&](auto f, int u, int c) -> bool {
        col[u] = c;
        int num0 = 0, num1 = 0;
        for(auto v : G[u]){
            if(col[v] == 0) num0++;
            if(col[v] == 1) num1++;
        }
        if(c == 0 && num0 >= 1) return false;
        if(c == 1 && num1 >= 1) return false;
        
        bool flag = 1;
        for(auto v : G[u]){
            if(col[v] == -1) flag &= f(f, v, !c);
        }
        return flag;
    };

    return dfs(dfs, 0, 0);
}

int main(){
    int N, M; cin >> N >> M;
    vector<vector<int>> G(N);
    for(int i = 0; i < M; i++){
        int u, v; cin >> u >> v; u--, v--;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    if(isBipartite(G)){
        cout << -1 << endl;
        return 0;
    }

    long long ans = 0;
    vector<bool> A(N), B(N);
    stack<int> vA, vB;
    int cntA = 0, cntB = 0;

    A[0] = 1;
    vA.push(0);
    cntA = 1;

    while(1){
        if(cntA == N) break;
        while(!vA.empty()){
            int u = vA.top();
            vA.pop();
            for(auto v : G[u]){
                if(!B[v]){
                    cntB++;
                    vB.push(v);
                }
                B[v] = 1;
            }
        }

        ans++;
        swap(A, B);
        swap(vA, vB);
        swap(cntA, cntB);
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 15228
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
using ll = long long;
template<class T> bool chmin(T &a,const T b){if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a,const T b){if(a<b){a=b;return 1;}return 0;}
const int INF = (1<<30)-1;
const ll  ILL = 1e15;
#define all(p) p.begin(),p.end()
const int mod=998244353;
using ld = long double;

int main(){
	int N,M;
	cin>>N>>M;
	vector<vector<int>> G(N*2);
	vector<int> seen(N*2,INF);
	seen[0]=0;
	rep(i,0,M){
		int a,b;
		cin>>a>>b;
		a--,b--;
		G[a].push_back(b+N);
		G[b].push_back(a+N);
		G[a+N].push_back(b);
		G[b+N].push_back(a);
	}
	vector<int> order={0};
	rep(i,0,N*2){
		if((int)(order.size())==i){
			cout<<"-1\n";
			return 0;
		}
		for(auto x:G[order[i]]){
			if(seen[x]==INF){
				order.push_back(x);
				seen[x]=seen[order[i]]+1;
			}
		}
	}
	int ans=INF;
	rep(j,0,2){
		int tmp=0;
		rep(i,0,N){
			chmax(tmp,seen[i+j*N]);
		}
		chmin(ans,tmp);
	}
	cout<<ans<<"\n";
}
accuracy: 1
time_ms: 50
memory_kb: 16504
final_rank: 17

code_snippet: /**
 *    author:  otera
**/
#include<bits/stdc++.h>
using namespace std;

#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using int128_t = __int128_t;
#define repa(i, n) for(int i = 0; i < n; ++ i)
#define repb(i, a, b) for(int i = a; i < b; ++ i)
#define repc(i, a, b, c) for(int i = a; i < b; i += c)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define rep(...) overload4(__VA_ARGS__, repc, repb, repa)(__VA_ARGS__)
#define rep1a(i, n) for(int i = 0; i <= n; ++ i)
#define rep1b(i, a, b) for(int i = a; i <= b; ++ i)
#define rep1c(i, a, b, c) for(int i = a; i <= b; i += c)
#define rep1(...) overload4(__VA_ARGS__, rep1c, rep1b, rep1a)(__VA_ARGS__)
#define rev_repa(i, n) for(int i=n-1;i>=0;i--)
#define rev_repb(i, a, b) assert(a > b);for(int i=a;i>b;i--)
#define rev_rep(...) overload3(__VA_ARGS__, rev_repb, rev_repa)(__VA_ARGS__)
#define rev_rep1a(i, n) for(int i=n;i>=1;i--)
#define rev_rep1b(i, a, b) assert(a >= b);for(int i=a;i>=b;i--)
#define rev_rep1(...) overload3(__VA_ARGS__, rev_rep1b, rev_rep1a)(__VA_ARGS__)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(), c.rend()
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define Sort(a) sort(all(a))
#define Rev(a) reverse(all(a))
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define si(c) (int)(c).size()
inline ll popcnt(ull a){ return __builtin_popcountll(a); }
#define kth_bit(x, k) ((x>>k)&1)
#define unless(A) if(!(A))
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll intpow(ll a, ll b, ll m) {ll ans = 1; while(b){ if(b & 1) (ans *= a) %= m; (a *= a) %= m; b /= 2; } return ans; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vc<T>>;
template <class T> using vvvc = vector<vvc<T>>;
template <class T> using vvvvc = vector<vvvc<T>>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template<class T> void scan(T& a){ cin >> a; }
template<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){ scan(head); in(tail...); }
void print(){ cout << ' '; }
template<class T> void print(const T& a){ cout << a; }
template<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout << ' '; print(*i); } }
int out(){ cout << '\n'; return 0; }
template<class T> int out(const T& t){ print(t); cout << '\n'; return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); cout << ' '; out(tail...); return 0; }
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#define dump(...) { print(#__VA_ARGS__); print(":"); out(__VA_ARGS__); }
#else
#define debug(...)
#define dump(...)
#endif

struct io_setup {
    io_setup(int precision = 20) {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout << fixed << setprecision(precision);
    }
} io_setup_ {};

const int inf = 1e9 + 7;

void solve() {
    INT(n, m);
    vvc<int> g(n);
    rep(i, m) {
        INT(u, v); -- u, -- v;
        g[u].eb(v);
        g[v].eb(u);
    }
    vc<int> c(n, -1);
    bool ok = 1;
    auto dfs = [&](auto&&dfs, int v, int col) -> void {
        if(!ok) return;
        c[v] = col;
        for(int nv: g[v]) {
            if(c[nv] == -1) dfs(dfs, nv, 1 - col);
            else if(c[nv] == col) {
                ok = 0;
            }
        }
    };
    dfs(dfs, 0, 0);
    if(ok) out(-1);
    else {
        vvc<int> di(2, vc<int>(n, inf));
        queue<P> que;
        que.push(P{0, 0});
        di[0][0] = 0;
        while(que.size()) {
            auto [parity, v] = que.front(); que.pop();
            int dist = di[parity][v];
            assert(dist % 2 == parity);
            for(int nv: g[v]) {
                if(chmin(di[parity ^ 1][nv], dist + 1)) {
                    que.push(P{parity ^ 1, nv});
                }
            }
        }
        int ans = 0;
        rep(i, n) {
            if(di[0][i] % 2 == 0) di[0][i] --;
            if(di[1][i] % 2 == 1) di[1][i] --;
            chmax(ans, max(di[0][i], di[1][i]));
            debug(i, di[0][i], di[1][i]);
        }
        out(ans);
    }
}

signed main() {
    int testcase = 1;
    // in(testcase);
    while(testcase--) solve();
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 17864
final_rank: 8

code_snippet: #include "bits/stdc++.h"
using namespace std;

typedef string::const_iterator State;
#define eps 1e-8L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))
#define ALL(x) (x).begin(), (x).end()

#define int long long
vector<int> vertexs[300000];
int dist[300000];
void solve()
{
  int n, m;
  cin >> n >> m;
  REP(i, m)
  {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    vertexs[a].push_back(b + n);
    vertexs[b + n].push_back(a);

    vertexs[a + n].push_back(b);
    vertexs[b].push_back(a + n);
  }
  REP(i, 2 * n)
  {
    dist[i] = 1e18;
  }
  dist[0] = 0;
  queue<int> nexts;
  nexts.push(0);
  while (!nexts.empty())
  {
    int now = nexts.front();
    nexts.pop();
    for (auto x : vertexs[now])
    {
      if (dist[x] == 1e18)
      {
        dist[x] = dist[now] + 1;
        nexts.push(x);
      }
    }
  }
  int a = min(*max_element(dist, dist + n), *max_element(dist + n, dist + 2 * n));
  if (a == 1e18)
    a = -1;
  cout << a << endl;
}

#undef int

// generated by oj-template v4.7.2
// (https://github.com/online-judge-tools/template-generator)
int main()
{
  // Fasterize input/output script
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  // scanf/printf user should delete this fasterize input/output script

  int t = 1;
  // cin >> t; // comment out if solving multi testcase
  for (int testCase = 1; testCase <= t; ++testCase)
  {
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 20612
final_rank: 13

code_snippet: #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)

int main() {
	int n;
	cin >> n;
	int m;
	cin >> m;
	vector<vector<int>> indices(2, vector<int>(n));
	int gs = 0;
	rep(i, 2) rep(j, n) indices[i][j] = gs++;
	vector<vector<int>> graph(gs);
	rep(_, m) {
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		rep(lp, 2) {
			rep(j, 2) graph[indices[1 - j][a]].push_back(indices[j][b]);
			swap(a, b);
		}
	}

	queue<int> q;
	const int inf = gs + 4;
	vector<int> dist(gs, inf);

	q.push(indices[0][0]);
	dist[indices[0][0]] = 0;

	while(q.size()) {
		int now = q.front();
		q.pop();
		for(auto nxt : graph[now]) {
			if(dist[nxt] > dist[now] + 1) {
				dist[nxt] = dist[now] + 1;
				q.push(nxt);
			}
		}
	}

	// for(auto t : dist) {
	// 	cerr << t << ' ';
	// }cerr << '\n';

	// int res = *max_element(dist.begin(), dist.end());
	int res = min(*max_element(dist.begin(), dist.begin() + n), *max_element(dist.begin() + n, dist.end()));

	cout << (res == inf ? -1 : res) << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 16640
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()
const int INF = 1 << 30;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    rep(i, M) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector<int> diso(N, INF), dise(N, INF);
    dise[0] = 0;
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        if (dise[v] != INF) {
            for (auto &nx : G[v]) {
                if (diso[nx] == INF) {
                    diso[nx] = dise[v] + 1;
                    que.push(nx);
                }
            }
        }
        if (diso[v] != INF) {
            for (auto &nx : G[v]) {
                if (dise[nx] == INF) {
                    dise[nx] = diso[v] + 1;
                    que.push(nx);
                }
            }
        }
    }
    int em = 0, om = 0;
    rep(i, N) {
        em = max(em, dise[i]);
        om = max(om, diso[i]);
    }
    int ans = min(em, om);
    cout << (ans == INF ? -1 : ans) << '\n';
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 10060
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()
const int INF = 1 << 30;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    rep(i, M) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector<int> diso(N, INF), dise(N, INF);
    dise[0] = 0;
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        if (dise[v] != INF) {
            for (auto &nx : G[v]) {
                if (diso[nx] == INF) {
                    diso[nx] = dise[v] + 1;
                    que.push(nx);
                }
            }
        }
        if (diso[v] != INF) {
            for (auto &nx : G[v]) {
                if (dise[nx] == INF) {
                    dise[nx] = diso[v] + 1;
                    que.push(nx);
                }
            }
        }
    }
    rep(i, N) {
        if (dise[i] == INF || diso[i] == INF) {
            cout << -1 << '\n';
            return 0;
        }
    }
    int em = 0, om = 0;
    rep(i, N) {
        em = max(em, dise[i]);
        om = max(om, diso[i]);
    }
    cout << min(em, om) << '\n';
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 10132
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()
const int INF = 1 << 30;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    rep(i, M) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector<int> diso(N, INF), dise(N, INF);
    dise[0] = 0;
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        if (dise[v] != INF) {
            for (auto &nx : G[v]) {
                if (diso[nx] == INF) {
                    diso[nx] = dise[v] + 1;
                    que.push(nx);
                }
            }
        }
        if (diso[v] != INF) {
            for (auto &nx : G[v]) {
                if (dise[nx] == INF) {
                    dise[nx] = diso[v] + 1;
                    que.push(nx);
                }
            }
        }
    }
    rep(i, N) {
        if (dise[i] == INF || diso[i] == INF) {
            cout << -1 << '\n';
            return 0;
        }
    }
    int ok = INF, ng = 0;
    while (ok - ng > 1) {
        int md = (ok + ng) / 2;
        int ok2 = 1;
        if (md & 1) {
            rep(i, N) if (diso[i] > md) ok2 = 0;
        } else {
            rep(i, N) if (dise[i] > md) ok2 = 0;
        }
        if (ok2)
            ok = md;
        else
            ng = md;
    }
    cout << ok << '\n';
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 10096
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define inc(i, a, b) for (int i = (a); i <= (b); ++i)
#define dec(i, a, b) for (int i = (a); i >= (b); --i)

int main() {
    ll n, m;
    cin >> n >> m;
    vector<ll> g[n];
    rep(i, m) {
        ll a, b;
        cin >> a >> b;
        g[a - 1].push_back(b - 1);
        g[b - 1].push_back(a - 1);
    }
    vector<ll> d(n * 2, 1e18);
    d[0] = 0;
    queue<ll> q;
    q.push(0);
    while (q.size()) {
        ll v = q.front();
        q.pop();
        for (auto x : g[v % n]) {
            ll p = v >= n ? x : x + n;
            if (d[p] > d[v] + 1) {
                d[p] = d[v] + 1;
                q.push(p);
            }
        }
    }
    ll res = min(*max_element(d.begin(), d.begin() + n), *max_element(d.begin() + n, d.end()));
    if (res == (ll) 1e18) res = -1;
    cout << res << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 11844
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < (int) v.size(); ++i) {
        os << v[i];
        if (i < (int) v.size() - 1) os << " ";
    }
    return os;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    rep(i,0,M) {
        int U, V;
        cin >> U >> V;
        --U, --V;
        G[U].push_back(V);
        G[V].push_back(U);
    }
    vector<int> col(N, -1);
    queue<int> que;
    que.push(0);
    col[0] = 0;
    bool ok = true;
    while (!que.empty() && ok) {
        int v = que.front(); que.pop();
        for (int u : G[v]) {
            if (col[u] == -1) {
                col[u] = col[v] ^ 1;
                que.push(u);
            } else if (col[u] == col[v]) {
                ok = false;
                break;
            }
        }
    }
    if (ok) {
        cout << -1 << endl;
        return 0;
    }
    queue<pair<int, int>> que2;
    que2.push({0, 0});
    vector<vector<int>> dist(N, vector<int>(2, -1));
    dist[0][0] = 0;
    while (!que2.empty()) {
        auto [v, b] = que2.front(); que2.pop();
        for (int u : G[v]) {
            if (dist[u][b^1] == -1) {
                dist[u][b^1] = dist[v][b] + 1;
                que2.push({u, b^1});
            }
        }
    }
    int ans = 0;
    rep(i,0,N) ans = max({ans, dist[i][0], dist[i][1]});
    // check if ans-1 is a solution
    ok = true;
    rep(i,0,N) {
        if (dist[i][(ans-1)%2] > ans-1) {
            ok = false;
            break;
        }
    }
    if (ok) --ans;
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 14432
final_rank: 6

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

void Calculate_Depth(vector<vector<int>>& edge, vector<int>& depth, int stnode) {
	depth[stnode] = 0;
	queue<int>Q;
	Q.push(stnode);
	while (!Q.empty()) {
		int cn = Q.front();
		Q.pop();
		for (auto i : edge[cn]) {
			if (depth[i] > depth[cn] + 1) {
				depth[i] = depth[cn] + 1;
				Q.push(i);
			}
		}
	}
	return;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N >> M;
	vector<vector<int>>edge(2 * N);
	vector<vector<int>>e(N);
	for (int i = 0; i < M; i++) {
		cin >> L >> R;
		L--, R--;
		e[L].push_back(R);
		e[R].push_back(L);
		edge[L].push_back(N + R);
		edge[R].push_back(N + L);
		edge[N + L].push_back(R);
		edge[N + R].push_back(L);
	}
	vector<int>dis(N, MOD);
	Calculate_Depth(e, dis, 0);
	bool bi = true;
	for (int i = 0; i < N; i++) {
		for (auto j : e[i]) {
			if (dis[i] % 2 == dis[j] % 2) {
				bi = false;
			}
		}
	}
	if (bi) {
		cout << -1 << endl;
		return 0;
	}
	int node = 0;
	dis.resize(2 * N);
	for (auto& i : dis)i = MOD;
	Calculate_Depth(edge, dis, 0);
	int a = 0, b = 0;
	for (int i = 0; i < N; i++) {
		a = max(a, dis[i]);
		b = max(b, dis[i + N]);
	}
	cout << min(a, b) << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 21316
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define POPCOUNT(x) __builtin_popcount(x)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }

const ll INF = 1LL << 60;

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
} fastiofastio;

const ll MOD = 1e9 + 7;

// BEGIN CUT
ll modpow(ll x, ll y, ll m) {
    ll a = 1, p = x;
    while (y > 0) {
        if (y % 2 == 0) {
            p = (p * p) % m;
            y /= 2;
        } else {
            a = (a * p) % m;
            y--;
        }
    }
    return a;
}
// END CUT

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

vector<int> G[100000];
int dist[100000][2];

int main() {
    int N, M;
    cin >> N >> M;
    for (int i = 0; i < M; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    fill((int *)dist, (int *)(dist + N), 1 << 30);
    dist[0][0] = 0;
    queue<pair<int, int>> que;
    que.emplace(0, 0);
    while (!que.empty()) {
        auto [v, b] = que.front();
        que.pop();
        for (int to : G[v]) {
            if (dist[to][b ^ 1] == 1 << 30) {
                dist[to][b ^ 1] = dist[v][b] + 1;
                que.emplace(to, b ^ 1);
            }
        }
    }
    int even = 0, odd = 0;
    for (int i = 0; i < N; i++) {
        even = max(even, dist[i][0]);
        odd = max(odd, dist[i][1]);
    }
    int ans = min(even, odd);
    if (ans == 1 << 30)
        cout << -1 << endl;
    else
        cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 10536
final_rank: 4

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m; cin >> n >> m;
    vector<vector<int>> g(n);
    for(int i=0;i<m;i++){
        int x,y; cin >> x >> y;
        x--; y--;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    const int inf = 1e9;
    vector<vector<int>> dp(n,vector<int>(2,inf));
    dp[0][0] = 0;
    // 二部グラフだったら-1
    queue<pair<int,int>> q;
    q.push({0,0});
    while(q.size()){
        auto p = q.front(); q.pop();
        for(int t:g[p.first]){
            if(dp[t][p.second^1] == inf){
                dp[t][p.second^1] = dp[p.first][p.second] + 1;
                q.push({t,p.second^1});
            }
        }
    }
    int res = inf;
    for(int i=0;i<2;i++){
        int mx = 0;
        for(int j=0;j<n;j++){
            mx = max(mx, dp[j][i]);
        }
        res = min(res, mx);
    }
    if(res == inf){
        res = -1;
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 15148
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

void bfs(vector<vector<int>> &G, vector<int> &d, int s) {
  d[s] = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < G[v].size(); i++) {
      if (d[G[v][i]] == 1e9) {
        d[G[v][i]] = d[v] + 1;
        q.push(G[v][i]);
      }
    }
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  vector<vector<int>> G(2 * n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].emplace_back(v + n);
    G[v + n].emplace_back(u);
    G[v].emplace_back(u + n);
    G[u + n].emplace_back(v);
  }

  vector<int> d(2 * n, 1e9);
  d[0] = 0;
  bfs(G, d, 0);
  d[0] = 2;

  int evenmax = -1, oddmax = -1;
  for (int i = 0; i < n; i++) {
    evenmax = max(evenmax, d[i]);
    oddmax = max(oddmax, d[i + n]);
  }

  if (min(evenmax, oddmax) == 1e9) cout << -1 << '\n';
  else cout << min(evenmax, oddmax) << '\n';
}
accuracy: 1
time_ms: 30
memory_kb: 15940
final_rank: 10