跳到正文

题解

题解:P12983 [GCJ 2022 Qualification] Twisty Little Passages

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

文章信息

发布
2026年7月1日
收录
2026年7月2日
更新
2026年11月11日
篇幅
约 3 分钟
获赞
1
题目
P12983

版本

  1. v1当前

一个显然的做法是:随机 T 操作 KK 次,用已知度数的平均值估算总度数。但这个估算值很容易被卡。

一个反例是菊花,即 n−1n-1 个点度数为 11,11 个点度数为 n−1n-1。

这题最重要的性质是:度数越大的点越容易被随机游走到达。

所以 TW 交替操作构造。

  1. A 类:被 T 但没有被 W 的点。这种点是大部分的,可以用平均值估计。
  2. B 类:被 T 且被 W 的点。这种点基本会被 W 到达,不影响剩下的 C 类点的估算。
  3. C 类:没有标记的点。这种点用 A 类算出的平均值估计即可。

当然 n≤k+1n \le k+1 的情况直接全 T 即可。

code
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
mt19937_64 Rand(time(0));
int n,k;
int deg[N];
int fs[N],fw[N];
int p[N],tot;
void solve(){
	cin>>n>>k;
	fill(deg+1,deg+1+n,-1);
	fill(fs+1,fs+1+n,0);
	fill(fw+1,fw+1+n,0);
	int u,d;
	cin>>u>>d;
	deg[u]=d;
	fs[u]=1;
	if(n<=k+1){
		for(int i=1;i<=n;i++){
			if(~deg[i])	continue;
			cout<<"T "<<i<<endl;
			cin>>u>>d;
			deg[u]=d;
		}
		int sum=0;
		for(int i=1;i<=n;i++)
			sum+=deg[i];
		cout<<"E "<<sum/2<<endl;
		return;
	}
	tot=1;
	for(int i=1;i<=n;i++)
		p[i]=i;
	shuffle(p+1,p+1+n,Rand);
	int st=0;
	while(st<k){
		if(st+2<=k){
			while(tot<=n&&~deg[p[tot]])	tot++;
			u=(tot<=n)?p[tot]:1;
			cout<<"T "<<u<<endl;
			cin>>u>>d;
			deg[u]=d;
			fs[u]=1;
			cout<<"W"<<endl;
			cin>>u>>d;
			deg[u]=d;
			fw[u]=1;
			st+=2;
		}
		else{
			while(tot<=n&&~deg[p[tot]])	tot++;
			u=(tot<=n)?p[tot]:1;
			cout<<"T "<<u<<endl;
			cin>>u>>d;
			deg[u]=d;
			fs[u]=1;
			st++;
		}
	}
	int sum=0,cnt=0,snw=0,cnw=0;
	for(int i=1;i<=n;i++)
		if(~deg[i]){
			sum+=deg[i];
			cnt++;
			if(fs[i]&!fw[i]){
				snw+=deg[i];
				cnw++;
			}
		}
	double ans=0;
	if(cnw)
		ans=1.0*snw/cnw;
	else	if(cnt)
		ans=1.0*sum/cnt;
	ans=sum+ans*(n-cnt);
	cout<<"E "<<(int)(ans/2.0)<<endl;
	return;
}
signed main(){
	int T;
	cin>>T;
	while(T--)
		solve();
	return 0;
}
原文