跳到正文

题解

题解 P4630 【[APIO2018] Duathlon 铁人两项】

s_r_f约 6 分钟
原文
文章信息与版本

文章信息

发布
2019年4月19日
收录
2026年3月24日
更新
2026年11月11日
篇幅
约 6 分钟
题目
P4630

版本

  1. v2当前
  2. v1

luoguluogu上的题解都太神仙了,我不太懂怎么容斥。。。。。

首先这道题要建出圆方树。

考虑枚举中转点cc,统计满足条件的(s,f)(s,f)的个数。

如果cc为圆点:

对于cc的每一个儿子xx,xx子树内的所有点作为ss都能和其他子树中的节点组成合法的(s,f)(s,f),对ansans的贡献为size[x]∗(size[c]−1−size[x]).size[x] * (size[c] - 1 - size[x]).

那么一个圆点作为cc的ansans就是Σsize[x]∗(size[c]−1−size[x])\Sigma size[x] * (size[c] - 1 - size[x])

看起来是O(n2)O(n^2)的,因为要枚举根来计算sizesize,但是我们可以通过一个dpdp,求出size2[x]size2[x]表示当xx为根时,(dfs树上的)fa[x]fa[x]的sizesize.

这个dpdp是O(n)O(n)的。

那么就可以O(n)O(n)统计所有圆点的ansans了。

再思考方点的贡献。

对于ss和ff都不在该点双内的(s,f)(s,f),它们到达这个点双内部之前,已经被这个点双里的一些点cc计算过了,所以我们不考虑。

那么方点计算的就是以这个点双中的某个点作为cc,(s,f)(s,f)中至少有一个点在点双内,且没有被圆点的ans统计到的答案。

求出这个点双中的圆点数,即方点的度数deg[x]deg[x].

如果(s,f)(s,f)中有一个点在点双内部,那么可以有(deg[x]−1)(deg[x]-1)个点双内的点作为cc.

但是(s,f)(s,f)在割点处被计算了一次,所以应该是(deg[x]−2).(deg[x] - 2).

如果(s,f)(s,f)都在点双内部,那么可行的cc显然有(deg[x]−2)(deg[x] - 2)种。

所以方点的答案为 Σ(deg[c]−2)∗size[x]∗(size[c]−size[x])\Sigma (deg[c] - 2) * size[x] * (size[c] - size[x])

这个也可以用类似上面的dpdp方法做到O(n)O(n).

所以这道题的复杂度为O(n+m).O(n+m).

注意图不联通。

代码:

C++
#include <bits/stdc++.h>
#define LL long long
using namespace std;
inline LL read(){
    LL x = 0,f = 1; char c = getchar();
    while (c != EOF && !isdigit(c)) {if (c == '-') f = -1;c = getchar();}
    while (isdigit(c)) {x = x * 10 + c - '0';c = getchar();}
    return x * f;
}
inline void write(LL x){
    if (x < 0) putchar('-'),x = -x;
    if (x > 9) write(x/10); putchar(x%10+'0');
}
inline void writeln(LL x){ write(x),putchar('\n'); }

const int N = 100005,M = 200005,E = M*4;
int To[E],Ne[E],He1[N],He2[N<<1],k;
inline void add(int *He,int x,int y){
	++k; To[k] = y,Ne[k] = He[x],He[x] = k;
	++k; To[k] = x,Ne[k] = He[y],He[y] = k;
}

int n,m,tot,deg[N<<1];
int dfn[N],low[N],st[N],top,Tim;
void tarjan(int x){
	dfn[x] = low[x] = ++Tim,st[++top] = x;
	for (int y,p = He1[x]; p ; p = Ne[p]){
		y = To[p];
		if (!dfn[y]){
			tarjan(y);
			low[x] = min(low[x],low[y]);
			if (low[y] >= dfn[x]){
				++tot; add(He2,tot,x); add(He2,tot,y);
				++deg[x],++deg[y],deg[tot] += 2;
				while (st[top] ^ y) add(He2,st[top],tot),++deg[tot],++deg[st[top]],--top;
				--top;
			}
		}
		else low[x] = min(low[x],dfn[y]);
	}
}

int size1[N<<1],size2[N<<1],fa[N<<1];
void dfs1(int x){
	size1[x] = x <= n ? 1 : 0;
	for (int p = He2[x]; p ; p = Ne[p]) if (To[p] ^ fa[x]) fa[To[p]] = x,dfs1(To[p]),size1[x] += size1[To[p]];
}
LL ans,ret;
void dfs2(int x){
	if (fa[x]) size2[x] = size2[fa[x]] + size1[fa[x]] - size1[x];
	for (int y,p = He2[x]; p ; p = Ne[p]) if ((y=To[p])^fa[x]) dfs2(To[p]);
	int sum = size2[x] + size1[x];
	ret = 0;
	if (x <= n){
		for (int y,p = He2[x]; p ; p = Ne[p]) if ((y=To[p])^fa[x]){
			ans += (LL)(sum - size1[y] - 1) * size1[y];
		}
		ans += (LL)(sum - size2[x] - 1) * size2[x];
	}
	else{
		for (int y,p = He2[x]; p ; p = Ne[p]) if ((y=To[p])^fa[x]){
			ans += (LL)(sum - size1[y]) * size1[y] * (deg[x] - 2);
		}
		ans += (LL)(sum - size2[x]) * size2[x] * (deg[x] - 2);
	}
}

int main(){
	int i,u,v;
	tot = n = read(),m = read();
	while (m--) u = read(),v = read(),add(He1,u,v);
	for (i = 1; i <= n; ++i) if (!dfn[i]) top = 0,tarjan(i);
	for (i = 1; i <= tot; ++i) if (!size1[i]) dfs1(i);
	for (i = 1; i <= tot; ++i) if (!size2[i]) dfs2(i);
	writeln(ans);
    return 0;
}
原文
存档评论17 条抓取于