#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 (m == 0) {
cout<<n<<'\n';
return;
}
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 |
5 ms |
3676 KB |
Output is correct |
2 |
Correct |
9 ms |
4240 KB |
Output is correct |
3 |
Correct |
13 ms |
4876 KB |
Output is correct |
4 |
Correct |
17 ms |
5464 KB |
Output is correct |
5 |
Correct |
21 ms |
5980 KB |
Output is correct |
6 |
Correct |
26 ms |
6336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
11100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Incorrect |
3 ms |
3676 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3164 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3676 KB |
Output is correct |
2 |
Correct |
9 ms |
4240 KB |
Output is correct |
3 |
Correct |
13 ms |
4876 KB |
Output is correct |
4 |
Correct |
17 ms |
5464 KB |
Output is correct |
5 |
Correct |
21 ms |
5980 KB |
Output is correct |
6 |
Correct |
26 ms |
6336 KB |
Output is correct |
7 |
Incorrect |
22 ms |
11100 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |