B. Flag Day

来源:互联网 发布:搜图片出处软件 编辑:程序博客网 时间:2024/05/14 18:56

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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;

  • exactly three people must take part in each dance;

  • each dance must have one dancer in white clothes, one dancer in red clothes and one dancer in blue clothes (these are the colors of the national flag of Berland).

    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.

  • Input

    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.

    Output

    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.

    Sample test(s)
    input
    7 31 2 31 4 54 6 7
    output
    1 2 3 3 2 2 1 
    input
    9 33 6 92 5 81 4 7
    output
    1 1 1 2 2 2 3 3 3 
    input
    5 24 1 53 1 2
    output
    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;}


    原创粉丝点击