题目链接:http://codeforces.com/problemset/problem/357/B

题目意思:输入n个人和m场舞蹈,给出每场舞蹈(只有3个人参与)中参与的舞者的编号,你需要为这些舞者安排衣服的颜色,使得每场舞蹈中3个舞者的颜色都满足是白,红,绿的条件。

特别要注意题目中的两句话:if some dance has two or more dancers from a previous dance, then the current dance stops being spectacular. 和 each dance has at most one dancer who has danced in some previous dance 。也就是说,每一场舞蹈最多只可能有一个旧舞者!

思路其实不难,对于某场舞蹈三个都是新舞者分配颜色就任意;而存在一个旧舞者的话,就要为其余的两个新舞者分配不同于旧舞者的颜色。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = 1e5 + ; int dan[maxn];
int visit[maxn];
int t[]; int main()
{
int m, n, i, j, k, l, t1, t2, t3, temp;
while (scanf("%d%d", &n, &m) != EOF)
{
memset(visit, , sizeof(visit));
memset(dan, , sizeof(dan));
while (m--)
{
scanf("%d%d%d", &t1, &t2, &t3);
int judge = (visit[t1] == ) + (visit[t2] == ) + (visit[t3] == ); // 判断是否存在旧舞者,0代表没有
if (!judge) // 该轮全部为新舞者,依次编号1, 2, 3
{
dan[t1] = ;
dan[t2] = ;
dan[t3] = ;
visit[t1] = visit[t2] = visit[t3] = ;
}
else
{
t[] = t1;
t[] = t2;
t[] = t3;
for (i = ; i <= ; i++) // 判断哪一个是旧舞者
{
if (visit[t[i]])
{
temp = i; // 记住旧舞者在数组t中的编号,以便后面的处理
break;
}
}
for (i = ; i <= ; i++)
{
if (i != temp) // 不是旧舞者的第一个新舞者的编号
{
for (j = ; j <= ; j++) // 分配颜色
{
if (j != dan[t[temp]]) // 第一个新舞者颜色不能跟旧舞者相同
{
dan[t[i]] = j;
visit[t[i]] = ;
for (k = ; k <= ; k++)
{
if (k != i && k != temp) // 第二个新舞者的编号
{
for (l = ; l <= ; l++)
{
if (l != dan[t[temp]] && l != dan[t[i]]) // 第二个新舞者的颜色不允许与第一个新舞者和旧舞者的颜色相同
{
dan[t[k]] = l;
visit[t[k]] = ;
goto h; // 找到一组解,跳出循环
}
}
}
}
}
}
}
}
}
h: ;
}
for (i = ; i <= n; i++)
{
printf("%d ", dan[i]);
}
printf("\n");
}
return ;
}

codeforces B. Flag Day 解题报告的更多相关文章

  1. Codeforces Round 665 赛后解题报告(暂A-D)

    Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...

  2. Codeforces Round 662 赛后解题报告(A-E2)

    Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...

  3. Codeforces Round #277.5 解题报告

    又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...

  4. codeforces B. Simple Molecules 解题报告

    题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...

  5. codeforces 507B. Painting Pebbles 解题报告

    题目链接:http://codeforces.com/problemset/problem/509/B 题目意思:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色 ...

  6. codeforces 495B. Modular Equations 解题报告

    题目链接:http://codeforces.com/problemset/problem/495/B 题目意思:给出两个非负整数a,b,求出符合这个等式      的所有x,并输出 x 的数量,如果 ...

  7. codeforces 475A.Bayan Bus 解题报告

    题目链接:http://codeforces.com/problemset/problem/475/A 题目意思:输入一个整数 k(0 ≤ k ≤ 34),表示participants的人数,需要在一 ...

  8. codeforces A. Strange Addition 解题报告

    题目链接:http://codeforces.com/problemset/problem/305/A 题目意思:给出一个序列,需要从中选择一些数,这些数需要满足:任意的两个数中每一位至少有一个数满足 ...

  9. codeforces A. Cinema Line 解题报告

    题目链接:http://codeforces.com/problemset/problem/349/A 题目意思:题目不难理解,从一开始什么钱都没有的情况下,要向每一个人售票,每张票价格是25卢布,这 ...

随机推荐

  1. 使用XML序列化器生成XML文件和利用pull解析XML文件

    首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...

  2. 如何查询Oracle中用户所有信息

    1.查看所有用户:   select * from dba_users;     select * from all_users;     select * from user_users;   2. ...

  3. 修改mysql最大连接数的方法

    MYSQL数据库安装完成后,默认最大连接数是100,一般流量稍微大一点的论坛或网站这个连接数是远远不够的,增加默认MYSQL连接数的方法有两个 方法一:进入MYSQL安装目录 打开MYSQL配置文件 ...

  4. Opencv加载和显示图片

    #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <ios ...

  5. Linux下添加新硬盘,分区及挂载

    挂载好新硬盘后输入fdisk -l命令看当前磁盘信息 可以看到除了当前的第一块硬盘外还有一块sdb的第二块硬盘,然后用fdisk /dev/sdb 进行分区 进入fdisk命令,输入h可以看到该命令的 ...

  6. make_pair

    Utilities <utility> 由短小精干的类和函数构成,执行最一般性的工作. 这些工具包括: general types 一些重要的C函数 numeric limits Pair ...

  7. tp auth 转载保存

    PS:最近需要做一个验证用户权限的功能,在官方和百度看了下,发现大家都是用auth来做验证,官方有很多auth的使用教程,但是都不全面,我也提问了几个关于auth的问题 也没人来回答我,无奈只好一步步 ...

  8. 高速公路(Highway,ACM/ICPC SEERC 2005,UVa1615)

    I think: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <m ...

  9. centos安装gitlab

    原文链接: http://www.centoscn.com/image-text/install/2015/0320/4929.html http://www.01happy.com/centos-6 ...

  10. 图解equals与hashcode方法相等/不相等的互相关系

    图解:比如equals相等的箭头指向hashcode相等,表示equals相等那么必有hashcode相等.而有两个箭头指向别人的表示可能是其中之一,比如hashcode相等,那么有可能equals相 ...