1、题目链接:http://codeforces.com/problemset/problem/357/B
2、这道题刚开始想用回溯法,非常麻烦,其实仔细一想就知道模拟即可,因为前面已经出现过的人最多在后面出现一次,每一轮至少有两个人的衣服颜色是不确定的,因此不会出现冲突。
#include<cstdio> #include<cstring> using namespace std; int a[100010][3],num[100010],b[3],n,m,cnt; int main(){ while(scanf("%d%d",&n,&m)==2){ cnt=0; memset(num,0,sizeof(num)); for(int i=0;i<m;i++){ for(int j=0;j<3;j++) scanf("%d",&a[i][j]); } for(int depth=0;depth<m;depth++){ for(int i=1;i<=3;i++) for(int j=1;j<=3;j++) if(i!=j){ for(int k=1;k<=3;k++){ if(i!=k&&j!=k){ if(num[a[depth][0]]==0) b[0]=i; else b[0]=num[a[depth][0]]; if(num[a[depth][1]]==0) b[1]=j; else b[1]=num[a[depth][1]]; if(num[a[depth][2]]==0) b[2]=k; else b[2]=num[a[depth][2]]; if(b[0]!=b[1]&&b[1]!=b[2]&&b[0]!=b[2]) for(int i=0;i<3;i++) num[a[depth][i]]=b[i]; } } } } printf("%d",num[1]); for(int i=2;i<=n;i++) printf(" %d",num[i]); printf("\n"); } return 0; }