#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 25;
vector <int> adj[MAXN];
int dp[MAXN][2];
bool vis[MAXN];
void dfs (int pos, int par) {
vis[pos] = 1;
int mx1 = 0, mx2 = 0, mx3 = 0;
for (auto j : adj[pos]) {
if (j == par) continue;
dfs(j, pos);
if (1 + dp[j][1] > mx1) {
mx2 = mx1; mx1 = dp[j][1];
} else if (dp[j][1] > mx2) {
mx2 = dp[j][1];
}
mx3 = max(mx3, dp[j][0]);
}
dp[pos][0] = max(1 + mx1 + mx2, mx3);
dp[pos][1] = 1 + mx1;
}
int main () {
int mx = 0;
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i, -1);
mx += dp[i][0];
}
}
cout << mx << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
3932 KB |
Output is correct |
2 |
Correct |
18 ms |
4952 KB |
Output is correct |
3 |
Correct |
24 ms |
5724 KB |
Output is correct |
4 |
Correct |
37 ms |
6312 KB |
Output is correct |
5 |
Correct |
43 ms |
7084 KB |
Output is correct |
6 |
Correct |
54 ms |
7504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
16076 KB |
Output is correct |
2 |
Correct |
5 ms |
3928 KB |
Output is correct |
3 |
Correct |
11 ms |
4364 KB |
Output is correct |
4 |
Correct |
13 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3248 KB |
Output is correct |
2 |
Correct |
1 ms |
3416 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
6 ms |
3932 KB |
Output is correct |
6 |
Correct |
11 ms |
4524 KB |
Output is correct |
7 |
Correct |
18 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3484 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3488 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3672 KB |
Output is correct |
10 |
Correct |
2 ms |
3416 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
3932 KB |
Output is correct |
2 |
Correct |
18 ms |
4952 KB |
Output is correct |
3 |
Correct |
24 ms |
5724 KB |
Output is correct |
4 |
Correct |
37 ms |
6312 KB |
Output is correct |
5 |
Correct |
43 ms |
7084 KB |
Output is correct |
6 |
Correct |
54 ms |
7504 KB |
Output is correct |
7 |
Correct |
58 ms |
16076 KB |
Output is correct |
8 |
Correct |
5 ms |
3928 KB |
Output is correct |
9 |
Correct |
11 ms |
4364 KB |
Output is correct |
10 |
Correct |
13 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
3248 KB |
Output is correct |
12 |
Correct |
1 ms |
3416 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
15 |
Correct |
6 ms |
3932 KB |
Output is correct |
16 |
Correct |
11 ms |
4524 KB |
Output is correct |
17 |
Correct |
18 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
3420 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
1 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
3484 KB |
Output is correct |
22 |
Correct |
1 ms |
3420 KB |
Output is correct |
23 |
Correct |
1 ms |
3488 KB |
Output is correct |
24 |
Correct |
1 ms |
3420 KB |
Output is correct |
25 |
Correct |
1 ms |
3420 KB |
Output is correct |
26 |
Correct |
1 ms |
3672 KB |
Output is correct |
27 |
Correct |
2 ms |
3416 KB |
Output is correct |
28 |
Correct |
2 ms |
3420 KB |
Output is correct |
29 |
Correct |
51 ms |
7508 KB |
Output is correct |
30 |
Correct |
10 ms |
4700 KB |
Output is correct |
31 |
Correct |
34 ms |
7200 KB |
Output is correct |
32 |
Correct |
28 ms |
5464 KB |
Output is correct |
33 |
Correct |
50 ms |
7492 KB |
Output is correct |
34 |
Correct |
27 ms |
6164 KB |
Output is correct |
35 |
Correct |
41 ms |
7420 KB |
Output is correct |
36 |
Correct |
36 ms |
6736 KB |
Output is correct |
37 |
Correct |
50 ms |
7308 KB |
Output is correct |
38 |
Correct |
44 ms |
7004 KB |
Output is correct |
39 |
Correct |
43 ms |
7520 KB |
Output is correct |