QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#820334#9881. Diverge and ConvergeYMH_fourteenWA 14ms26892kbC++142.3kb2024-12-18 21:02:242024-12-18 21:02:25

Judging History

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

  • [2024-12-18 21:02:25]
  • 评测
  • 测评结果:WA
  • 用时:14ms
  • 内存:26892kb
  • [2024-12-18 21:02:24]
  • 提交

answer

// Author: YE Minghan
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "templates/debug.h"
#define __int128 ll
#else
#define dbg(...) (void)0
#define msg(...) (void)0
#endif
#define ll long long
#define endl '\n'
#define PB emplace_back
#define PPB pop_back
#define MP make_pair
#define ALL(Name) Name.begin(),Name.end()
#define PII pair<int,int>
#define VI vector<int>
#define GI greater<int>
#define fi first
#define se second

const int N=1005;
int n;
map<PII,int>id1,id2;
VI g[N];
bool dfs(int x,int fa,int tg)
{
	if(x==tg)return 1;
	for(int y:g[x])
		if(y!=fa&&dfs(y,x,tg))return 1;
	return 0;
}

pair<vector<PII>,vector<PII>> sol(vector<PII> xe,vector<PII> ye)
{
	if(xe.empty())return {};
	VI deg(n+1);
	for(auto [u,v]:xe)deg[u]++,deg[v]++;
	for(auto [u,v]:ye)deg[u]++,deg[v]++;
	int x=0;deg[0]=4;
	for(int i=1;i<=n;i++)
		if(deg[i]&&deg[i]<deg[x])x=i;
	if(deg[x]==2)
	{
		PII ex,ey;vector<PII> nx,ny;
		for(auto [u,v]:xe)
			if(u==x||v==x)ex={u,v};
			else nx.PB(u,v);
		for(auto [u,v]:ye)
			if(u==x||v==x)ey={u,v};
			else ny.PB(u,v);
		tie(xe,ye)=sol(nx,ny);
		xe.PB(ex),ye.PB(ey);return {xe,ye};
	}
	else
	{
		VI ex,ey;vector<PII> nx,ny;
		for(auto [u,v]:xe)
			if(u==x||v==x)ex.PB(u+v-x);
			else nx.PB(u,v);
		for(auto [u,v]:ye)
			if(u==x||v==x)ey.PB(u+v-x);
			else ny.PB(u,v);
		bool swp=0;
		if(ey.size()==2)swap(ex,ey),swap(nx,ny),swp=1;
		int y=ex[0],z=ex[1],w=ey[0];
		nx.PB(y,z);
		tie(xe,ye)=sol(nx,ny);
		auto it=find(ALL(xe),MP(y,z));
		if(it==xe.end())it=find(ALL(xe),MP(z,y));
		int pos=it-xe.begin();
		int u=ye[pos].fi,v=ye[pos].se;
		for(int i=1;i<=n;i++)g[i].clear();
		for(auto [U,V]:xe)g[U].PB(V),g[V].PB(U);
		if(dfs(u,v,w)^dfs(u,v,z))swap(y,z);
		xe[pos]={x,y};
		xe.emplace(xe.begin()+pos+1,x,z);
		ye.emplace(ye.begin()+pos+1,x,w);
		if(swp)swap(xe,ye);
		return {xe,ye};
	}
}

int main()
{
	ios::sync_with_stdio(false),cin.tie(nullptr);
//	int _;cin>>_;while(_--)

	cin>>n;
	vector<PII> x,y;
	for(int i=1,u,v;i<n;i++)
	{
		cin>>u>>v;
		if(u>v)swap(u,v);
		x.PB(u,v),id1[MP(u,v)]=id1[MP(v,u)]=i;
	}
	for(int i=1,u,v;i<n;i++)
	{
		cin>>u>>v;
		if(u>v)swap(u,v);
		y.PB(u,v),id2[MP(u,v)]=id2[MP(v,u)]=i;
	}
	tie(x,y)=sol(x,y);
	for(auto i:x)cout<<id1[i]<<" ";cout<<endl;
	for(auto i:y)cout<<id2[i]<<" ";cout<<endl;

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3628kb

input:

4
1 2
2 3
3 4
1 2
2 4
2 3

output:

2 3 1 
3 2 1 

result:

ok Correct, N = 4

Test #2:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

2
1 2
2 1

output:

1 
1 

result:

ok Correct, N = 2

Test #3:

score: 0
Accepted
time: 0ms
memory: 3568kb

input:

7
1 2
1 3
2 4
2 5
3 6
3 7
1 5
1 6
1 7
5 2
6 3
7 4

output:

5 2 1 4 6 3 
5 2 1 4 3 6 

result:

ok Correct, N = 7

Test #4:

score: -100
Wrong Answer
time: 14ms
memory: 26892kb

input:

1000
780 67
364 281
934 245
121 472
460 233
574 534
91 687
91 832
413 613
815 638
519 196
992 120
150 157
198 365
643 700
279 698
623 215
578 330
869 333
874 570
879 697
897 671
962 670
108 137
779 9
988 91
919 314
696 722
431 270
810 692
769 49
254 915
737 580
229 888
379 612
19 161
787 346
280 753...

output:

192 104 722 918 951 240 592 189 585 661 359 292 681 427 607 985 567 58 266 825 195 599 208 46 618 328 524 824 923 512 728 594 255 830 976 712 159 334 336 57 332 470 278 459 191 692 903 188 813 705 974 995 550 383 948 912 63 319 967 216 551 305 682 816 745 970 20 888 966 477 372 789 282 246 902 636 6...

result:

wrong answer Wrong, N = 1000, not forming a tree