TLE 85pts 求调
查看原帖
TLE 85pts 求调
682739
CuteMurasame楼主2024/12/1 21:03
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i(a);i<=(b);++i)
#define req(i,a,b) for(int i(a);i>=(b);--i)
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf,ubuf[1<<23],*u=ubuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline void read(int &x)
{
	x=0;
	char ch=getchar();
	while(ch<48||ch>57) ch=getchar();
	while(48<=ch&&ch<=57) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
}
struct node
{
	int st,ed,col,pos1,pos2;
} a[100001];
int n,m,q,x,y,k,q1[50001],q2[50001];
int ts1,ts2,vis1[50001],vis2[50001];
signed main()
{
	read(n),read(m),read(q);
	vector<vector<int>> g1(n+1,vector<int>()),g2(n+1,vector<int>());
	rep(i,1,m)
	{
		read(a[i].st),read(a[i].ed),a[i].col=1;
		g1[a[i].st].emplace_back(i);
		a[i].pos1=g1[a[i].st].size()-1;
		g2[a[i].ed].emplace_back(i);
		a[i].pos2=g2[a[i].ed].size()-1;
	}
	while(q--)
	{
		int opt; read(opt);
		if(opt==1)
		{
			read(k);
			if(a[k].col)
			{
				int x=a[k].st,y=a[k].ed;
				int posx=a[k].pos1,posy=a[k].pos2;
				int lstx=g1[x].back(),lsty=g2[y].back();
				g1[x][posx]=lstx,g1[x].pop_back();
				g2[y][posy]=lsty,g2[y].pop_back();
				a[lstx].pos1=posx,a[lsty].pos2=posy;
				a[k].col=0;
			}
			else
			{
				int x=a[k].st,y=a[k].ed;
				g1[x].emplace_back(k),a[k].pos1=g1[x].size()-1;
				g2[y].emplace_back(k),a[k].pos2=g2[y].size()-1;
				a[k].col=1;
			}
		}
		else
		{
			read(x),read(y);
			if(x==y) {puts("YES"); continue;}
			++ts1,++ts2;
			int found=0;
			int head1=0,tail1=0,head2=0,tail2=0;
			q1[tail1++]=x,q2[tail2++]=y;
			vis1[x]=ts1,vis2[y]=ts2;
			while(head1<tail1&&head2<tail2&&!found)
			{
				if(tail1-head1<=tail2-head2)
				{
					int u=q1[head1++];
					for(auto v:g1[u])
					{
						if(vis1[a[v].ed]==ts1) continue;
						vis1[a[v].ed]=ts1;
						q1[tail1++]=a[v].ed;
						if(vis2[a[v].ed]==ts2) {found=1; break;}
					}
				}
				else
				{
					int u=q2[head2++];
					for(auto v:g2[u])
					{
						if(vis2[a[v].st]==ts2) continue;
						vis2[a[v].st]=ts2;
						q2[tail2++]=a[v].st;
						if(vis1[a[v].st]==ts1) {found=1; break;}
					}
				}
			}
			if(found) puts("YES");
			else puts("NO");
		}
	}
	return 0;
}
2024/12/1 21:03
加载中...