#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);
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;
}
int getLength(int cur){
visited[cur] = true;
int a = 0,b = 0;
for(int tar : edges[cur]){
int cur = getLengthChild(tar);
if(cur > a){
b = a;
a = cur;
}else if(cur > b){
b = cur;
}
}
return a+b+1;
}
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]) ans.push_back(getLength(i));
}
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:79:9: warning: unused variable 'temp' [-Wunused-variable]
79 | int temp = t;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
3284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
10040 KB |
Output is correct |
2 |
Correct |
4 ms |
3204 KB |
Output is correct |
3 |
Correct |
7 ms |
3852 KB |
Output is correct |
4 |
Correct |
9 ms |
4308 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 |
5 ms |
3284 KB |
Output is correct |
6 |
Correct |
8 ms |
3924 KB |
Output is correct |
7 |
Correct |
10 ms |
4496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
3284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |