#include<bits/stdc++.h>
using namespace std;
#define NAME "CHAIN"
#define nl '\n'
#define allofa(x,sz) x,x+sz+1
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,val) memset(x,val,sizeof(x))
#define couf(x) cout << fixed << setprecision(x)
template<class T> T Abs(T &x) {return (x>=0 ? x : -x);};
template<class X,class Y> bool minimize(X &a, Y b){if(a>b) {a=b;return true;}return false;};
template<class X,class Y> bool maximize(X &a, Y b){if(a<b) {a=b;return true;}return false;};
typedef long long ll;
const ll mod = (long long)1e9+7;
const ll LINF = (long long)1e15;
const int INF = (int)1e9;
const int MAXN = (int)1e5+5;
vector<int> graph[MAXN];
ll dist[MAXN];
bool visited[MAXN];
int n,m;
void ccps() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen(NAME".inp","r")) {
freopen(NAME".inp","r",stdin);
freopen(NAME".out","w",stdout);
}
}
void dfs(int u, int pre) {
visited[u]=true;
for (int v : graph[u]) {
if(v^pre) {
dist[v]=dist[u]+1;
dfs(v,u);
}
}
}
ll calc(int node) {
fill(allofa(dist,n+1),1);
int start = node;
dfs(start,start);
ll res = -1;
for (int u = 1; u<=n; u++) {
if(maximize(res,dist[u])) {
start = u;
}
}
fill(allofa(dist,n+1),1);
dfs(start,start);
res = -1;
for (int u = 1; u<=n; u++) {
maximize(res,dist[u]);
}
return res;
}
signed main() {
ccps();
cin >> n >> m;
mset(visited,false);
ll res = 0;
for (int i = 1; i<=m; i++) {
int u,v; cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for (int u = 1; u<=n; u++) {
if(!visited[u]) {
res+=calc(u);
}
}
cout << res;
}
Compilation message (stderr)
Main.cpp: In function 'void ccps()':
Main.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen(NAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen(NAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |