QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#886000#10000. Add, Remove, TransformNaganohara_YoimiyaRE 12ms5120kbC++206.6kb2025-02-06 20:01:162025-02-06 20:01:16

Judging History

This is the latest submission verdict.

  • [2025-02-06 20:01:16]
  • Judged
  • Verdict: RE
  • Time: 12ms
  • Memory: 5120kb
  • [2025-02-06 20:01:16]
  • Submitted

answer

#include<bits/stdc++.h>

#define ll long long
#define mk make_pair
#define fi first
#define se second

using namespace std;

#define gc getchar
inline int read(){
	int x=0,f=1;char c=getchar();
	for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
	for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
	return x*f;
}

template<typename T>void cmax(T &x,T v){x=max(x,v);}
template<typename T>void cmin(T &x,T v){x=min(x,v);}

vector<pair<int,int> >getTree(vector<int>P){ // prufer sequence -> tree
	int n=P.size()+2;
	vector<int>deg(n,1);for(int x:P)deg[x]++;

	set<int>Leaf;vector<pair<int,int> >edges;
	for(int i=0;i<n;i++)if(deg[i]==1)Leaf.insert(i);
	for(int i=0;i<n-2;i++){
		int x=*Leaf.begin();Leaf.erase(Leaf.begin());
		int y=P[i];edges.emplace_back(mk(y,x));
		if(--deg[y]==1)Leaf.insert(y);
	}
	edges.emplace_back(mk(*Leaf.begin(),*--Leaf.end()));
	return edges;
}

void A(int x,int y,vector<vector<int> >&G){G[x][y]^=1,G[y][x]^=1;}
void oper(int a,int b,int c,int d,vector<vector<int> >&G){
	assert(G[a][b]&&G[b][c]&&G[c][d]&&a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d);
	A(a,b,G),A(b,c,G),A(c,d,G),A(c,a,G),A(a,d,G),A(d,b,G);
}

const int N=1305; // 6^4 = 1296
map<vector<vector<int> >,int>ID;
vector<vector<int> >Gs[N];
int pre[N];
struct OP{int a,b,c,d;OP(int A=0,int B=0,int C=0,int D=0):a(A),b(B),c(C),d(D){}};
OP rec[N];
vector<pair<int,OP> >to[N];

OP rev(OP x){return OP(x.c,x.a,x.d,x.b);}

void prework(){
	int n=6,ncnt=0;
	vector<int>P(n-2);
	for(P[0]=0;P[0]<n;P[0]++)for(P[1]=0;P[1]<n;P[1]++)for(P[2]=0;P[2]<n;P[2]++)for(P[3]=0;P[3]<n;P[3]++){
		auto E=getTree(P);++ncnt;
		vector<vector<int> >G(n,vector<int>(n));
		for(auto [u,v]:E)G[u][v]=G[v][u]=1;
		ID[G]=ncnt,Gs[ncnt]=G;
	}
	// assert(ncnt==1296);
	for(int i=1;i<=ncnt;i++){
		auto G=Gs[i];
		for(int a=0;a<n;a++)for(int b=0;b<n;b++)for(int c=0;c<n;c++)for(int d=0;d<n;d++){
			if(a==b||a==c||a==d||b==c||b==d||c==d)continue;
			if(!G[a][b]||!G[b][c]||!G[c][d])continue;
			oper(a,b,c,d,G);
			to[i].emplace_back(mk(ID[G],OP(a,b,c,d)));
			oper(c,a,d,b,G);
		}
	}

	queue<int>Q;Q.push(2);
	while(Q.size()){
		int x=Q.front();Q.pop();
		for(auto [y,op]:to[x])if(!pre[y])pre[y]=x,rec[y]=op,Q.push(y);
	}
}

vector<OP>find_path(vector<vector<int> >G1,vector<vector<int> >G2){
	int n=G1.size();
	// cerr<<" G1 = ";for(int i=0;i<n;i++)for(int j=i+1;j<n;j++)if(G1[i][j])cerr<<"("<<i<<","<<j<<") ";cerr<<endl;
	// cerr<<" G2 = ";for(int i=0;i<n;i++)for(int j=i+1;j<n;j++)if(G2[i][j])cerr<<"("<<i<<","<<j<<") ";cerr<<endl;
	int x=ID[G1],y=ID[G2];
	// cerr<<"x,y = "<<x<<" "<<y<<" pre = "<<pre[x]<<" "<<pre[y]<<endl;
	assert(pre[x]&&pre[y]);
	vector<OP>path,path2;
	while(y!=2)path2.emplace_back(rec[y]),y=pre[y];
	reverse(path2.begin(),path2.end());
	while(x!=2)path.emplace_back(rev(rec[x])),x=pre[x];
	for(auto op:path2)path.emplace_back(op);
	return path;
}

