이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
vector<int>adj[100001];
bool vis[100001]={false};
int again, maxdepth;
void dfs(int current, int past, int depth) {
vis[current]=true;
if(depth>maxdepth) {
maxdepth=depth;
again=current;
}
for(auto iter:adj[current]) {
if(iter != past) {
dfs(iter, current, depth+1);
}
}
}
int longest(int i) {
maxdepth=0;
dfs(i, -1, 1);
maxdepth=0;
dfs(again, -1, 1);
return maxdepth;
}
int main() {
int N,M,A,B;
cin >> N >> M;
for(int i=1; i<=M; i++) {
cin >> A >> B;
adj[A].push_back(B);
adj[B].push_back(A);
}
int sum=0;
for(int i=1; i<=N; i++) {
if(vis[i]==false) {
sum+=longest(i);
}
}
cout << sum << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |