#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) {
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<n-1;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;
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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3676 KB |
Output is correct |
2 |
Correct |
9 ms |
4700 KB |
Output is correct |
3 |
Correct |
13 ms |
5212 KB |
Output is correct |
4 |
Correct |
17 ms |
5956 KB |
Output is correct |
5 |
Correct |
22 ms |
6944 KB |
Output is correct |
6 |
Correct |
24 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
11100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
5980 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3160 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3676 KB |
Output is correct |
2 |
Correct |
9 ms |
4700 KB |
Output is correct |
3 |
Correct |
13 ms |
5212 KB |
Output is correct |
4 |
Correct |
17 ms |
5956 KB |
Output is correct |
5 |
Correct |
22 ms |
6944 KB |
Output is correct |
6 |
Correct |
24 ms |
7260 KB |
Output is correct |
7 |
Incorrect |
24 ms |
11100 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |