这依托为啥T啊[蒟蒻疑问]
查看原帖
这依托为啥T啊[蒟蒻疑问]
989997
DGL__DGL楼主2024/9/10 08:15
#include<bits/stdc++.h>
using namespace std;
int t,n,m;
int h[25514],e[214514],ne[214514],idx;
int c[25514];
int q[1145140],l,r;
int res[5],cnt,maxx;

inline void add(int a,int b)
{
	idx++;
	ne[idx]=h[a];
	e[idx]=b;
	h[a]=idx;
}

inline int op(int x)
{
	maxx=114514;
	for(int i=h[x];i;i=ne[i])
	{
		int y=e[i];
		res[c[y]]++;
	}
	for(int i=1;i<=4;i++)
	{
		if(maxx>res[i])
	  {
	  	maxx=res[i];
	  	cnt=i;
		}
		res[i]=0;
	}
	  
	return cnt;	
}

inline bool check(int x)
{
	int a=0;
	for(int i=h[x];i;i=ne[i])
	{
		int y=e[i];
		if(c[y]==c[x])
		  a++;
		if(a>=2)
		  return false;	  
	}
	return true;
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0);
	cin>>t;
	while(t--)
	{
		idx=0;
		l=1;
		r=0;
		memset(h,0,sizeof h);
		memset(c,0,sizeof c);
		cin>>n>>m;
		while(m--)
		{
			int a,b;
			cin>>a>>b;
			add(a,b);
			add(b,a);
		}
		
		for(int i=1;i<=n;i++)
		  c[i]=i%4+1;
		
		for(int i=1;i<=n;i++)
		  if(!check(i))
		    q[++r]=i;
		
		while(l<=r)
		{
			int x=q[l];
			l++;
			x=op(x);
			
			for(int i=h[x];i;i=ne[i])
			{
				int y=e[i];
				if(!check(y))
			    q[++r]=y;
			}
			  
		} 
		
		for(int i=1;i<=n;i++)
		  if(c[i]==1)
		    cout<<"a";
		  else if(c[i]==2) 
			  cout<<"b";
			else if(c[i]==3)
			  cout<<"c";
			else 
			  cout<<"d";		  
		cout<<'\n';	   
	}
	
	
	return 0;
}


2024/9/10 08:15
加载中...