vector<OP>ans;
void add_op(int a,int b,int c,int d){ans.emplace_back(OP(a,b,c,d));}

void solve(){
	int n=read(),k=3;
	vector<vector<int> >G(n,vector<int>(n)),adj(n);
	for(int i=1;i<=n-1;i++){
		int u=read()-1,v=read()-1;
		G[u][v]=G[v][u]=1,adj[u].emplace_back(v),adj[v].emplace_back(u);
	}

	auto bfs=[&](int u){
		queue<int>q;vector<int>dis(n,-1);
		dis[u]=0;q.push(u);
		while(q.size()){
			int x=q.front();q.pop();
			for(int y=0;y<n;y++)if(G[x][y]&&dis[y]==-1)dis[y]=dis[x]+1,q.push(y);
		}
		return dis;
	};

	auto diam=[&](){
		auto dis=bfs(0);int x=0;
		for(int i=0;i<n;i++)if(dis[i]>dis[x])x=i;
		dis=bfs(x);
		for(int i=0;i<n;i++)if(dis[i]>dis[x])x=i;
		return dis[x];
	};

	if(diam()<=2)return puts("0"),void();
	if(k==2)return puts("-1"),void();
	if(n==5){
		if(diam()==4){
			auto dis=bfs(0);int x=0;
			for(int i=0;i<n;i++)if(dis[i]>dis[x])x=i;
			dis=bfs(x);vector<int>id(10);
			for(int i=0;i<n;i++)id[dis[i]]=i;
			puts("1");
			cout<<id[0]<<" "<<id[1]<<" "<<id[2]<<" "<<id[3]<<endl;
		}
		else puts("0");
		return ;
	}

	auto compact=[&](vector<int>nodes){
		assert(nodes.size()==6&&nodes[0]==0);
		// cerr<<"compact : nodes = ";for(int x:nodes)cerr<<x<<" ";cerr<<endl;

		vector<int>id(n);
		for(int i=0;i<6;i++)id[nodes[i]]=i;
		vector<vector<int> >G1(6,vector<int>(6)),G2(6,vector<int>(6));
		for(int i=0;i<6;i++)for(int j=0;j<6;j++)G1[i][j]=G[nodes[i]][nodes[j]];
		G2[0][1]=G2[0][2]=G2[0][3]=G2[0][4]=G2[4][5]=1;
		G2[1][0]=G2[2][0]=G2[3][0]=G2[4][0]=G2[5][4]=1;
		
		auto ops=find_path(G1,G2);
		for(auto op:ops){
			auto [a,b,c,d]=op;
			a=nodes[a],b=nodes[b],c=nodes[c],d=nodes[d];
			oper(a,b,c,d,G),add_op(a,b,c,d);
		}
	};

	// cerr<<"now G = ";for(int i=0;i<n;i++)for(int j=i+1;j<n;j++)if(G[i][j])cout<<"("<<i<<","<<j<<") ";puts("");
	// cerr<<"diam = "<<diam()<<endl;
	while(true){
		vector<int>fa(n),dep(n),d(n),hson(n,-1);
		function<void(int)>dfs=[&](int u){
			hson[u]=-1;
			for(int v=0;v<n;v++)if(G[u][v]&&fa[u]!=v){
				fa[v]=u,dep[v]=dep[u]+1,dfs(v);
				cmax(d[u],d[v]+1);if(hson[u]==-1||d[v]>d[hson[u]])hson[u]=v;
			}
		};
		dfs(0);bool hav=false;
		for(int i=0;i<n;i++)if(dep[i]>=3)hav=true;
		// cerr<<"dep = ";for(int i=0;i<n;i++)cerr<<dep[i]<<" \n"[i==n-1];
		if(!hav)break;

		vector<int>nodes;
		function<void(int)>dfs2=[&](int u){
			if(nodes.size()==6)return ;
			nodes.emplace_back(u);
			if(hson[u]!=-1)dfs2(hson[u]);
			for(int v=0;v<n;v++)if(G[u][v]&&v!=fa[u]&&v!=hson[u])dfs2(v);
		};

		dfs2(0);
		compact(nodes);
		// cerr<<" -> G = ";for(int i=0;i<n;i++)for(int j=i+1;j<n;j++)if(G[i][j])cout<<"("<<i<<","<<j<<") ";puts("");
	}

	auto output=[&](){
		cout<<ans.size()<<'\n';
		for(auto [a,b,c,d]:ans)cout<<a+1<<" "<<b+1<<" "<<c+1<<" "<<d+1<<'\n';
		vector<OP>().swap(ans);
		return ;
	};

	if(diam()<=3)return output();
	assert(diam()==4);
	// cerr<<"ok"<<endl;

	while(diam()==4){
		vector<int>fa(n),dep(n);
		function<void(int)>dfs=[&](int u){
			for(int v=0;v<n;v++)if(v!=fa[u]&&G[u][v])fa[v]=u,dep[v]=dep[u]+1,dfs(v);
		};
		dfs(0);
	
		int x=-1,y=-1;
		for(int i=0;i<n;i++)if(dep[i]==2){
			x=i;auto dis=bfs(x);
			for(int j=0;j<n;j++)if(dis[j]==4){y=j;break;}
			break;
		}
		assert(x!=-1&&y!=-1);

		int p=-1;
		for(int i=0;i<n;i++)if(i!=0&&i!=x&&i!=y&&i!=fa[x]&&i!=fa[y]){
			if(G[0][i]||G[fa[x]][i]||G[fa[y]][i])p=i;
		}
		assert(p!=-1);
		// cerr<<"x,y = "<<x<<" "<<y<<" fa = "<<fa[x]<<" "<<fa[y]<<" p = "<<p<<endl;
		vector<int>nodes(6);
		nodes[0]=0,nodes[1]=x,nodes[2]=y,nodes[3]=fa[x],nodes[4]=fa[y],nodes[5]=p;
		compact(nodes);
	}

	assert(diam()<=3);
	output();
}

