#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=1e18;
const ll mod=1e9+7;
ll h[maxN];
ll lab[maxN];
vector<ll>vec[maxN],g[maxN];
ll findset(ll x)
{
return lab[x]<0?x:lab[x]=findset(lab[x]);
}
void unite(ll u,ll v)
{
u=findset(u);
v=findset(v);
if(u==v) return;
if(lab[u]>lab[v]) swap(u,v);
lab[u]+=lab[v];
lab[v]=u;
for(auto zz:vec[v]) vec[u].pb(zz);
vec[v].clear();
}
void dfs(ll u,ll p)
{
for(int v:g[u])
{
if(v!=p)
{
h[v]=h[u]+1;
dfs(v,u);
}
}
}
ll ans=0;
ll m;
ll n;
void solve()
{
cin >> n >> m;
for(int i=1;i<=n;i++) lab[i]=-1,vec[i].pb(i);
for(int i=1;i<=m;i++)
{
ll u,v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
unite(u,v);
}
for(int i=1;i<=n;i++)
{
if(findset(i)==i)
{
dfs(i,0);
ll mx=-1,id=-1;
h[i]=0;
for(auto zz:vec[i])
{
if(h[zz]>mx)
{
mx=h[zz];
id=zz;
}
}
h[id]=0;
dfs(id,0);
mx=-1;
for(auto zz:vec[i])
{
mx=max(mx,h[zz]);
}
ans+=mx+1;
}
}
cout << ans;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
11220 KB |
Output is correct |
2 |
Correct |
22 ms |
12776 KB |
Output is correct |
3 |
Correct |
39 ms |
14320 KB |
Output is correct |
4 |
Correct |
45 ms |
15816 KB |
Output is correct |
5 |
Correct |
59 ms |
17304 KB |
Output is correct |
6 |
Correct |
100 ms |
18940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
21096 KB |
Output is correct |
2 |
Correct |
11 ms |
10960 KB |
Output is correct |
3 |
Correct |
19 ms |
12116 KB |
Output is correct |
4 |
Correct |
24 ms |
13400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9732 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
10 ms |
11092 KB |
Output is correct |
6 |
Correct |
20 ms |
12628 KB |
Output is correct |
7 |
Correct |
22 ms |
14016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
8 ms |
9796 KB |
Output is correct |
6 |
Correct |
7 ms |
9688 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9808 KB |
Output is correct |
9 |
Correct |
6 ms |
9804 KB |
Output is correct |
10 |
Correct |
7 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
11220 KB |
Output is correct |
2 |
Correct |
22 ms |
12776 KB |
Output is correct |
3 |
Correct |
39 ms |
14320 KB |
Output is correct |
4 |
Correct |
45 ms |
15816 KB |
Output is correct |
5 |
Correct |
59 ms |
17304 KB |
Output is correct |
6 |
Correct |
100 ms |
18940 KB |
Output is correct |
7 |
Correct |
56 ms |
21096 KB |
Output is correct |
8 |
Correct |
11 ms |
10960 KB |
Output is correct |
9 |
Correct |
19 ms |
12116 KB |
Output is correct |
10 |
Correct |
24 ms |
13400 KB |
Output is correct |
11 |
Correct |
5 ms |
9732 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
10 ms |
11092 KB |
Output is correct |
16 |
Correct |
20 ms |
12628 KB |
Output is correct |
17 |
Correct |
22 ms |
14016 KB |
Output is correct |
18 |
Correct |
5 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
8 ms |
9796 KB |
Output is correct |
23 |
Correct |
7 ms |
9688 KB |
Output is correct |
24 |
Correct |
7 ms |
9684 KB |
Output is correct |
25 |
Correct |
6 ms |
9808 KB |
Output is correct |
26 |
Correct |
6 ms |
9804 KB |
Output is correct |
27 |
Correct |
7 ms |
9812 KB |
Output is correct |
28 |
Correct |
6 ms |
9812 KB |
Output is correct |
29 |
Correct |
85 ms |
18944 KB |
Output is correct |
30 |
Correct |
34 ms |
15268 KB |
Output is correct |
31 |
Correct |
81 ms |
17972 KB |
Output is correct |
32 |
Correct |
34 ms |
16096 KB |
Output is correct |
33 |
Correct |
69 ms |
19012 KB |
Output is correct |
34 |
Correct |
74 ms |
17048 KB |
Output is correct |
35 |
Correct |
71 ms |
17912 KB |
Output is correct |
36 |
Correct |
62 ms |
18156 KB |
Output is correct |
37 |
Correct |
62 ms |
19056 KB |
Output is correct |
38 |
Correct |
75 ms |
18988 KB |
Output is correct |
39 |
Correct |
85 ms |
18600 KB |
Output is correct |