#include<bits/stdc++.h>
using namespace std;
long long n, m;
vector <long long> vec[100005];
bool visited[100005], visited1[100005];
pair <long long, long long> ans;
void dfs (long long v, long long d)
{
if (d > ans.first)
{
ans = {d, v};
}
visited[v] = 1;
for (int i = 0; i < vec[v].size (); i++)
{
long long id = vec[v][i];
if (!visited[id])
{
dfs (id, d + 1);
}
}
}
void dfs1 (long long v, long long d)
{
if (d > ans.first)
{
ans = {d, v};
}
visited1[v] = 1;
for (int i = 0; i < vec[v].size (); i++)
{
long long id = vec[v][i];
if (!visited1[id])
{
dfs1 (id, d + 1);
}
}
}
int main ()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--)
{
long long len = 0;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
long long x, y;
cin >> x >> y;
vec[x].push_back (y);
vec[y].push_back (x);
}
for (int i = 1; i <= n; i++)
{
if (!visited[i])
{
dfs (i, 1);
ans.first = 0;
dfs1 (ans.second, 1);
len += ans.first;
ans = {0, 0};
}
}
cout << len;
}
}
Compilation message
Main.cpp: In function 'void dfs(long long int, long long int)':
Main.cpp:16:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int i = 0; i < vec[v].size (); i++)
| ~~^~~~~~~~~~~~~~~~
Main.cpp: In function 'void dfs1(long long int, long long int)':
Main.cpp:33:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for (int i = 0; i < vec[v].size (); i++)
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3460 KB |
Output is correct |
2 |
Correct |
10 ms |
4364 KB |
Output is correct |
3 |
Correct |
14 ms |
5092 KB |
Output is correct |
4 |
Correct |
23 ms |
6004 KB |
Output is correct |
5 |
Correct |
25 ms |
6780 KB |
Output is correct |
6 |
Correct |
36 ms |
7648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
11676 KB |
Output is correct |
2 |
Correct |
3 ms |
3156 KB |
Output is correct |
3 |
Correct |
6 ms |
3712 KB |
Output is correct |
4 |
Correct |
7 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
4 ms |
3284 KB |
Output is correct |
6 |
Correct |
6 ms |
3984 KB |
Output is correct |
7 |
Correct |
8 ms |
4684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 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 |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3460 KB |
Output is correct |
2 |
Correct |
10 ms |
4364 KB |
Output is correct |
3 |
Correct |
14 ms |
5092 KB |
Output is correct |
4 |
Correct |
23 ms |
6004 KB |
Output is correct |
5 |
Correct |
25 ms |
6780 KB |
Output is correct |
6 |
Correct |
36 ms |
7648 KB |
Output is correct |
7 |
Correct |
24 ms |
11676 KB |
Output is correct |
8 |
Correct |
3 ms |
3156 KB |
Output is correct |
9 |
Correct |
6 ms |
3712 KB |
Output is correct |
10 |
Correct |
7 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
4 ms |
3284 KB |
Output is correct |
16 |
Correct |
6 ms |
3984 KB |
Output is correct |
17 |
Correct |
8 ms |
4684 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 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 |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
41 ms |
7712 KB |
Output is correct |
30 |
Correct |
9 ms |
3924 KB |
Output is correct |
31 |
Correct |
18 ms |
6648 KB |
Output is correct |
32 |
Correct |
12 ms |
4736 KB |
Output is correct |
33 |
Correct |
26 ms |
7372 KB |
Output is correct |
34 |
Correct |
17 ms |
5460 KB |
Output is correct |
35 |
Correct |
20 ms |
6868 KB |
Output is correct |
36 |
Correct |
22 ms |
6104 KB |
Output is correct |
37 |
Correct |
24 ms |
7408 KB |
Output is correct |
38 |
Correct |
37 ms |
6788 KB |
Output is correct |
39 |
Correct |
21 ms |
7040 KB |
Output is correct |