signed main(void){

#ifndef ONLINE_JUDGE
	freopen("2.in","r",stdin);
	freopen("2.out","w",stdout);
#endif

	prework();
	int tt=1;
	// int tt=read();
	while(tt--)solve();

	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 7ms
memory: 4864kb

input:

6
1 2
2 3
3 4
4 5
5 6

output:

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

result:

ok ok nice tree :D

Test #2:

score: 0
Accepted
time: 12ms
memory: 4992kb

input:

100
4 44
45 44
33 44
60 44
71 44
25 44
69 44
57 44
16 44
78 44
17 44
10 44
21 44
80 44
2 44
98 44
20 44
83 44
3 44
79 44
22 44
65 44
5 44
39 44
23 44
100 44
66 44
7 44
14 44
81 44
6 44
41 44
59 44
87 44
32 44
63 44
74 44
84 44
86 44
18 44
24 44
96 4
35 44
8 44
62 44
58 44
85 44
53 44
82 44
42 44
12 ...

output:

1177
1 4 44 45
44 1 45 70
70 44 45 4
48 45 70 1
44 4 70 48
70 44 48 1
4 48 70 1
44 1 4 70
44 70 1 45
4 44 45 70
48 1 44 70
1 70 4 45
44 48 70 45
1 45 44 70
1 70 45 48
4 1 44 2
100 44 4 26
1 2 4 100
4 1 100 26
26 4 100 2
4 2 26 44
1 26 4 44
100 26 44 1
2 44 100 1
26 1 2 100
26 100 1 44
2 26 44 100
4 ...

result:

ok ok nice tree :D

Test #3:

score: 0
Accepted
time: 11ms
memory: 5120kb

input:

99
60 59
19 59
47 59
83 59
41 59
33 59
99 59
7 59
15 59
36 59
50 59
9 59
10 59
76 59
14 59
30 59
90 59
43 59
88 59
8 59
27 59
25 59
81 59
3 59
38 59
68 59
24 59
1 59
58 59
21 59
44 59
87 59
4 59
74 59
72 59
79 59
16 59
64 60
82 59
12 59
37 59
22 59
61 59
65 59
20 59
35 59
54 59
56 60
23 59
26 59
28 ...

output:

1215
1 59 19 77
2 59 77 1
19 1 2 77
59 1 77 19
77 59 19 71
1 19 77 71
2 19 71 1
59 71 2 1
19 1 59 2
19 2 1 71
59 19 71 2
77 1 19 2
1 2 59 71
19 77 2 71
1 71 19 2
1 2 71 77
1 59 60 56
56 1 60 29
49 60 56 29
29 49 56 59
49 59 29 60
1 29 49 60
1 60 29 56
59 60 56 1
60 1 59 56
60 56 1 29
59 60 29 56
49 ...

result:

ok ok nice tree :D

Test #4:

score: 0
Accepted
time: 9ms
memory: 4992kb

input:

99
59 57
45 57
93 57
5 57
80 57
77 57
70 57
14 57
64 57
39 57
81 57
18 57
7 57
11 57
73 57
67 57
8 57
29 59
66 57
63 57
4 57
74 57
83 57
21 57
52 59
28 57
27 57
26 57
36 57
38 57
32 59
53 59
24 57
49 57
58 45
94 57
89 57
65 59
12 57
46 59
54 57
60 57
51 57
50 57
15 59
19 59
40 93
35 45
6 57
99 59
96...

output:

821
1 57 5 76
4 57 76 1
5 1 4 76
57 1 76 5
76 57 5 37
1 5 76 37
4 5 37 1
57 37 4 1
5 1 57 4
5 4 1 37
57 5 37 4
76 1 5 4
1 4 57 37
5 76 4 37
1 37 5 4
1 4 37 76
1 57 45 35
35 1 45 2
3 45 35 2
2 3 35 57
3 57 2 45
1 2 3 45
1 45 2 35
57 45 35 1
45 1 57 35
45 35 1 2
57 45 2 35
3 1 45 35
1 35 57 2
45 3 35 ...

result:

ok ok nice tree :D

Test #5:

score: 0
Accepted
time: 10ms
memory: 4992kb

input:

98
77 49
52 49
78 49
72 49
91 49
85 49
21 49
32 49
96 49
42 49
79 49
41 49
89 49
24 49
46 49
36 49
48 49
86 49
12 49
88 49
60 49
6 49
39 49
53 49
59 49
90 49
50 49
25 49
10 49
81 49
83 49
54 49
82 49
16 52
94 49
87 49
40 49
14 52
65 77
19 49
51 49
7 49
98 49
84 78
4 49
9 77
70 52
75 49
20 77
80 77
6...

output:

907
1 49 52 18
18 1 52 14
16 52 18 14
14 16 18 49
16 49 14 52
1 14 16 52
1 52 14 18
49 52 18 1
52 1 49 18
52 18 1 14
49 52 14 18
16 1 52 18
1 18 49 14
52 16 18 14
1 14 52 18
1 18 14 16
1 49 72 76
76 1 72 28
45 72 76 28
28 45 76 49
45 49 28 72
1 28 45 72
1 72 28 76
49 72 76 1
72 1 49 76
72 76 1 28
49...

result:

ok ok nice tree :D

Test #6:

score: 0
Accepted
time: 11ms
memory: 4992kb

input:

100
26 54
89 54
35 54
99 54
24 54
18 54
66 54
59 54
49 54
52 54
70 54
73 54
1 26
4 54
69 54
20 26
2 54
50 26
79 54
94 54
78 89
5 54
74 54
44 54
75 54
98 54
96 54
90 54
17 54
38 26
58 26
80 89
30 26
15 26
48 26
16 26
82 35
63 54
33 26
39 89
22 89
65 54
67 54
60 89
3 54
97 89
7 70
40 26
19 26
56 54
88...

output:

841
1 26 54 24
54 1 24 21
21 54 24 26
13 24 21 1
54 26 21 13
21 54 13 1
26 13 21 1
54 1 26 21
54 21 1 24
26 54 24 21
13 1 54 21
1 21 26 24
54 13 21 24
1 24 54 21
1 21 24 13
15 26 50 27
16 26 27 15
50 15 16 27
1 26 15 27
16 50 27 1
26 27 16 1
50 1 26 16
50 16 1 27
26 50 27 16
15 1 50 16
1 16 26 27
50...

result:

ok ok nice tree :D

Test #7:

score: 0
Accepted
time: 9ms
memory: 4992kb

input:

98
84 21
68 21
67 21
78 21
93 21
98 21
19 21
51 21
43 21
96 21
95 21
50 84
7 21
27 21
6 21
89 21
46 21
38 84
71 84
53 68
4 21
69 84
77 21
14 21
32 84
29 21
42 84
40 21
91 21
15 21
5 21
82 21
57 84
44 21
28 21
13 84
9 68
41 21
1 68
62 21
52 21
65 84
86 93
31 21
87 93
39 67
37 93
75 68
56 21
36 21
83 ...

output:

763
1 68 21 19
21 1 19 23
23 21 19 68
11 19 23 1
21 68 23 11
23 21 11 1
68 11 23 1
21 1 68 23
21 23 1 19
68 21 19 23
11 1 21 23
1 23 68 19
21 11 23 19
1 19 21 23
1 23 19 11
3 21 51 8
4 21 8 3
51 3 4 8
1 21 3 8
4 51 8 1
21 8 4 1
51 1 21 4
51 4 1 8
21 51 8 4
3 1 51 4
1 4 21 8
51 3 4 8
1 8 51 4
1 4 8 3...

result:

ok ok nice tree :D

Test #8:

score: -100
Runtime Error

input:

99
94 74
1 74
89 74
80 74
87 74
79 74
61 74
23 74
96 94
24 74
25 74
97 74
86 94
82 74
5 74
76 74
41 89
9 94
43 74
50 89
17 89
12 74
72 74
3 74
8 74
58 74
18 23
62 74
60 74
39 89
15 87
55 74
10 89
27 80
44 74
42 94
11 94
48 1
98 23
63 89
37 89
54 80
4 1
91 74
93 74
29 94
59 74
78 94
32 79
38 80
52 94...

output:


result: