QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#508922#8029. Traveling MerchantFugiPigTL 0ms0kbC++143.4kb2024-08-07 21:56:552024-08-07 21:56:55

Judging History

你现在查看的是最新测评结果

  • [2024-08-07 21:56:55]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-08-07 21:56:55]
  • 提交

answer

#include<iostream>
#include<vector>
#define ls ch[x][0]
#define rs ch[x][1]
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
using namespace std;
const int maxn=2e5+5,inf=0x3f3f3f3f;
vector<int> G[maxn];
int mp[maxn],in,im;
int fdp[maxn];
int find(int x)
{
	return fdp[x]==x?x:fdp[x]=find(fdp[x]);
}
struct LinkCutTree
{
	int fa[maxn],ch[maxn][2],rev[maxn];
	bool isrt(int x)
	{
		x=find(x);
		int f=find(fa[x]);
		return ch[f][0]!=x&&ch[f][1]!=x;
	}
	int get(int x)
	{
		x=find(x);
		return ch[find(fa[x])][1]==x;
	}
	void reverse(int x)
	{
		x=find(x);
		rev[x]^=1;
		swap(ls,rs);
	}
	void pushdown(int x)
	{
		x=find(x);
		if(rev[x]){
			if(ls)reverse(ls);
			if(rs)reverse(rs);
			rev[x]=0;
		}
	}
	void update(int x)
	{
		x=find(x);
		if(!isrt(x))update(fa[x]);
		pushdown(x);
	}
	void rotate(int x)
	{
		x=find(x);
		int y=find(fa[x]),z=find(fa[y]),p=get(x);
		if(!isrt(y))ch[z][get(y)]=x;
		ch[y][p]=ch[x][p^1];
		fa[find(ch[x][p^1])]=y;
		ch[x][p^1]=y;
		fa[y]=x;
		fa[x]=z;
	}
	void splay(int x)
	{
		x=find(x);
		update(x);
		for(int f=find(fa[x]);!isrt(x);rotate(x),f=find(fa[x]))if(!isrt(f))rotate(get(x)==get(f)?f:x);
	}
	void access(int x)
	{
		for(int p=0;x;p=x,x=fa[x]){
			//cout<<x<<'+'<<p<<endl;
			x=find(x);
			splay(x);
			ch[x][1]=p;
			fa[p]=x;
		}
	}
	int findrt(int x)
	{
		x=find(x);
		access(x);
		splay(x);
//		rep(v1,1,in){
//			cout<<fa[v1]<<'-'<<ch[v1][0]<<'-'<<ch[v1][1]<<' ';
//		} 
//		cout<<endl;
//		print(x);
//		cout<<endl;
		pushdown(x);
		while(ls)x=find(ls),pushdown(x);
		splay(x);
		return x;
	}
	void makeroot(int x)
	{
		x=find(x);
		access(x);
		splay(x);
		reverse(x);
	}
	void print(int x)
	{
		pushdown(x);
		if(ls)print(ls);
		cout<<x<<' ';
		if(rs)print(rs);
	}
	void merge(int x,int rt)
	{
		x=find(x);
		fdp[x]=rt;
		pushdown(x);
		//cout<<x<<' '<<rt<<' '<<find(ls)<<' '<<rs<<endl;
		if(ls)merge(ls,rt);
		if(rs)merge(rs,rt);
	}
	void link(int x,int y)
	{
		x=find(x);
		y=find(y);
		if(findrt(x)==findrt(y)){
			makeroot(x);
			access(y);
			splay(y);
			int t=find(y);
			merge(y,t);
			rev[t]=ch[t][0]=ch[t][1]=0;
			//cout<<"zs"<<endl;
		}
		else{
			makeroot(x);
			splay(x);
			fa[x]=y;
		}
	}
}lct;
int main()
{
	//freopen("travel.in","r",stdin);
	//freopen("travel.out","w",stdout);
	int it;
	cin>>it;
	while(it--)
	{
		scanf("%d %d",&in,&im);
		rep(v1,1,in){
			G[v1].clear();
			fdp[v1]=v1;
			lct.ch[v1][0]=lct.ch[v1][1]=lct.fa[v1]=lct.rev[v1]=0;
		}
		char c=getchar();
		while(c<'A'||c>'Z')c=getchar();
		rep(v1,1,in){
			mp[v1]=(c=='H');
			c=getchar();
		}
		rep(v1,1,im){
			int ix,iy;
			scanf("%d %d",&ix,&iy);
			if(mp[ix]==mp[iy]){
				G[ix].push_back(iy);
				G[iy].push_back(ix);
			}
			else lct.link(ix,iy);
		}
		lct.makeroot(1);
		bool flag=false;
		rep(v1,1,in){
			//cout<<v1<<"-"<<lct.findrt(v1)<<endl;
			if(lct.findrt(v1)!=find(1))continue;
//			cout<<"zs"<<' '<<G[v1].size()<<endl;
			for(int v2=0;v2<G[v1].size();v2++){
				int to=G[v1][v2];
				if(lct.findrt(to)!=find(1))continue; 
				//cout<<v1<<"->"<<to<<' '<<lct.findrt(to)<<endl;
				lct.access(v1);
				lct.splay(v1);
				int t=find(to);
				while(!lct.isrt(t))t=find(lct.fa[find(t)]);
				if(find(t)==find(v1))
				{
					flag=true;
					break;
				}
				lct.splay(to);
			}
			if(flag)break;
		}
		if(flag)printf("yes\n");
		else printf("no\n");
		//if(!flag)break;
	}
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

2
4 4
LHLH
0 1
1 2
1 3
2 3
3 3
LHH
0 1
0 2
1 2

output:


result: