跳到正文

题解

题解:AT_abc469_d [ABC469D] The Big Two

luziyu_xingyandong约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年8月4日
收录
2026年11月8日
更新
2026年11月8日
篇幅
约 7 分钟

版本

  1. v1当前

题意理解:

存在数组(x,y)使得对于每一项(Ai,Bi)中,两个数组中至少有一个数是同时存在于两个数组中,问一共有多少个这样的(x,y)。

我们注意到,任取一组(Ai,Bi)(比如(A1,B1)),不难发现(x,y)中至少有一个数是A1或B1,因此讨论的数量就从n(n-1)/2降到了2n-3(A1n-1,B1n-1,再减去公共项),时间复杂度O(mn)。

由于mn<=4*10^10,所以我们不能把2n-3个(x,y)全部枚举出来于(Ai,Bi)比对,所以我们需要通过(Ai,Bi)的性质来排除不符合要求的(x,y)。以(A1,B1)为基准,我们可以发现(Ai,Bi)可以分为4类:(==A1,==B1),(==A1,!=B1),(!=A1,==B1),(!=A1,!=B1)(根据对排除(x,y)的不同影响方式分类),第一类对排除(x,y)并没有影响,第二三四类(Ai,Bi)每一个类别都有可能存在或不存在,对二、三、四是否存在类我们定义三个状态s1、s2、s3,接下来将对s1、s2、s3的2^3=8种情况进行分类讨论:

s1=0、s2=0、s3=0:即不存在不同于(A1,B1)之外的(Ai,Bi),答案就是2n-3,输出即可。

s1=1,s2=0,s3=0/s1=0,s2=1,s3=0:对于(==A1,!=B1)的(Ai,Bi)他只会影响与B1组合的(x,y)数量,固定ans=N-1(因为其他数字与A1组合成(x,y)共N-1组),对于状态为s1的所有(Ai,Bi),如果出现不同的另外一个数的数量为1,那么ans++((x,y)=(A1,另外一个数)),大于等于二就不加。对于(!=A1,==B1)也是如此。

s1=1,s2=1,s3=0:其实可以类比上面一种情况,只是不存在固定的ans=N-1,而是对(!=A1,==B1),(==A1,!=B1)同时判段,0<=ans<=2。

对于s3=1的剩下4种情况,逻辑就是先讨论上面情况总数,然后对所有(!=A1,!=B1)的(Ai,Bi)找到公共数字(0<=总数<=2),最后再取交集就可以。

不懂的可以看代码,时间复杂度O(M),16ms AC

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,a1,b1,a,b,sta[200005][4],sta_a[4];
int main()
{
   ios::sync_with_stdio(false);
   cin.tie(0);
   memset(sta_a,0,sizeof(sta_a)); 
   cin>>n>>m>>a1>>b1;
   for(int i=2;i<=m;i++){
   	cin>>a>>b;
   	if((a==a1 and b==b1)or(b==a1 and a==b1)) sta[i][1]=0;
   	if((a==a1 and b!=b1) or (b==a1 and a!=b1)){
   		sta[i][1]=1;sta_a[1]=1;
   		if(a==a1) sta[i][2]=b;
   		else sta[i][2]=a;
   	}
   	if((a==b1 and b!=a1) or (b==b1 and a!=a1)){
   		sta[i][1]=2;sta_a[2]=1;
   		if(a==b1) sta[i][2]=b;
   		else sta[i][2]=a;
   	}
   	if((a!=b1 and b!=a1) and (b!=b1 and a!=a1)){
   		sta[i][1]=3;sta_a[3]=1;
   		sta[i][2]=a;sta[i][3]=b;
   	}
   } 
   if(sta_a[1]==0 and sta_a[2]==0 and sta_a[3]==0) cout<<2*n-3;
   if(sta_a[1]==0 and sta_a[2]==0 and sta_a[3]==1){
   	int cnt=0,xn=0,i=2,ax,bx;
   	while(sta[i][1]!=3) i++;
   	ax=sta[i][2];bx=sta[i][3];
   	for(;i<=m;i++){
   		if(sta[i][1]==3){
   			if(ax!=sta[i][2] and ax!=sta[i][3]) ax=0;
   			if(bx!=sta[i][2] and bx!=sta[i][3]) bx=0;
   		}
   	}
   	cout<<2*((ax>0)+(bx>0));
   }
   if((sta_a[1]==1 and sta_a[2]==0 and sta_a[3]==0)or(sta_a[1]==0 and sta_a[2]==1 and sta_a[3]==0)){
   	int i=2,jn,nsta=sta_a[2]+1;
   	while(sta[i][1]!=nsta) i++;
   	jn=sta[i][2];
   	for(;i<=m;i++){
   		if(sta[i][1]==nsta and sta[i][2]!=jn)	{cout<<n-1;return 0;}
   	}
   	cout<<n;
   }
   if((sta_a[1]==1 and sta_a[2]==0 and sta_a[3]==1)or(sta_a[1]==0 and sta_a[2]==1 and sta_a[3]==1)){
   	int i=2,nsta=sta_a[2]+1,ax,bx,ans=0,jn;
   	while(sta[i][1]!=3) i++;
   	ax=sta[i][2];bx=sta[i][3];
   	for(;i<=m;i++){
   		if(sta[i][1]==3){
   			if(ax!=sta[i][2] and ax!=sta[i][3]) ax=0;
   			if(bx!=sta[i][2] and bx!=sta[i][3]) bx=0;
   		}
   	}
   	ans+=((ax>0)+(bx>0));
   	i=2;
   	while(sta[i][1]!=nsta) i++;
   	jn=sta[i][2];
   	for(;i<=m;i++){
   		if(sta[i][1]==nsta and sta[i][2]!=jn)	{cout<<ans;return 0;}
   	}
   	if(jn==ax or jn==bx) ans++;
   	cout<<ans;
   }
   if(sta_a[1]==1 and sta_a[2]==1 and sta_a[3]==0){
   	int ans=1,i,jn,add;
   	for(int j=1;j<=2;j++){
   		i=2;add=1;
   		while(sta[i][1]!=j) i++;
   		jn=sta[i][2];
   		for(;i<=m&&add>0;i++){
   			if(sta[i][1]==j and sta[i][2]!=jn) add=0;
   		}
   		ans+=add;	
   	}
   	cout<<ans;
   }
   if(sta_a[1]==1 and sta_a[2]==1 and sta_a[3]==1){
   	int i=2,nsta=sta_a[2]+1,ax,bx,ans=0,jn,add;
   	while(sta[i][1]!=3) i++;
   	ax=sta[i][2];bx=sta[i][3];
   	for(;i<=m;i++){
   		if(sta[i][1]==3){
   			if(ax!=sta[i][2] and ax!=sta[i][3]) ax=0;
   			if(bx!=sta[i][2] and bx!=sta[i][3]) bx=0;
   		}
   	}
   	for(int j=1;j<=2;j++){
   		i=2;add=1;
   		while(sta[i][1]!=j) i++;
   		jn=sta[i][2];
   		if(jn!=ax and jn!=bx) add=0;
   		for(;i<=m&&add>0;i++){
   			if(sta[i][1]==j and sta[i][2]!=jn) add=0;
   		}
   		ans+=add;
   	}
   	cout<<ans;
   }
   return 0;
} 
原文