#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define sz size
#define Junanna ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const ll K = 32;
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;
const ll N = (1e5 + 125);
ll n, m;
vector <ll> g[N];
bool used[N], used1[N];
pair <ll, ll> ans;
void dfs (ll v, ll d)
{
if (d > ans.fi)
{
ans = {d, v};
}
used[v] = 1;
for (ll i = 0; i < g[v].sz (); i++)
{
ll to = g[v][i];
if (!used[to])
{
dfs (to, d + 1);
}
}
}
void dfss (ll v, ll d)
{
if (d > ans.fi)
{
ans = {d, v};
}
used1[v] = 1;
for (ll i = 0; i < g[v].sz (); i++)
{
ll to = g[v][i];
if (!used1[to])
{
dfss (to, d + 1);
}
}
}
void ma1n ()
{
cin >> n >> m;
for (ll i = 1; i <= m; i++)
{
ll x, y;
cin >> x >> y;
g[x].pb (y);
g[y].pb (x);
}
ll dis = 0;
for (ll i = 1; i <= n; i++)
{
if (!used[i])
{
dfs (i, 1);
ans.fi = 0;
dfss (ans.se, 1);
dis += ans.fi;
ans = {0, 0};
}
}
cout << dis;
}
int main ()
{
Junanna;
ll t = 1;
while (t--)
{
ma1n ();
}
return 0;
}
Compilation message
Main.cpp: In function 'void dfs(long long int, long long int)':
Main.cpp:29:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (ll i = 0; i < g[v].sz (); i++)
| ~~^~~~~~~~~~~~
Main.cpp: In function 'void dfss(long long int, long long int)':
Main.cpp:46:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (ll i = 0; i < g[v].sz (); i++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3456 KB |
Output is correct |
2 |
Correct |
15 ms |
4272 KB |
Output is correct |
3 |
Correct |
19 ms |
5176 KB |
Output is correct |
4 |
Correct |
39 ms |
6016 KB |
Output is correct |
5 |
Correct |
32 ms |
6904 KB |
Output is correct |
6 |
Correct |
51 ms |
7756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
11780 KB |
Output is correct |
2 |
Correct |
4 ms |
3184 KB |
Output is correct |
3 |
Correct |
6 ms |
3716 KB |
Output is correct |
4 |
Correct |
9 ms |
4128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
5 ms |
3332 KB |
Output is correct |
6 |
Correct |
7 ms |
3976 KB |
Output is correct |
7 |
Correct |
9 ms |
4728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3456 KB |
Output is correct |
2 |
Correct |
15 ms |
4272 KB |
Output is correct |
3 |
Correct |
19 ms |
5176 KB |
Output is correct |
4 |
Correct |
39 ms |
6016 KB |
Output is correct |
5 |
Correct |
32 ms |
6904 KB |
Output is correct |
6 |
Correct |
51 ms |
7756 KB |
Output is correct |
7 |
Correct |
27 ms |
11780 KB |
Output is correct |
8 |
Correct |
4 ms |
3184 KB |
Output is correct |
9 |
Correct |
6 ms |
3716 KB |
Output is correct |
10 |
Correct |
9 ms |
4128 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
5 ms |
3332 KB |
Output is correct |
16 |
Correct |
7 ms |
3976 KB |
Output is correct |
17 |
Correct |
9 ms |
4728 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2680 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
3 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
51 ms |
7636 KB |
Output is correct |
30 |
Correct |
8 ms |
3924 KB |
Output is correct |
31 |
Correct |
22 ms |
6724 KB |
Output is correct |
32 |
Correct |
16 ms |
4836 KB |
Output is correct |
33 |
Correct |
35 ms |
7352 KB |
Output is correct |
34 |
Correct |
23 ms |
5560 KB |
Output is correct |
35 |
Correct |
26 ms |
6964 KB |
Output is correct |
36 |
Correct |
30 ms |
6124 KB |
Output is correct |
37 |
Correct |
36 ms |
7344 KB |
Output is correct |
38 |
Correct |
36 ms |
6804 KB |
Output is correct |
39 |
Correct |
25 ms |
7044 KB |
Output is correct |