#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define all(x) x.begin(), x.end()
// mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
// int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
const int NM = 1e5 + 5;
const int LM = 1e4 + 5;
const int mod = 1e9 + 7;
vector<int> g[NM];
vector<pair<int, int>> V;
int ans, n, m, dep[NM], d, vis[NM];
void dfs(int u, int p = 0)
{
vis[u] = 1;
V.push_back({dep[u], u});
for (int v : g[u])
if (v != p)
{
dep[v] = dep[u] + 1;
dfs(v, u);
}
}
void DFS(int u, int l, int p = 0)
{
setmax(d, l);
for (int v : g[u])
if (v != p)
DFS(v, l + 1, u);
}
void solve()
{
cin >> n >> m;
while (m--)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++)
if (!vis[i])
{
V.clear();
dfs(i);
int u = (*max_element(V.begin(), V.end())).se;
d = 1;
DFS(u, 1);
ans += d;
}
cout << ans << endl;
}
signed main()
{
fastIO
if (fopen("in.txt", "r"))
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
int tc = 1;
// cin >> tc;
while (tc--)
solve();
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen("in.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen("out.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5848 KB |
Output is correct |
2 |
Correct |
10 ms |
7124 KB |
Output is correct |
3 |
Correct |
13 ms |
7900 KB |
Output is correct |
4 |
Correct |
17 ms |
9660 KB |
Output is correct |
5 |
Correct |
23 ms |
10440 KB |
Output is correct |
6 |
Correct |
26 ms |
11208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
19660 KB |
Output is correct |
2 |
Correct |
3 ms |
4768 KB |
Output is correct |
3 |
Correct |
5 ms |
5212 KB |
Output is correct |
4 |
Correct |
6 ms |
5724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
1 ms |
4188 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
4 ms |
5344 KB |
Output is correct |
6 |
Correct |
6 ms |
6360 KB |
Output is correct |
7 |
Correct |
8 ms |
7620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
1 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
1 ms |
4188 KB |
Output is correct |
8 |
Correct |
2 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
1 ms |
4188 KB |
Output is correct |
11 |
Correct |
1 ms |
4188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5848 KB |
Output is correct |
2 |
Correct |
10 ms |
7124 KB |
Output is correct |
3 |
Correct |
13 ms |
7900 KB |
Output is correct |
4 |
Correct |
17 ms |
9660 KB |
Output is correct |
5 |
Correct |
23 ms |
10440 KB |
Output is correct |
6 |
Correct |
26 ms |
11208 KB |
Output is correct |
7 |
Correct |
23 ms |
19660 KB |
Output is correct |
8 |
Correct |
3 ms |
4768 KB |
Output is correct |
9 |
Correct |
5 ms |
5212 KB |
Output is correct |
10 |
Correct |
6 ms |
5724 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
12 |
Correct |
1 ms |
4188 KB |
Output is correct |
13 |
Correct |
1 ms |
4188 KB |
Output is correct |
14 |
Correct |
1 ms |
4188 KB |
Output is correct |
15 |
Correct |
4 ms |
5344 KB |
Output is correct |
16 |
Correct |
6 ms |
6360 KB |
Output is correct |
17 |
Correct |
8 ms |
7620 KB |
Output is correct |
18 |
Correct |
3 ms |
4188 KB |
Output is correct |
19 |
Correct |
1 ms |
4188 KB |
Output is correct |
20 |
Correct |
2 ms |
4188 KB |
Output is correct |
21 |
Correct |
2 ms |
4188 KB |
Output is correct |
22 |
Correct |
1 ms |
4188 KB |
Output is correct |
23 |
Correct |
2 ms |
4188 KB |
Output is correct |
24 |
Correct |
1 ms |
4188 KB |
Output is correct |
25 |
Correct |
2 ms |
4188 KB |
Output is correct |
26 |
Correct |
2 ms |
4188 KB |
Output is correct |
27 |
Correct |
1 ms |
4188 KB |
Output is correct |
28 |
Correct |
1 ms |
4188 KB |
Output is correct |
29 |
Correct |
35 ms |
11208 KB |
Output is correct |
30 |
Correct |
7 ms |
5212 KB |
Output is correct |
31 |
Correct |
18 ms |
8020 KB |
Output is correct |
32 |
Correct |
10 ms |
6236 KB |
Output is correct |
33 |
Correct |
22 ms |
8792 KB |
Output is correct |
34 |
Correct |
14 ms |
7004 KB |
Output is correct |
35 |
Correct |
19 ms |
8284 KB |
Output is correct |
36 |
Correct |
22 ms |
8916 KB |
Output is correct |
37 |
Correct |
23 ms |
8796 KB |
Output is correct |
38 |
Correct |
28 ms |
10452 KB |
Output is correct |
39 |
Correct |
21 ms |
8540 KB |
Output is correct |