codeforces 357B Flag Day

Flag Day
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 3
1 2 3
1 4 5
4 6 7
output
1 2 3 3 2 2 1 
input
9 3
3 6 9
2 5 8
1 4 7
output
1 1 1 2 2 2 3 3 3 
input
5 2
4 1 5
3 1 2
output
2 3 1 1 3 
 
       
思维题
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;

const int maxn=111111;

int col[maxn],vis[4];

void solve(int a,int b,int c)
{
    int i;
    memset(vis,0,sizeof(vis));
    vis[col[a]]++;
    vis[col[b]]++;
    vis[col[c]]++;
    for(i=1;i<=3;i++)
    {
        if(!col[a]&&!vis[i])
        {
            col[a]=i;
            vis[i]=1;
        }
    }
    for(i=1;i<=3;i++)
    {
        if(!col[b]&&!vis[i])
        {
            col[b]=i;
            vis[i]=1;
        }
    }
    for(i=1;i<=3;i++)
    {
        if(!col[c]&&!vis[i])
        {
            col[c]=i;
            vis[i]=1;
        }
    }
}


int main()
{
    int n,m;
    int i;
    int a,b,c;
    scanf("%d%d",&n,&m);
    memset(col,0,sizeof(col));
    for(i=0;i<m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        solve(a,b,c);
    }
    for(i=1;i<=n;i++)
    {
        if(i>1)
            cout<<" ";
        cout<<col[i];
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值