Codeforces629-Div. 3-D. Carousel(dfs+剪枝)

D. Carousel

The round carousel consists of n figures of animals. Figures are numbered from 1 to n in order of the carousel moving. Thus, after the n-th figure the figure with the number 1 follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the i-th figure equals ti.
在这里插入图片描述
The example of the carousel for n=9 and t=[5,5,1,15,1,5,5,1,1].
You want to color each figure in one of the colors. You think that it’s boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color.

Your task is to color the figures in such a way that the number of distinct colors used is the minimum possible and there are no figures of the different types going one right after another and colored in the same color. If you use exactly k distinct colors, then the colors of figures should be denoted with integers from 1 to k.

Input
The input contains one or more test cases.

The first line contains one integer q (1≤q≤104) — the number of test cases in the test. Then q test cases follow. One test case is given on two lines.

The first line of the test case contains one integer n (3≤n≤2⋅105) — the number of figures in the carousel. Figures are numbered from 1 to n in order of carousel moving. Assume that after the n-th figure the figure 1 goes.

The second line of the test case contains n integers t1,t2,…,tn (1≤ti≤2⋅105), where ti is the type of the animal of the i-th figure.

The sum of n over all test cases does not exceed 2⋅105.

Output
Print q answers, for each test case print two lines.

In the first line print one integer k — the minimum possible number of distinct colors of figures.

In the second line print n integers c1,c2,…,cn (1≤ci≤k), where ci is the color of the i-th figure. If there are several answers, you can print any.

题意:

告诉你一个序列,不同的数字代表不同的种类的动物,你需要给每只动物上色,如果相邻两个动物种类不同,那么他们涂的颜色也必须是不同的,如果相同种类相同,则可以涂不同的颜色也可以图相同的颜色
问最少需要多少种颜色去涂,且输出涂色方案(首位相连)

解题思路:

dfs+剪枝

类似于图的染色问题,当然可以直接建图(不过好像有点复杂,需要考虑相邻点相同不能连边的情况)。所以直接利用相邻顶点作为边的判断即可。

关于为什么不能用贪心:
8
13 13 9 12 13 1 13 1
贪心答案:
3
1 1 2 1 2 1 2 3
正解:
2
1 2 1 2 1 2 1 2

AC代码

#include<iostream>
#include<cstring>
#include<string>
#include<math.h>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;

int T,N,M;//M记录所需的最小染色个数 
int a[200005];
int b[200005];
int c[200005]; //记录当前染色的最优情况 
void dfs(int n,int m){//前n个动物当前需要m种颜色 
	int i;
	if(m>=M)//剪枝 
		return;
	if(n==N){
		if((a[n]==a[n-1])||(a[n]!=a[n-1]&&b[n-1]!=b[1]))
			if(m<M){
				M=m;
				for(i = 1;i<N;i++){//找到当前最小的M,则记录染色情况 
					c[i]=b[i];
				}
			}
		return;
	}
	if(M==1)//剪枝,如果只有一种染色,直接退出 
		return;
	for(i = 1;i<=m;i++){
		b[n] = i;
		if(a[n]==a[n-1])//第n只动物与第n-1只动物相等的情况 
			dfs(n+1,m);
		else{
			if(b[n]==b[n-1])
				continue;
			else
				dfs(n+1,m);
		}
	}
	//如果m种颜色不能满足前n只动物染色,则将第n只动物染为m+1 
	b[n] = m+1;
	dfs(n+1,m+1);
	return;
}

int main(){
	int i,j,k;
	cin>>T;
	for(i = 1;i<=T;i++){
		scanf("%d",&N);
		for(j = 1;j<=N;j++){
			scanf("%d",&a[j]);
		}
		a[j] = a[1];N++;//在最后与a[1]相连,构成长度为N+1的链 
		M=inf;
		b[1] = 1; //将第一只动物染色为1 
		dfs(2,1);
		printf("%d\n",M);
		for(j = 1;j<N;j++){
			printf("%d ",c[j]);
		}
		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值