loading

P10441 [JOISC 2024] 乒乓球 (Day4)

题意

给定 \(n,m\),要求构造一张竞赛图使得其包含 \(n\) 个点且恰好有 \(m\) 个三元环。

\(n\le 5000,m\le \binom{n}{3}\)

分析

前置知识——根据度数序列构造竞赛图:先找到一个出度最小的点,然后将其随便找点连向它,剩下的点被它连。如果构造不出来说明不符合兰道定理,无解。复杂度 \(O(n^2)\)


一个结论是:竞赛图三元环个数为 \(\binom{n}{3}-\sum_{i=1}^n \binom{deg_i}{2}\),其中 \(deg_i\) 是出度,证明显然。

考虑调整法,先把三元环个数弄成极限情况,即环数最大的情况,根据不等式知识可知当 \(deg_i\) 分布最平均时三元环个数最大。如果 \(m\) 大于这个最大值,那么就无解;下面通过构造证明其他情况一定有解:

考虑转换一条边带来的后果:假设原来 \(x\rightarrow y\),设 \(deg_x,deg_y\),原方案数为 \(\frac{(deg_x-1)deg_x}{2}+\frac{(deg_y-1)deg_y}{2}\),转换后新出边为 \(deg_x-1,deg_y+1\),方案数差值为 \(\frac{((deg_x-2)-deg_x)(deg_x-1)}{2}+\frac{((deg_y+1)-(deg_y-1))deg_y}{2}=deg_y-deg_x+1\)。不难发现当 \(deg_x=deg_y\) 时方案数只会变化 1,并且无法操作下去的时候对应的三元环数量为 0(此时的 \(deg=\{0,1,2,\cdots,n-1\}\),不难发现缩点后形成一条 \(n\) 元链)。

用 queue+vector 维护同一个 \(deg\) 的点数和点数 \(\ge 2\)\(deg\) 编号,设 \(f_n\) 表示 \(n\) 个点的竞赛图的最大三元环数量,可以做到 \(O(f_n)=O(n^3)\)

考虑在 \(f_n\ge m\) 的前提下往下压 \(f_n\)。注意到 \(f_n-f_{n-1}=O(n^2)\),所以找到第一个 \(f_p\ge m\)\(p\),对大小为 \(p\) 的图构造即可。复杂度 \(O(n^2)\)

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0)
#define OTIE cout.tie(0)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define il inline
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define popc __builtin_popcountll 
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
	int qwqx=0,qwqf=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
	if(qwqx<0){qwqx=-qwqx;putchar('-');}
	int qwqy=0;static char qwqz[40];
	while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
	while(qwqy--){putchar(qwqz[qwqy]);}if(ch)putchar(ch);
}
bool Mbg;
const int mod=998244353;
template<typename T1,typename T2>inline void adder(T1 &x,T2 y){x+=y,x=x>=mod?x-mod:x;}
template<typename T1,typename T2>inline void suber(T1 &x,T2 y){x-=y,x=x<0?x+mod:x;}
const int maxn=5e3+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,m;
char s[maxn][maxn];
int deg[maxn];
bool vis[maxn];
int calc(int k){
    int d=(k*(k-1)/2)/k,r=(k*(k-1)/2)%k;
    rep(i,1,r)deg[i]=d+1;
    rep(i,r+1,k)deg[i]=d;
    int res=k*(k-1)*(k-2)/6;
    rep(i,1,k)res-=deg[i]*(deg[i]-1)/2;
    return res;
}
il void add(int x,int y){
    s[x][y]='1',s[y][x]='0',deg[x]--;
}
void construct(int p){
    rep(i,1,p)vis[i]=0;
    rep(_,1,p-1){
        int ps=-1,mn=inf;
        rep(i,1,p)if(!vis[i]&&deg[i]<mn)mn=deg[i],ps=i;
        vis[ps]=1;
        rep(i,1,p)if(!vis[i]){
            if(deg[ps])add(ps,i);
            else add(i,ps);
        }
    }
}
vector<int>vec[maxn];
inline void solve_the_problem(){
	n=rd(),m=rd();
    if(m>calc(n))return (void)PN;
    int p;
    for(p=1;p<=n;++p)if(calc(p)>=m)break;
    construct(p);
    m=calc(p)-m;
    rep(i,1,p)vec[i].clear();
    rep(i,1,p)vec[deg[i]].pb(i);
    rep(i,1,p)vis[i]=0;
    queue<int>q;
    rep(i,1,p)if(vec[i].size()>=2)vis[i]=1,q.push(i);
    for(;m;--m){
        assert(!q.empty());
        int d=q.front();
        int x=vec[d].back();vec[d].pop_back();
        int y=vec[d].back();vec[d].pop_back();
        if(s[x][y]=='0')swap(x,y);
        swap(s[x][y],s[y][x]),deg[x]--,deg[y]++;
        vec[d-1].pb(x),vec[d+1].pb(y);
        if(vec[d].size()<2)q.pop(),vis[d]=0;
        if(vec[d-1].size()>=2&&!vis[d-1])vis[d-1]=1,q.push(d-1);
        if(vec[d+1].size()>=2&&!vis[d+1])vis[d+1]=1,q.push(d+1);
    }
    rep(i,p+1,n)rep(j,1,i-1)add(i,j);
    PY;
    rep(i,2,n){
        rep(j,1,i-1)pc(s[i][j]);
        P__;
    }
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=rd();
	while(_--)solve_the_problem();
}
/*
g++ p10441.cpp -o a -std=c++14 -O2 -Wall -Wextra -Wshadow -lm
3
3 1
4 4
5 3
*/
posted @ 2025-07-04 16:55  dcytrl  阅读(20)  评论(3)    收藏  举报