F - Perfect Matching on a Tree
1.P3478 [POI2008] STA-Station2.E1. Weights Division (easy version)3.P5007 DDOSvoid 的疑惑4.P2168 [NOI2015] 荷马史诗5.P8306 【模板】字典树6.P1481 魔族密码7.P3128 [USACO15DEC] Max Flow P8.P3258 [JLOI2014] 松鼠的新家9.P5536 【XR-3】核心城市10.P5836 [USACO19DEC] Milk Visits S11.P3384 【模板】重链剖分/树链剖分12.P3038 [USACO11DEC] Grass Planting G13.P4551 最长异或路径14.F. Alex's whims15.J. 上学16.Game on Tree17.E. We Need More Bosses18.A. Party19.D. Strong Vertices20.B. Omkar and Heavenly Tree21.B. Mahmoud and Ehab and the bipartiteness22.P8655 [蓝桥杯 2017 国 B] 发现环23.P10298 [CCC 2024 S4] Painting Roads24.F. Gardening Friends25.D. Playoff Tournament26.E. Lomsat gelral27.F. Minimum Maximum Distance28.D. Book of Evil29.P2195 HXY造公园30.P3304 [SDOI2013] 直径31.D. Sasha and a Walk in the City32.D. Vitaly and Cycle33.P10838 『FLA - I』庭中有奇树
34.F - Perfect Matching on a Tree
分析
考虑两个点对
如果点对
此时两个点对的路径相交,且
所以,最后的答案一定是一条路径与其他所有路径都有交点
此时猜想,如果树上每条路径与其他路径都有交点,那么是否存在一个点,使得所有路径都经过这点?
验证:
假设存在三条绳子,每两条绳子相交,由于不存在环(树结构),所以他们必然有至少一个共同的交点
四五六条绳子同理
简化题意
我么已知最优答案下,所有路径必然有一个共同的点,因此我们可以遍历所有点,假定它是那个共同交点的情况下的答案
但是当我们取叶子节点时,我们发现不是所有点都能成为共同交点,因此我们试着找哪些点不能/能成为共同交点
我们把这个点作为树的根,如果有一个子树的节点个数大于
所以共同交点的要求是,不存在子树大小超过
实施
考虑任意两个点
我们对节点标记 dfs 序,dfs序差大于等于
code
#include<bits/stdc++.h>
using namespace std;
/*
#define int long long
#define double long double
#define lowbit(x) ((x)&(-x))
const int inf=1e18;
const int mod=1e9+7;
const int N=4e5;
int qpow(int a,int n)
{
int res=1;
while(n)
{
if(n&1) res=res*a%mod;
a=a*a%mod;
n>>=1;
}
return res;
}
int inv(int x)
{
return qpow(x,mod-2);
}
int fa[2000005];
int finds(int now){return now==fa[now]?now:finds(fa[now]);}
int dfn[200005],low[200005];
int cnt=0,num=0;
int in_st[200005]={0};
stack<int> st;
int belong[200005]={0};
void scc(int now,int fa)
{
dfn[now]=++cnt;
low[now]=dfn[now];
in_st[now]=1;
st.push(now);
for(auto next:G[now])
{
if(next==fa) continue;
if(!dfn[next])
{
scc(next,now);
low[now]=min(low[now],low[next]);
}
else if(in_st[next])
{
low[now]=min(low[now],dfn[next]);
}
}
if(low[now]==dfn[now])
{
int x;
num++;
do
{
x=st.top();
st.pop();
in_st[x]=0;
belong[x]=num;
}while(x!=now);
}
}
vector<int> prime;
bool mark[200005]={0};
void shai()
{
for(int i=2;i<=200000;i++)
{
if(!mark[i]) prime.push_back(i);
for(auto it:prime)
{
if(it*i>200000) break;
mark[it*i]=1;
if(it%i==0) break;
}
}
}
*/
vector<int> G[200005];
int sizes[200005]={0};
int sum[200005]={0};
int n;
int center=1,cmin;
void dfs1(int now,int fa)//统计最大子树大小,找交点
{
sizes[now]=1;
int tem=0;
for(auto next:G[now])
{
if(next==fa) continue;
dfs1(next,now);
sizes[now]+=sizes[next];
tem=max(tem,sizes[next]);
}
tem=max(tem,n-sizes[now]);
if(tem<cmin)
{
cmin=tem;
center=now;
}
}
int dfn[200005];
int cnt=0;
void dfs2(int now,int fa)
{
dfn[++cnt]=now;
for(auto next:G[now])
{
if(next==fa) continue;
dfs2(next,now);
}
}
void solve()
{
cin>>n;
cmin=n;
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
dfs1(1,1);
dfs2(center,center);
int half=n/2;
for(int i=1+n%2;i<=half+n%2;i++) cout<<dfn[i]<<' '<<dfn[i+half]<<'\n';
}
signed main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int TT=1;
//cin>>TT;
while(TT--) solve();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~