#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define endl '\n'
const ll MOD = 1e9+7;
const ll INF = 1e18;
const ll MAX = 1e5;
vector<vector<int>> edges(MAX);
vector<bool> visited(MAX, false);
stack<int> recVisited;
int getLengthChild(int cur){
if(visited[cur]) return 0;
visited[cur] = true;
int ans = 0;
for(int tar : edges[cur]){
ans = max(ans, getLengthChild(tar));
}
return ans+1;
}
pair<int,int> getFurthest(int cur, int deg){
if(visited[cur]){
return {-1,-1};
}
visited[cur] = true;
recVisited.push(cur);
pair<int,int> ans = {cur, deg};
for(int tar : edges[cur]){
pair<int,int> furth = getFurthest(tar, deg+1);
if(furth.second > ans.second) ans = furth;
}
return ans;
}
void solve(){
int n,m;
cin>>n>>m;
for(int i = 0; i < m; i++){
int a,b; cin>>a>>b;
a--,b--;
edges[a].push_back(b);
edges[b].push_back(a);
}
vector<int> ans;
for(int i = 0; i < n; i++){
if(!visited[i]){
int furth = getFurthest(i, 0).first;
while(!recVisited.empty()){
visited[recVisited.top()] = false;
recVisited.pop();
}
ans.push_back(getLengthChild(furth));
}
}
int sum = 0;
for(int a : ans) sum+=a;
cout<<sum;
}
int main()
{
cin.tie(NULL);
ios::sync_with_stdio(NULL);
int t = 1;
//cin>>t;
int temp = t;
while(t--){
//cout<<"Case #"<<temp - t<<" > "<<endl;
solve();
cout<<endl;
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:90:9: warning: unused variable 'temp' [-Wunused-variable]
90 | int temp = t;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
3412 KB |
Output is correct |
2 |
Correct |
15 ms |
4368 KB |
Output is correct |
3 |
Correct |
20 ms |
5076 KB |
Output is correct |
4 |
Correct |
26 ms |
5780 KB |
Output is correct |
5 |
Correct |
37 ms |
6604 KB |
Output is correct |
6 |
Correct |
43 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
14156 KB |
Output is correct |
2 |
Correct |
4 ms |
3156 KB |
Output is correct |
3 |
Correct |
6 ms |
3684 KB |
Output is correct |
4 |
Correct |
11 ms |
4104 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 |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
5 ms |
3284 KB |
Output is correct |
6 |
Correct |
7 ms |
3856 KB |
Output is correct |
7 |
Correct |
12 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2692 KB |
Output is correct |
4 |
Correct |
2 ms |
2692 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 |
2696 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 |
8 ms |
3412 KB |
Output is correct |
2 |
Correct |
15 ms |
4368 KB |
Output is correct |
3 |
Correct |
20 ms |
5076 KB |
Output is correct |
4 |
Correct |
26 ms |
5780 KB |
Output is correct |
5 |
Correct |
37 ms |
6604 KB |
Output is correct |
6 |
Correct |
43 ms |
7424 KB |
Output is correct |
7 |
Correct |
32 ms |
14156 KB |
Output is correct |
8 |
Correct |
4 ms |
3156 KB |
Output is correct |
9 |
Correct |
6 ms |
3684 KB |
Output is correct |
10 |
Correct |
11 ms |
4104 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
5 ms |
3284 KB |
Output is correct |
16 |
Correct |
7 ms |
3856 KB |
Output is correct |
17 |
Correct |
12 ms |
4440 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
2 ms |
2692 KB |
Output is correct |
21 |
Correct |
2 ms |
2692 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 |
2696 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
52 ms |
7404 KB |
Output is correct |
30 |
Correct |
11 ms |
4312 KB |
Output is correct |
31 |
Correct |
28 ms |
6860 KB |
Output is correct |
32 |
Correct |
16 ms |
5200 KB |
Output is correct |
33 |
Correct |
39 ms |
6964 KB |
Output is correct |
34 |
Correct |
29 ms |
5568 KB |
Output is correct |
35 |
Correct |
32 ms |
6808 KB |
Output is correct |
36 |
Correct |
33 ms |
6184 KB |
Output is correct |
37 |
Correct |
38 ms |
6912 KB |
Output is correct |
38 |
Correct |
40 ms |
6640 KB |
Output is correct |
39 |
Correct |
27 ms |
6900 KB |
Output is correct |