链接:https://loj.ac/problem/2587
以前想过一次,觉得比较简单没有写。。于是就留下的机会给今天填坑
做某题的时候想到,这不就是个圆方树嘛
然后忽然一想,圆方树是什么来着,怎么写来着???我到底会不会啊
一度十分尴尬。。于是先来做这个。。
先把圆方树建出来
那么,假设确定一个起点和终点,那么他对答案的贡献就是路径上所有的点以及路径旁边的点的和
当然,这里的点指有效点,也就是原点
考虑怎么计算这个东西
我们可以把方点的权值看做是度数
那么可以变成是路径和
但是有一点要注意的,就是在这条路径上的方点度数要-2
因为前后的原点在这条路径上贡献答案了
然后计算每个点的贡献就好了,也就是枚举每个点是c点时的答案
当然,计算贡献的时候,我们枚举的点是c点,是不可以当做s和f的,这个要注意
有一种高妙做法,是把原点的权值看做-1,但是我并不是很懂这样算出来为什么是对的。。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;
const LL N=200005;
LL n,m;
struct qq
{
LL x,y,last;
}e[N*2];LL num,last[N];
void init (LL x,LL y)
{
num++;e[num].x=x;e[num].y=y;
e[num].last=last[x];
last[x]=num;
}
vector<int> vec[N];
LL dfn[N],low[N],id;
LL sta[N],top,cnt;
LL TOT;//有多少个有用的点
LL d[N];
void dfs (LL x)
{
TOT++;
top++;sta[top]=x;
low[x]=dfn[x]=++id;
for (LL u=last[x];u!=-1;u=e[u].last)
{
LL y=e[u].y;
if (dfn[y]==0)
{
dfs(y);
low[x]=min(low[x],low[y]);
if (low[y]>=dfn[x])
{
cnt++;LL i;
do
{
d[cnt]++;
i=sta[top--];
// printf("%lld %lld %lld\n",cnt,i,cnt);
vec[cnt].push_back(i);
vec[i].push_back(cnt);
}while (i!=y);
d[cnt]++;
// printf("%lld %lld %lld\n",cnt,x,cnt);
vec[x].push_back(cnt);
vec[cnt].push_back(x);
}
}
else low[x]=min(low[x],dfn[y]);
}
}
LL tot[N];
LL ans;
void calc (LL x,LL fa)
{
LL now=0;
tot[x]=(x<=n);
LL siz=vec[x].size();
for (LL u=0;u<siz;u++)
{
LL y=vec[x][u];
if (y!=fa)
{
calc(y,x);
// printf("%lld %lld %lld %lld\n",x,y,tot[y],now);
if (x<=n) ans=ans+tot[y]*now;
else ans=ans+tot[y]*now*(d[x]-2);
now=now+tot[y];tot[x]=tot[x]+tot[y];
}
}
if (fa!=0)
{
if (x<=n) ans=ans+now*(TOT-tot[x]);
else ans=ans+now*(TOT-tot[x])*(d[x]-2);
}
}
int main()
{
num=0;memset(last,-1,sizeof(last));
scanf("%lld%lld",&n,&m);cnt=n;
for (LL u=1;u<=m;u++)
{
LL x,y;
scanf("%lld%lld",&x,&y);
init(x,y);init(y,x);
}
for (LL u=1;u<=n;u++)
if (dfn[u]==0)
{
TOT=0;
dfs(u);
calc(u,0);
}
printf("%lld\n",ans*2);
return 0;
}