codeforces B. Flag Day 解题报告
题目链接: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 解题报告的更多相关文章
- Codeforces Round 665 赛后解题报告(暂A-D)
Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...
- Codeforces Round 662 赛后解题报告(A-E2)
Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...
- Codeforces Round #277.5 解题报告
又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...
- codeforces B. Simple Molecules 解题报告
题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...
- codeforces 507B. Painting Pebbles 解题报告
题目链接:http://codeforces.com/problemset/problem/509/B 题目意思:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色 ...
- codeforces 495B. Modular Equations 解题报告
题目链接:http://codeforces.com/problemset/problem/495/B 题目意思:给出两个非负整数a,b,求出符合这个等式 的所有x,并输出 x 的数量,如果 ...
- codeforces 475A.Bayan Bus 解题报告
题目链接:http://codeforces.com/problemset/problem/475/A 题目意思:输入一个整数 k(0 ≤ k ≤ 34),表示participants的人数,需要在一 ...
- codeforces A. Strange Addition 解题报告
题目链接:http://codeforces.com/problemset/problem/305/A 题目意思:给出一个序列,需要从中选择一些数,这些数需要满足:任意的两个数中每一位至少有一个数满足 ...
- codeforces A. Cinema Line 解题报告
题目链接:http://codeforces.com/problemset/problem/349/A 题目意思:题目不难理解,从一开始什么钱都没有的情况下,要向每一个人售票,每张票价格是25卢布,这 ...
随机推荐
- MyEclipse------如何添加jspsmartupload.jar+文件上传到服务器
下载地址:http://download.csdn.net/detail/heidan2006/182263 如何添加jspsmartupload.jar:右键“Web”工程->properti ...
- 神经网络训练中的Tricks之高效BP(反向传播算法)
神经网络训练中的Tricks之高效BP(反向传播算法) 神经网络训练中的Tricks之高效BP(反向传播算法) zouxy09@qq.com http://blog.csdn.net/zouxy09 ...
- linux安全加固(1)
Redhat是目前企业中用的最多的一类Linux,而目前针对Redhat攻击的黑客也越来越多了.我们要如何为这类服务器做好安全加固工作呢? 一. 账户安全 1.1 锁定系统中多余的自建帐号 检查方法: ...
- centos 7 的几点改动
1.运行级别 旧:/etc/inittab 新:/etc/ststemd/system 例:ln -sf /lib/systemd/system/multi-user.target /etc/sys ...
- CentOS 7 使用经验(更新中)
首先说一下写这篇博客的初衷. 由于公司这一期的产品准备支持的环境有CentOS 7.MySql 5.6.Java 8.Tomcat 8等等,并且因为人员严重不足,我本月的开发任务在原有的基础上又加上了 ...
- SpringMVC 返回JSON数据
首先添加json包
- AppSettingManager
public class AppSettingManager { public static bool Update(string key, string value) { try { var con ...
- Html Div 拖拽
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- exe4j中"this executable was created with an evaluation错误解决方法
在使用exe4j时,如果您的exe4j没有注册,在运行有exe4j转换的*.jar为*.exe的可执行文件是会提示:"this executable was created with an ...
- bat programming is easy and powerful
用linux的角度来思考windows,习惯了linux的shell后, 再来看windows的bat编程,就简单多了,简直就是理所当然 实际上windows的cmd命令行和linux的shell命令 ...