mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2025-02-20 00:06:36 +00:00
parent
1feb2670c5
commit
ba8abcf36c
56
Gym/103081/I/I.cpp
Normal file
56
Gym/103081/I/I.cpp
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#include <cmath>
|
||||||
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
#include <queue>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using std::cin;
|
||||||
|
using std::cout;
|
||||||
|
using std::endl;
|
||||||
|
|
||||||
|
const int N = 100005;
|
||||||
|
|
||||||
|
int n, m;
|
||||||
|
std::vector<int> g[N];
|
||||||
|
|
||||||
|
// Shortest Path
|
||||||
|
int dist[N], max;
|
||||||
|
bool vis[N];
|
||||||
|
void spfa() {
|
||||||
|
memset(dist, 0x3f, sizeof(dist));
|
||||||
|
std::queue<int> q;
|
||||||
|
q.push(1);
|
||||||
|
dist[1] = 0;
|
||||||
|
while (!q.empty()) {
|
||||||
|
int u = q.front();
|
||||||
|
q.pop();
|
||||||
|
if (vis[u]) continue;
|
||||||
|
vis[u] = true;
|
||||||
|
for (int v : g[u]) {
|
||||||
|
if (dist[v] > dist[u] + 1) {
|
||||||
|
dist[v] = dist[u] + 1;
|
||||||
|
q.push(v);
|
||||||
|
max = std::max(max, dist[v]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cin >> n >> m;
|
||||||
|
for (int i = 1; i <= m; i++) {
|
||||||
|
int u, v;
|
||||||
|
cin >> u >> v;
|
||||||
|
g[u].push_back(v);
|
||||||
|
g[v].push_back(u);
|
||||||
|
}
|
||||||
|
spfa();
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
if (dist[i] == 0x3f3f3f3f) {
|
||||||
|
cout << -1 << endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << (int)ceil(log2(max)) + 1 << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user