#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int N = 1e5+5;
vector<int> adj[N];
int dep[N];
int dep2[N];
int vis[N];
int bfs(int src){
vis[src] = 1;
queue<int> q;
q.push(src);
pair<int,int> mxDep {0,src};
while(q.size()){
int node = q.front(); q.pop();
for(auto ch : adj[node]){
if(vis[ch]) continue;
q.push(ch);
vis[ch] = 1;
dep[ch] = dep[node] + 1;
mxDep = max(mxDep,{dep[ch],ch});
}
}
q.push(mxDep.second);
vis[mxDep.second] = 2;
int ret = 1;
while(q.size()){
int node = q.front(); q.pop();
for(auto ch : adj[node]){
if(vis[ch] == 2) continue;
q.push(ch);
vis[ch] = 2;
dep2[ch] = dep2[node] + 1;
ret = max(ret , dep2[ch] + 1);
}
}
return ret;
}
void solve(){
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int ans = 0;
for(int i=1;i<=n;i++){
if(vis[i]) continue;
ans += bfs(i);
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4444 KB |
Output is correct |
2 |
Correct |
10 ms |
5212 KB |
Output is correct |
3 |
Correct |
14 ms |
6144 KB |
Output is correct |
4 |
Correct |
19 ms |
6748 KB |
Output is correct |
5 |
Correct |
21 ms |
7508 KB |
Output is correct |
6 |
Correct |
26 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
8020 KB |
Output is correct |
2 |
Correct |
3 ms |
4188 KB |
Output is correct |
3 |
Correct |
5 ms |
4836 KB |
Output is correct |
4 |
Correct |
7 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3676 KB |
Output is correct |
2 |
Correct |
1 ms |
3804 KB |
Output is correct |
3 |
Correct |
1 ms |
3672 KB |
Output is correct |
4 |
Correct |
1 ms |
3676 KB |
Output is correct |
5 |
Correct |
3 ms |
4420 KB |
Output is correct |
6 |
Correct |
6 ms |
5288 KB |
Output is correct |
7 |
Correct |
8 ms |
5956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3676 KB |
Output is correct |
2 |
Correct |
1 ms |
3676 KB |
Output is correct |
3 |
Correct |
1 ms |
3676 KB |
Output is correct |
4 |
Correct |
1 ms |
3676 KB |
Output is correct |
5 |
Correct |
1 ms |
3676 KB |
Output is correct |
6 |
Correct |
1 ms |
3676 KB |
Output is correct |
7 |
Correct |
1 ms |
3676 KB |
Output is correct |
8 |
Correct |
1 ms |
3804 KB |
Output is correct |
9 |
Correct |
2 ms |
3676 KB |
Output is correct |
10 |
Correct |
1 ms |
3676 KB |
Output is correct |
11 |
Correct |
1 ms |
3768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4444 KB |
Output is correct |
2 |
Correct |
10 ms |
5212 KB |
Output is correct |
3 |
Correct |
14 ms |
6144 KB |
Output is correct |
4 |
Correct |
19 ms |
6748 KB |
Output is correct |
5 |
Correct |
21 ms |
7508 KB |
Output is correct |
6 |
Correct |
26 ms |
8284 KB |
Output is correct |
7 |
Correct |
26 ms |
8020 KB |
Output is correct |
8 |
Correct |
3 ms |
4188 KB |
Output is correct |
9 |
Correct |
5 ms |
4836 KB |
Output is correct |
10 |
Correct |
7 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
3676 KB |
Output is correct |
12 |
Correct |
1 ms |
3804 KB |
Output is correct |
13 |
Correct |
1 ms |
3672 KB |
Output is correct |
14 |
Correct |
1 ms |
3676 KB |
Output is correct |
15 |
Correct |
3 ms |
4420 KB |
Output is correct |
16 |
Correct |
6 ms |
5288 KB |
Output is correct |
17 |
Correct |
8 ms |
5956 KB |
Output is correct |
18 |
Correct |
1 ms |
3676 KB |
Output is correct |
19 |
Correct |
1 ms |
3676 KB |
Output is correct |
20 |
Correct |
1 ms |
3676 KB |
Output is correct |
21 |
Correct |
1 ms |
3676 KB |
Output is correct |
22 |
Correct |
1 ms |
3676 KB |
Output is correct |
23 |
Correct |
1 ms |
3676 KB |
Output is correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
1 ms |
3804 KB |
Output is correct |
26 |
Correct |
2 ms |
3676 KB |
Output is correct |
27 |
Correct |
1 ms |
3676 KB |
Output is correct |
28 |
Correct |
1 ms |
3768 KB |
Output is correct |
29 |
Correct |
31 ms |
8344 KB |
Output is correct |
30 |
Correct |
10 ms |
4956 KB |
Output is correct |
31 |
Correct |
20 ms |
7652 KB |
Output is correct |
32 |
Correct |
13 ms |
5724 KB |
Output is correct |
33 |
Correct |
23 ms |
8064 KB |
Output is correct |
34 |
Correct |
17 ms |
6492 KB |
Output is correct |
35 |
Correct |
24 ms |
7772 KB |
Output is correct |
36 |
Correct |
20 ms |
6920 KB |
Output is correct |
37 |
Correct |
32 ms |
8136 KB |
Output is correct |
38 |
Correct |
26 ms |
7508 KB |
Output is correct |
39 |
Correct |
23 ms |
7816 KB |
Output is correct |