题目链接: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. MongoDB的安装及配置

    MongoDB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐. Windows (1). 登录Mongodb官网点击下载 (2). 将zi ...

  2. 旅行(Dijkstra)问题

    问题:输入: 输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个:   接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车 ...

  3. BZOJ-3282 Tree Link-Cut-Tree(似乎树链剖分亦可)

    蛋蛋用链剖A的,我写的LCT 3282: Tree Time Limit: 30 Sec Memory Limit: 512 MB Submit: 1241 Solved: 542 [Submit][ ...

  4. firefox(ff)下无法显示bootstrap图标问题的解决方案(转)

    原文链接: http://www.th7.cn/web/html-css/201502/82548.shtml 后在网上搜到了解决方案,在此分享以供各位遇到问题的同好参考:在ff的地址栏中输入“abo ...

  5. hihocoder 1154 Spring Outing

    传送门 #1154 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring ...

  6. Extreme Learning Machine(ELM)的工程哲学

    Extreme Learning Machine(ELM)的工程哲学 David_Wang2015 发布于2015年5月6日 11:29 工程问题往往需要的是一定精度范围内的结果,而不是“真正的”结果 ...

  7. 拆分Spring配置文件

    把一个applicationContext.xml拆分(还可以再分出action部分和tx事务部分) 1.dao部分 <!-- 配置DAO --> <bean id="em ...

  8. systemctl 命令的用法

    对比表,以 apache / httpd 为例 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 httpd on systemctl enable httpd.serv ...

  9. 《深入PHP与jQuery开发》读书笔记——Chapter3

    <深入PHP与jQuery开发>第三章学习笔记 1.PHP的魔术方法(在对象中发生某些例行事件时会自动调用这些方法) PHP提供了魔术方法__construct()(构造函数),在新对象被 ...

  10. GNU make 升级

    网上下载新版本的make文件后 在make目录下 ./configure make make install mv make /opt/mv_pro_5.0.0/montavista/common/b ...