This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define float double
#define pb push_back
#define F first
#define S second
#define T int t; cin >> t; while(t--)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
/// Benzema is the best player in the world
const int N = 1e6 + 6;
const int M = 1e3 + 3;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int LOG = 25;
int n, m, dp[N], pars[N];
vector<int> adj[N];
int Rec(int node, int par) {
if (adj[node].size() == 1) return 1;
int &ret = dp[node];
if (ret != -1) return ret;
ret = 0;
for(auto x: adj[node]) {
if (x != par)
ret = max(ret, Rec(x, node) + 1);
}
return ret;
}
vector<int> vec;
bool vis[N];
void dfs(int node, int par) {
vec.push_back(node);
pars[node] = par;
vis[node] = 1;
for(auto x: adj[node]) {
if (x != par)
dfs(x, node);
}
}
main() {
IOS
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(dp, -1, sizeof dp);
int ans = 0;
for(int i = 1; i <= n; i++) {
if (vis[i]) continue;
vec.clear();
dfs(i, 0);
int mx = 0;
for(auto node: vec) {
int mx1 = 0, mx2 = 0;
for(auto x: adj[node]) {
if (pars[x] != node) continue;
if (Rec(x, node) > mx1) mx2 = mx1, mx1 = Rec(x, node);
else if (Rec(x, node) > mx2) mx2 = Rec(x, node);
}
mx = max(mx, mx1 + mx2 + 1);
}
ans += mx;
}
cout << ans;
}
Compilation message (stderr)
Main.cpp:45:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
45 | main() {
| ^~~~
# | 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... |