codeforces B. Flag Day 解题报告
2013-10-17 09:59
393 查看
题目链接: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 。也就是说,每一场舞蹈最多只可能有一个旧舞者!
思路其实不难,对于某场舞蹈三个都是新舞者分配颜色就任意;而存在一个旧舞者的话,就要为其余的两个新舞者分配不同于旧舞者的颜色。
题目意思:输入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 + 10; int dan[maxn]; int visit[maxn]; int t[5]; int main() { int m, n, i, j, k, l, t1, t2, t3, temp; while (scanf("%d%d", &n, &m) != EOF) { memset(visit, 0, sizeof(visit)); memset(dan, 0, sizeof(dan)); while (m--) { scanf("%d%d%d", &t1, &t2, &t3); int judge = (visit[t1] == 1) + (visit[t2] == 1) + (visit[t3] == 1); // 判断是否存在旧舞者,0代表没有 if (!judge) // 该轮全部为新舞者,依次编号1, 2, 3 { dan[t1] = 1; dan[t2] = 2; dan[t3] = 3; visit[t1] = visit[t2] = visit[t3] = 1; } else { t[1] = t1; t[2] = t2; t[3] = t3; for (i = 1; i <= 3; i++) // 判断哪一个是旧舞者 { if (visit[t[i]]) { temp = i; // 记住旧舞者在数组t中的编号,以便后面的处理 break; } } for (i = 1; i <= 3; i++) { if (i != temp) // 不是旧舞者的第一个新舞者的编号 { for (j = 1; j <= 3; j++) // 分配颜色 { if (j != dan[t[temp]]) // 第一个新舞者颜色不能跟旧舞者相同 { dan[t[i]] = j; visit[t[i]] = 1; for (k = 1; k <= 3; k++) { if (k != i && k != temp) // 第二个新舞者的编号 { for (l = 1; l <= 3; l++) { if (l != dan[t[temp]] && l != dan[t[i]]) // 第二个新舞者的颜色不允许与第一个新舞者和旧舞者的颜色相同 { dan[t[k]] = l; visit[t[k]] = 1; goto h; // 找到一组解,跳出循环 } } } } } } } } } h: ; } for (i = 1; i <= n; i++) { printf("%d ", dan[i]); } printf("\n"); } return 0; }
相关文章推荐
- 面试常考的常用数据结构与算法(zz)
- tomcat部署java项目之后,war可以删除吗?
- onAttachedToWindow () 和 onDetachedFromWindow ()
- 从三个排好序的整数数组中分别选一个元素,使得这三个元素的最大差最小
- iptables 禁止端口和开放端口
- DLL快速入门
- python之poplib库
- 简单的分支与循环结构2
- 实验4
- 随堂作业3
- 课堂作业2
- 菜鸟学数据库(四)——超键、候选键、主键、外键
- 5ucms 调用当前文章的评论,以及评论列表
- 自动过滤添加域名中的http
- Several ports (8005, 8080, 8009) required by Tomcat Server at localhost are already in use
- 菜鸟学数据库(四)——超键、候选键、主键、外键
- 如何解决inline-block元素的空白间距
- 实验 4 在分支循环结构中调用自定义函数
- an concreate example
- 自定义函数替代file_get_contents获取远程数据