P1955&&ybtoj [Graph Theory] Chapter 1 and 2 Questions [Automatic Analysis of Programs]

Program automatic analysis

topic

Program automatic analysis


Parsing

Beginning: This is not a road extension field and set it to check
and then WA a
"colorful"
later found discrete and does not require an extension field, just deal with the same first and then deal with different, then they WA the (also with the RE After
adjusting for a while, I was too idle to add a merge by rank, and successfully avoided RE (what's the use of this)
and found that the array was getting smaller, but it didn’t work.
Eventually, I found that I made a mistake in discretization. AC finally .
Conclusion: I’m Konjac

code:

#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
    
    int a,y;}a[2000010];
int f[2000010],tot[2000010],z[1000010],t,n,s=0,T;//WA+1
bool ok;
bool cmp1(node x,node y){
    
    return x.a<y.a;}
bool cmp2(node x,node y){
    
    return x.y<y.y;}
int find(int dep)
{
    
    
	if(f[dep]==dep)return dep;
	return f[dep]=find(f[dep]);
}
void merge(int x,int y)
{
    
    
	if(!(x^y))return;
	if(tot[x]<tot[y])f[x]=y;
	else f[y]=x;
	if(tot[x]==tot[y])tot[x]++;//乱搞按秩合并
}
int main()
{
    
    
	scanf("%d",&T);
	while(T--)
	{
    
    
		scanf("%d",&n);
		n<<=1;
		for(int i=1;i<=n;i++)
		{
    
    
			scanf("%d",&a[i].a);
			a[i].y=i;
			if(!(i&1))scanf("%d",&z[i>>1]);//所以!i&1和!(i&1)是不同的……
		}
		sort(a+1,a+n+1,cmp1);
		t=0;
		for(int i=1;i<=n;i++){
    
    if(a[i].a!=t)t=a[i].a,a[i].a=a[i-1].a+1;else a[i].a=a[i-1].a;}
		//打挂的离散化上没有else一句
		for(int i=1;i<=a[n].a;i++)f[i]=i,tot[i]=1;
		sort(a+1,a+n+1,cmp2);
		n>>=1;
		for(int i=1;i<=n;i++)if(z[i])merge(find(a[(i<<1)-1].a),find(a[i<<1].a));
		for(int i=1;i<=n;i++)if((!z[i])&&(find(a[i<<1].a)==find(a[(i<<1)-1].a))){
    
    ok=1;break;}
		if(!ok)printf("YES\n");
		else printf("NO\n"),ok=0;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/zhanglili1597895/article/details/113095208