跳到正文

题解

P14779 [COCI 2025/2026 #3] 宴会 / Domjenak

tianqishuaishuai约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年8月14日
收录
2026年8月19日
更新
2026年11月10日
篇幅
约 3 分钟

版本

  1. v1当前

2026-8-14

题目

模拟赛T3
挂分原因:

  1. 拓扑排序写错,没有打访问标记
  2. 用setset找匹配,时间太大。
code
C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
vector<int> b[N],g[N];
int q[N],p[N],pr[N],ln[N],dg[N];
bitset<N> T;
void init(int x){
	int y=p[x];
	g[x].emplace_back(y),T[x]=T[y]=1;
	for(int u: b[x]) if(u!=y){
		g[u].emplace_back(x);
		if(!T[u]) init(p[u]);
	}
	for(int u: b[y]) if(u!=x){
		g[y].emplace_back(u);
		if(!T[u]) init(u);
	}
}
void topo(int x){
	int &u=pr[x];
	for(int y: g[x]){
		if(!T[y]) topo(y);
		if(ln[y]>ln[u]) u=y;
	}ln[x]=ln[u]+1,T[x]=1;
}
int main(){
//	freopen("party.in","r",stdin);
//	freopen("party.out","w",stdout);
//	freopen("P14779_21.in","r",stdin);
	ios::sync_with_stdio(0),cin.tie(0);
	int n,m,z,tp=0;
	cin>>n>>m,z=n<<1;
	for(int x,y;m--;) cin>>x>>y,dg[x]++,dg[y]++,
		b[x].emplace_back(y),b[y].emplace_back(x);
	for(int i=1;i<=z;i++) if(dg[i]==1) q[++tp]=i;
	for(int j=1,x,y;j<=tp;j++){
		x=q[j];
		if(p[x]) continue;
		for(int u: b[x]) if(!p[u]){y=u;break;}
		p[x]=y,p[y]=x;
		for(int u: b[y]) if(!p[u]){
			dg[u]--;
			if(dg[u]==1) q[++tp]=u;
		}
	}
	for(int i=1;i<=z;i++) if(!T[i]) init(i);
	T=0;
	int x=0;
	for(int i=1;i<=z;i++){
		if(!T[i]) topo(i);
		if(ln[i]>ln[x]) x=i;
	}cout<<ln[x]<<"\n";
	for(;x;) cout<<x<<" ",x=pr[x];
}
原文