#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
const int INF = 2147483645;
const int maxN = (int)1e5+5;
const ll LLINF = LLONG_MAX;
//const ll mod = 998244353;
const ll mod = 1000000007;
vector<int> adj[maxN];
int dist[maxN];
bool vis[maxN], bck[maxN];
void dfs(int s) {
vis[s] = true;
for (auto i: adj[s]) {
if (vis[i]) continue;
vis[i] = true;
dist[i] = dist[s]+1;
dfs(i);
}
}
void solv() {
int n, m, a, b;
cin>>n>>m;
for (int i=0;i<m;i++) {
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(1);
int mx = 0, start = 1;
for (int i=1;i<=n;i++) {
if (dist[i] > mx) {
mx = dist[i];
start = i;
}
}
memset(dist, 0, sizeof dist);
memset(vis, 0, sizeof vis);
dfs(start);
mx = 0;
for (int i=1;i<=n;i++) {
mx = max(mx, dist[i]);
}
if (vis[2]) {
cout<<mx+1<<'\n';
return;
}
memset(dist, 0, sizeof dist);
memset(vis, 0, sizeof vis);
dfs(2);
int mx2 = 0;
start = 2;
for (int i=1;i<=n;i++) {
if (dist[i] > mx2) {
mx2 = dist[i];
start = i;
}
}
memset(dist, 0, sizeof dist);
memset(vis, 0, sizeof vis);
dfs(start);
mx2 = 0;
for (int i=1;i<=n;i++) {
mx2 = max(mx2, dist[i]);
}
cout<<mx+mx2+2<<'\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t=1;
// cin>>t;
while (t--) solv();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3672 KB |
Output is correct |
2 |
Correct |
9 ms |
4188 KB |
Output is correct |
3 |
Correct |
19 ms |
4808 KB |
Output is correct |
4 |
Correct |
36 ms |
5200 KB |
Output is correct |
5 |
Correct |
49 ms |
5908 KB |
Output is correct |
6 |
Correct |
41 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
10992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
2 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Incorrect |
5 ms |
3676 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3416 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3160 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3672 KB |
Output is correct |
2 |
Correct |
9 ms |
4188 KB |
Output is correct |
3 |
Correct |
19 ms |
4808 KB |
Output is correct |
4 |
Correct |
36 ms |
5200 KB |
Output is correct |
5 |
Correct |
49 ms |
5908 KB |
Output is correct |
6 |
Correct |
41 ms |
6492 KB |
Output is correct |
7 |
Incorrect |
25 ms |
10992 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |