QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#824798#9771. Guessing Gameucup-team3586#WA 289ms48944kbC++234.3kb2024-12-21 15:53:072024-12-21 15:53:10

Judging History

This is the latest submission verdict.

  • [2024-12-21 15:53:10]
  • Judged
  • Verdict: WA
  • Time: 289ms
  • Memory: 48944kb
  • [2024-12-21 15:53:07]
  • Submitted

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
const int M=100000;
namespace dsu1
{
	int fa[200200];
	inline int anc(int x)
	{
		while(fa[x]!=x) x=fa[x]=fa[fa[x]];
		return x;
	}
	void clear()
	{
		for(int i=1;i<=M+M;i++)
			fa[i]=i;
	}
}
namespace dsu2
{
	int fa[200200];
	inline int anc(int x)
	{
		while(fa[x]!=x) x=fa[x]=fa[fa[x]];
		return x;
	}
	void clear()
	{
		for(int i=1;i<=M+M;i++)
			fa[i]=i;
	}
}
vector<int> G[200200];
int fa[200200],depth[200200];
int a[1001000],b[1001000];
int rt[200200];
int anc[19][200200];
set<int> canda,canda2;
set<int> candb,candb2;
int U[200200],V[200200],D[200200];
inline int lca(int u,int v)
{
	for(int i=18;i>=0;i--)
	{
		if(depth[anc[i][u]]>=depth[v]) u=anc[i][u];
		if(depth[anc[i][v]]>=depth[u]) v=anc[i][v];
	}
	if(u==v) return u;
	for(int i=18;i>=0;i--)
		if(anc[i][u]!=anc[i][v])
		{
			u=anc[i][u];
			v=anc[i][v];
		}
	return anc[0][u];
}
inline int get(int u,int v)
{
	int w=lca(u,v);
	if(u==w)
	{
		int diff=depth[v]-depth[u]-1;
		for(int i=0;i<19;i++)
			if(diff>>i&1)
				v=anc[i][v];
		return v;
	}
	else return fa[u];
}
inline int dist(int a,int b)
{
	int c=lca(a,b);
	return depth[a]+depth[b]-depth[c]*2;
}
void comb(int x)
{
	int Rt=dsu1::anc(x);
	int dest=rt[Rt];
	while(dsu2::anc(x)!=dest)
	{
		if(x<=M) canda2.insert(x);
		else candb2.insert(x);
		dsu2::fa[x]=dest;
		x=get(x,dest);
	}
}
int vis[200200];
void dfs(int u,int fa)
{
	vis[u]=1;
	::fa[u]=fa;
	depth[u]=depth[fa]+1;
	for(auto v:G[u])
		if(v!=fa)
			dfs(v,u);
}
int tA,tB;
void calc(int x,int v)
{
	int s1=(U[x]<=M);
	int s2=(V[x]<=M);
	if(s1==s2)
	{
		int len=D[x]/2;
		s1^=(len&1);
		if(s1) tA+=v;
		else tB+=v;
	}
	else
	{
		s1=0;
		int len=D[x]/2;
		s1^=(len&1);
		if(s1) tA+=v;
		else tB+=v;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int q;
	cin>>q;
	dsu1::clear();
	dsu2::clear();
	for(int i=1;i<=M+M;i++)
	{
		U[i]=V[i]=i;
		D[i]=0;
	}
	for(int i=1;i<=q;i++)
	{
		cin>>a[i]>>b[i];
		b[i]+=M;
		int x=dsu1::anc(a[i]);
		int y=dsu1::anc(b[i]);
		if(x!=y)
		{
			G[a[i]].pb(b[i]);
			G[b[i]].pb(a[i]);
			dsu1::fa[x]=y;
		}
	}
	dsu1::clear();
	for(int i=1;i<=M+M;i++)
		if(!vis[i])
			dfs(i,0);
	for(int i=1;i<=M+M;i++)
		anc[0][i]=fa[i];
	for(int i=1;i<19;i++)
		for(int j=1;j<=M+M;j++)
			anc[i][j]=anc[i-1][anc[i-1][j]];
	for(int i=1;i<=q;i++)
	{
		canda.insert(a[i]);
		candb.insert(b[i]);
		int x=dsu1::anc(a[i]);
		int y=dsu1::anc(b[i]);
		if(x==y)
		{
			if(!rt[x])
			{
				calc(x,1);
				int u=a[i];
				int v=b[i];
				while(u!=v)
				{
					if(depth[u]<depth[v]) swap(u,v);
					if(u<=M) canda2.insert(u);
					else candb2.insert(u);
					dsu1::fa[u]=a[i];
					u=fa[u];
				}
				if(u<=M) canda2.insert(u);
				else candb2.insert(u);
				dsu1::fa[u]=a[i];
				rt[x]=a[i];
			}
			else
			{
				comb(a[i]);
				comb(b[i]);
			}
		}
		else
		{
			if(!rt[x])
				calc(x,1);
			if(!rt[y])
				calc(y,1);
			if(!rt[x]&&!rt[y])
			{
				int nU=U[y];
				int nV=V[y];
				if(D[x]>D[y])
				{
					D[y]=D[x];
					nU=U[x];
					nV=V[x];
				}
				auto upd=[&](int U2,int V2)
				{
					int D2=dist(U2,V2);
					if(D2>D[y])
					{
						D[y]=D2;
						nU=U2;
						nV=V2;
					}
				};
				upd(U[x],U[y]);
				upd(U[x],V[y]);
				upd(V[x],U[y]);
				upd(V[x],V[y]);
				U[y]=nU;
				V[y]=nV;
				dsu1::fa[x]=y;
				calc(y,-1);
			}
			else if(!rt[x])
				dsu1::fa[x]=y;
			else if(!rt[y])
			{
				dsu1::fa[x]=y;
				rt[y]=rt[x];
				rt[x]=0;
			}
			else
			{
				dsu1::fa[x]=y;
				comb(rt[x]);
				rt[x]=0;
			}
		}
		cout<<tA-sz(canda2)<<" "<<tB-sz(candb2)<<'\n';
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 30540kb

input:

4
1 1
1 2
2 1
2 2

output:

1 0
0 2
1 2
0 0

result:

ok 8 numbers

Test #2:

score: -100
Wrong Answer
time: 289ms
memory: 48944kb

input:

250000
49324 49323
44443 44445
92513 92513
69591 69591
52085 52082
95024 95025
21004 21005
34373 34371
60771 60772
17131 17134
34885 34882
6011 6015
56103 56105
21055 21054
71592 71593
14894 14895
25774 25771
96225 96224
16444 16442
48432 48432
86954 86952
7202 7202
38661 38665
20063 20063
85383 853...

output:

1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 0
25 0
26 0
27 0
28 0
29 0
30 0
31 0
32 0
33 0
34 0
35 0
36 0
37 0
38 0
39 0
40 0
41 0
42 0
43 0
44 0
45 0
46 0
47 0
48 0
49 0
50 0
51 0
52 0
53 0
54 0
55 0
56 0
57 0
58 0
59 0
60 0
61 0
62 0...

result:

wrong answer 19251st numbers differ - expected: '8293', found: '8294'