B. Flag Day
来源:互联网 发布:搜图片出处软件 编辑:程序博客网 时间:2024/05/14 18:56
In Berland, there is the national holiday coming — the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to organize it. He has several conditions:
- overall, there must be m dances;
The agency has n dancers, and their number can be less than 3m. That is, some dancers will probably have to dance in more than one dance. All of your dancers must dance on the party. However, if some dance has two or more dancers from a previous dance, then the current dance stops being spectacular. Your agency cannot allow that to happen, so each dance has at most one dancer who has danced in some previous dance.
You considered all the criteria and made the plan for the m dances: each dance had three dancers participating in it. Your task is to determine the clothes color for each of the n dancers so that the President's third condition fulfilled: each dance must have a dancer in white, a dancer in red and a dancer in blue. The dancers cannot change clothes between the dances.
The first line contains two space-separated integers n (3 ≤ n ≤ 105) and m (1 ≤ m ≤ 105) — the number of dancers and the number of dances, correspondingly. Then m lines follow, describing the dances in the order of dancing them. The i-th line contains three distinct integers — the numbers of the dancers that take part in the i-th dance. The dancers are numbered from 1 to n. Each dancer takes part in at least one dance.
Print n space-separated integers: the i-th number must represent the color of the i-th dancer's clothes (1 for white, 2 for red, 3 for blue). If there are multiple valid solutions, print any of them. It is guaranteed that at least one solution exists.
7 31 2 31 4 54 6 7
1 2 3 3 2 2 1
9 33 6 92 5 81 4 7
1 1 1 2 2 2 3 3 3
5 24 1 53 1 2
2 3 1 1 3
解题说明:题目意思是输入n个人和m场舞蹈,给出每场舞蹈(只有3个人参与)中参与的舞者的编号,你需要为这些舞者安排衣服的颜色,使得每场舞蹈中3个舞者的颜色都满足是白,红,绿的条件,同时保证每一场舞蹈最多只可能有一个旧舞者。做法是判断一组里面第一个出现的之前跳过舞的人,让其他两个人的颜色为这个跳过舞的人的颜色后两位(在1,2,3之间循环)。若加和完之后大于3,自减3 。若全部人之前都没有跳过舞,直接赋值1,2,3
#include <iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>#include<cstdlib>using namespace std;int main(){int n,m,i,x,y,z;int color[100005];scanf("%d%d",&n,&m);memset(color,0,sizeof(color));for(i=0;i<m;i++){scanf("%d%d%d",&x,&y,&z);if(color[x]!=0) {color[y]=color[x]%3+1;color[z]=color[y]%3+1;}else if(color[y]!=0) {color[z]=color[y]%3+1;color[x]=color[z]%3+1;}else if(color[z]!=0) {color[x]=color[z]%3+1;color[y]=color[x]%3+1;}else{color[x]=1;color[y]=2;color[z]=3;}}for(i=1;i<=n;i++){printf("%d ",color[i]);}printf("\n");return 0;}
- B. Flag Day
- 【codeforce】B. Flag Day
- codeforces 357B Flag Day
- codeforces#357B. Flag Day
- CodeForces#207Div.2 - B. Flag Day
- Code Forces 357 B. Flag Day 暴力map
- Flag Day: xVM version updated
- codeforces 837B Flag of Berland
- B Have a Nice Day
- DAY 3 问题 B: 超车
- 问题 B: Day of Week
- FLag
- Flag
- flag
- flag
- flag
- FLAG
- Flag
- Path图片缓存实现方法
- Linux I2C子系统分析-I2C总线驱动&&Linux I2C子系统分析-I2C设备驱动
- 安装Zend Studio 9.0.4
- 为何HTML5是Android开发的一个很好的选择
- Android开发中使用eclipse中的DDMS对应用的内存检测
- B. Flag Day
- java之jvm学习笔记八(实践对jar包的代码签名)
- OCP-1Z0-052-V8.02-176题
- 黑马训练营--反射
- 【算法】Dancing Links (DLX) I
- 即是5的倍数又是7的倍数
- What Can Happen When You Try to Convert a Standard SharePoint 2010 Farm to Enterprise
- 记事本 java版(有瑕疵)
- Linux并发处理