/*
In the name of Allah
made by: Ahmed_Solyman
*/
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O1")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
cout<<(x?"YES":"NO")<<endl;
}
const int N=1e5+5;
vector<int>adj[N];
bool vis[N];
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#ifndef ONLINE_JUDGE
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
fast
int n,m;cin>>n>>m;
for(int i=0;i<m;i++){
int u,v;cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int ans=0;
for(int i=1;i<=n;i++){
if(!vis[i]){
map<int,int>dist;
map<int,bool>v;
queue<int>q;
q.push(i);
dist[i]=0;
int mx=0;
int root=0;
while(q.size()){
int x=q.front(); q.pop();
if(dist[x]>=mx){
mx=dist[x];
root=x;
}
vis[x]=1;v[x]=1;
for(auto ch:adj[x]){
if(!v[ch]){
dist[ch]=dist[x]+1;
q.push(ch);
}
}
}
dist[root]=0;
v.clear();
dist.clear();
q.push(root);
dist[root]=0;
mx=0;
while(q.size()){
int x=q.front(); q.pop();
if(dist[x]>=mx){
mx=dist[x];
}
vis[x]=1;v[x]=1;
for(auto ch:adj[x]){
if(!v[ch]){
dist[ch]=dist[x]+1;
q.push(ch);
}
}
}
ans+=mx+1;
}
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
4956 KB |
Output is correct |
2 |
Correct |
74 ms |
7280 KB |
Output is correct |
3 |
Correct |
131 ms |
9556 KB |
Output is correct |
4 |
Correct |
161 ms |
11848 KB |
Output is correct |
5 |
Correct |
212 ms |
14316 KB |
Output is correct |
6 |
Correct |
283 ms |
16620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
16432 KB |
Output is correct |
2 |
Correct |
6 ms |
3164 KB |
Output is correct |
3 |
Correct |
11 ms |
3676 KB |
Output is correct |
4 |
Correct |
18 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
14 ms |
4700 KB |
Output is correct |
6 |
Correct |
27 ms |
6748 KB |
Output is correct |
7 |
Correct |
41 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2904 KB |
Output is correct |
6 |
Correct |
1 ms |
2848 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2924 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
4956 KB |
Output is correct |
2 |
Correct |
74 ms |
7280 KB |
Output is correct |
3 |
Correct |
131 ms |
9556 KB |
Output is correct |
4 |
Correct |
161 ms |
11848 KB |
Output is correct |
5 |
Correct |
212 ms |
14316 KB |
Output is correct |
6 |
Correct |
283 ms |
16620 KB |
Output is correct |
7 |
Correct |
153 ms |
16432 KB |
Output is correct |
8 |
Correct |
6 ms |
3164 KB |
Output is correct |
9 |
Correct |
11 ms |
3676 KB |
Output is correct |
10 |
Correct |
18 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
14 ms |
4700 KB |
Output is correct |
16 |
Correct |
27 ms |
6748 KB |
Output is correct |
17 |
Correct |
41 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2648 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2904 KB |
Output is correct |
23 |
Correct |
1 ms |
2848 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2924 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
339 ms |
16700 KB |
Output is correct |
30 |
Correct |
24 ms |
3928 KB |
Output is correct |
31 |
Correct |
35 ms |
6788 KB |
Output is correct |
32 |
Correct |
30 ms |
4696 KB |
Output is correct |
33 |
Correct |
58 ms |
7336 KB |
Output is correct |
34 |
Correct |
45 ms |
5592 KB |
Output is correct |
35 |
Correct |
50 ms |
6744 KB |
Output is correct |
36 |
Correct |
127 ms |
10532 KB |
Output is correct |
37 |
Correct |
59 ms |
7004 KB |
Output is correct |
38 |
Correct |
250 ms |
13952 KB |
Output is correct |
39 |
Correct |
51 ms |
6920 KB |
Output is correct |