这道题是codeforces #51F 的数据加强版
感觉应该是状压dp什么的,但数据范围达到了2e4,不得不从其他角度考虑
我们可以发现一些性质:题目最后要形成的是一棵树,那么考虑如果原图中有一个环,那么如果这个环最后没有被缩成一个点,那么始终是不符合题意的,进一步可以发现,这个性质实际上是属于边双联通分量的,所以我么可以先跑一边tarjan,把边双联通分量拉出来缩点,这样原图就变成了一个森林,每个边双联通分量对答案的贡献是 点数-1
接下来考虑一棵树的情况怎么做
我们肯定要选取一条链,先不管我们怎么选出这条链,考虑如果这条链已经被选出了,我们会怎样选取剩下的点使得合并次数最少。我们会发现无论链是什么样的,我们的之后的策略都是一样的:应该保留剩下的点中所有的叶子节点并合并其他的点,这里给出一个简短的证明:
这样一棵树的形态是上面一条长链,链上的每个点上挂着若干棵子树
先证明可行性:选取这些子树的叶子节点显然是符合题意的,我们只要将其他的点和他对应的链上的点合并就好
再证明最优性:假设存在一种方案使得选取的点比这种方法多,那么一定会存在某两个点u,v,其中u是v的祖先,这样的话v到链的距离必定大于1,矛盾
所以,在这样固定的策略下,我们应该选取最长的链,即树的直径,总结一下,在一棵树中我们的选取方案应该是树的直径+其余的叶子节点
最后我们考虑如何使得森林联通,首先要使得森林联通,至少需要子树个数-1次合并操作,而且这样的次数一定是可行的,因为我们可以将每棵树的直径连起来,一定是符合题意的
这样我们就做完了此题
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <utility>
#include <cmath>
#include <bitset>
#include <cctype>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#define LL long long
#define LB long double
#define Pair pair<int,int>
#define pLL pair<LL,LL>
#define pii pair<double,double>
#define x first
#define y second
#define pb push_back
#define pf push_front
#define mp make_pair
using namespace std;
const int MOD=1e9+7;
const int INF=2e9;
const LL LINF=2e16;
const int magic=348;
const double eps=1e-5;
const double pi=acos(-1);
inline int getint()
{
char ch;int res;bool f;
while (!isdigit(ch=getchar()) && ch!='-') {}
if (ch=='-') res=0,f=false; else res=ch-'0',f=true;
while (isdigit(ch=getchar())) res=res*10+ch-'0';
return f?res:-res;
}
int n,e;
int dfn[100048],low[100048],ind;
vector<Pair> v[100048];
vector<int> vv[100048];
bool isbridge[100048];
int ans=0;
int vlist[100048],vtot=0;
bool visited[100048],visited2[100048];
int col[100048];
inline void tarjan(int cur,int father)
{
dfn[cur]=low[cur]=++ind;int i,y;
visited[cur]=true;vlist[++vtot]=cur;
for (i=0;i<int(v[cur].size());i++)
{
y=v[cur][i].x;
if (!visited[y])
{
tarjan(y,cur);
low[cur]=min(low[cur],low[y]);
if (low[y]>=dfn[y]) isbridge[v[cur][i].y]=true;
}
else
if (y!=father) low[cur]=min(low[cur],low[y]);
}
}
inline void dfs(int cur,int curcol)
{
int i,y;col[cur]=curcol;
for (i=0;i<int(v[cur].size());i++)
{
y=v[cur][i].x;
if (isbridge[v[cur][i].y])
{
if (col[y]) vv[curcol].pb(col[y]),vv[col[y]].pb(curcol);
}
else
{
if (col[y]) continue;
dfs(y,curcol);
}
}
}
int depth[100048],fa[100048];
bool isdia[100048];
int dlist[100048],dtot;
inline void Dfs1(int cur,int father)
{
int i;
for (i=0;i<int(vv[cur].size());i++)
if (vv[cur][i]!=father)
{
depth[vv[cur][i]]=depth[cur]+1;
Dfs1(vv[cur][i],cur);
}
}
inline void Dfs2(int cur,int father)
{
int i;
for (i=0;i<int(vv[cur].size());i++)
if (vv[cur][i]!=father)
{
fa[vv[cur][i]]=cur;
depth[vv[cur][i]]=depth[cur]+1;
Dfs2(vv[cur][i],cur);
}
}
inline int calc_diameter()
{
depth[1]=1;Dfs1(1,-1);
int starter=1,cur,i,cnt=0,j,y;
for (i=2;i<=ind;i++) if (depth[i]>depth[starter]) starter=i;
depth[starter]=1;fa[starter]=0;Dfs2(starter,-1);
starter=1;
for (i=2;i<=ind;i++) if (depth[i]>depth[starter]) starter=i;
for (i=1;i<=ind;i++) isdia[i]=false;dtot=0;
for (cur=starter;cur;cur=fa[cur]) dlist[++dtot]=cur,isdia[cur]=true;
for (i=1;i<=ind;i++)
{
if (isdia[i]) {cnt++;continue;}
if (int(vv[i].size())==1) cnt++;
}
return ind-cnt;
}
int main ()
{
int i,j,x,y;
n=getint();e=getint();
for (i=1;i<=e;i++)
{
x=getint();y=getint();
v[x].pb(mp(y,i));v[y].pb(mp(x,i));
}
memset(isbridge,false,sizeof(isbridge));
memset(visited,false,sizeof(visited));
memset(col,0,sizeof(col));
int cnt=0;
for (i=1;i<=n;i++)
{
if (!dfn[i])
{
ind=0;vtot=0;cnt++;
tarjan(i,-1);
ind=0;
for (j=1;j<=vtot;j++)
if (!col[vlist[j]])
{
++ind;vv[ind].clear();
dfs(vlist[j],ind);
}
ans+=vtot-ind;
ans+=calc_diameter();
}
}
ans+=cnt-1;
printf("%d\n",ans);
return 0;
}