이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const double pi = 3.14159265358979323846;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
int d, aux, cmp[MAXN], mark[MAXN];
vector<int> v[MAXN];
void dfs(int x, int dist){
cmp[x]=1;
mark[x]=1;
if(dist>d){
aux=x;
d=dist;
}
for(auto u : v[x]){
if(cmp[u]==0) dfs(u, dist+1);
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int ans=0, n, m; cin >> n >> m;
for(int i=0;i<m;i++){
int u, a; cin >> u >> a;
v[u].pb(a);
v[a].pb(u);
}
for(int i=1;i<=n;i++){
if(mark[i]==0){
d=-1, aux=-1;
memset(cmp, 0, sizeof cmp);
dfs(i, 0);
if(d==0){
ans++;
continue;
}
d=-1;
memset(cmp, 0, sizeof cmp);
dfs(aux, 0);
d++;
ans+=d;
}
}
cout << ans << "\n";
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... |