跳到正文

题解

题解:AT_agc033_f [AGC033F] Adding Edges

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

文章信息

发布
2026年8月25日
收录
2026年8月31日
更新
2026年11月16日
篇幅
约 6 分钟

版本

  1. v1当前

图上构造好题。

我们不妨设原图为 G0G_0 ,最后生成的图为 GG。

我们先考虑 a,b,ca,b,c 在树上就按照这个顺序出现。那么一条边 (u,v)∈G(u,v)\in G ,当且仅当存在 u=v1,v2,v3…vk−1,vk=vu=v_1,v_2,v_3\dots v_{k-1},v_k=v 使得对于每一个 i∈[1,k−1]i\in[1,k-1] ,都满足 (vi,vi+1)∈G0(v_i,v_{i+1})\in G_0 且 v1,v2,…vkv_1,v_2,\dots v_k 在树上的某一条链上顺序出现。

我们现在考虑将原图改成这个形式,对于树上某条简单路径上顺序出现的三个点 a,b,ca,b,c ,若 (a,b),(a,c)∈G0(a,b),(a,c)\in G_0 则我们可以将边 (a,c)(a,c) 替换为 (b,c)(b,c) 使答案不变。

现在考虑快速处理这个过程。考虑依次加入 G0G_0 中的边,并记 f(x,y)f(x,y) 为树的根为 xx 时当前生成的图 GG 中与 yy 有连边的与 xx 在该树上的祖先。则加入边 (x,y)(x,y) 时如下更新 f(x,y)f(x,y) :

  1. 当 f(x,y)=yf(x,y)=y 时,显然不需要加入这条边。
  2. 当 f(x,y)f(x,y) 有值且不为 yy 时,我们需要将 (f(x,y),y)(f(x,y),y) 加入待加入边的队列。
  3. 当 f(x,y)f(x,y) 没有值的时候,我们先加入边 (x,y)(x,y) 和边 (y,x)(y,x) (及更新 f(x,y)f(x,y) 和 f(y,x)f(y,x) 的值),然后更新以 xx 为根, yy 子树内的 f(x,u)f(x,u) 为 yy 。

具体更新方法是如果 f(x,u)f(x,u) 有值,那么将 (y,u)(y,u) 加入待加入边的队列。否则将 f(x,u)f(x,u) 更新为 yy 。注意反过来对于 y,xy,x 也要跑一遍相同的算法,因为我们需要的是无向图。

最后统计答案时就是分别以每个节点为树的根跑 dfs 统计答案,注意最后答案要除以二,因为我们会正反统计两边。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define il inline
struct IO
{
	static const int Size=(1<<21);
	char buf[Size],*p1,*p2;
	int st[105],Top;
	~IO(){clear();}
	il void clear(){fwrite(buf,1,Top,stdout);Top=0;}
	il char gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,Size,stdin),p1==p2)?EOF:*p1++;}
	il void pc(const char c){Top==Size&&(clear(),0);buf[Top++]=c;}
	il IO& operator >>(char& c){while(c=gc(),c==' ' || c=='\n' || c=='\r');return *this;}
	template<typename T>il IO& operator >>(T& x)
	{
		x=0;bool f=0;char c=gc();
		while(!isdigit(c)){if(c=='-') f=1;c=gc();}
		while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=gc();}
		f?x=-x:0;
		return *this;
	}
	il IO& operator >>(string& s)
	{
		s="";char c=gc();
		while(c==' ' || c=='\n' || c=='\r') c=gc();
		while(c!=' ' && c!='\n' && c!='\r' && c!=EOF) s+=c,c=gc();
		return *this;
	}
	il IO& operator <<(const char c){pc(c);return *this;}
	template<typename T> il IO& operator <<(T x)
	{
		if(x<0) pc('-'),x=-x;
		do st[++st[0]]=x%10,x/=10;while(x);
		while(st[0]) pc(st[st[0]--]+'0');
		return *this;
	}
	il IO& operator <<(const string s){for(auto c:s) pc(c);return *this;}
	il IO& operator <<(const char* c){for(int i=0;c[i];i++) pc(c[i]);return *this;}
} fin,fout;
const int N=2010;
int f[N][N],p[N][N],ans;
vector<int> g[N];
il void get_fa(int u,int* fa)
{
	for(int v:g[u])
	{
		if(v==fa[u]) continue;
		fa[v]=u;
		get_fa(v,fa);
	}
}
queue<int> qx,qy;
il void dfs(int u,int rt,int t)
{
	if(u!=t && p[rt][u])
	{
		qx.push(u),qy.push(t);
		return;
	}
	p[rt][u]=t;
	for(int v:g[u])
	{
		if(v==f[rt][u]) continue;
		dfs(v,rt,t);
	}
}
il void add(int u,int v)
{
	qx.push(u),qy.push(v);
	while(!qx.empty())
	{
		int x=qx.front(),y=qy.front();
		qx.pop(),qy.pop();
		if(p[x][y]==y) continue;
		if(p[x][y])
		{
			qx.push(p[x][y]),qy.push(y);
			continue;
		}
		if(p[y][x])
		{
			qx.push(p[y][x]),qy.push(x);
			continue;
		}
		p[x][y]=y,p[y][x]=x;
		dfs(y,x,y);dfs(x,y,x);
	}
}
il void get_ans(int u,int rt,int t)
{
	if(t!=u && p[t][u])
	{
		ans++;t=u;
	}
	for(int v:g[u])
	{
		if(v==f[rt][u]) continue;
		get_ans(v,rt,t);
	}
}
signed main()
{
	int n,m;fin>>n>>m;
	for(int i=1;i<n;i++)
	{
		int u,v;fin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++) get_fa(i,f[i]);
	for(int i=1;i<=m;i++)
	{
		int u,v;fin>>u>>v;add(u,v);
	}
	for(int i=1;i<=n;i++) get_ans(i,i,i);
	fout<<ans/2;
	return 0;
}
原文