CodeForces#207Div.2 - B. Flag Day


初始化所有人都没有跳过舞。

判断一组里面第一个出现的之前跳过舞的人,让其他两个人的颜色为这个跳过舞的人的颜色后两位(在1,2,3之间循环)。若加和完之后大于3,自减3 。若全部人之前都没有跳过舞,直接赋值1,2,3 。




AC代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

int color[100005];

int main() {
    int n, m;
    while(scanf("%d%d", &n, &m)!=EOF) {
        int a, b, c;
        memset(color, 0, sizeof(color));
        for(int i = 1; i <= m; i++) {
            scanf("%d%d%d", &a, &b, &c);
            if(color[a] != 0) {
                color[b] = color[a] + 1;
                color[c] = color[a] + 2;
                if(color[b] > 3)
                    color[b] -= 3;
                if(color[c] > 3)
                    color[c] -= 3;
            }
            else if(color[b] != 0) {
                color[a] = color[b] + 2;
                color[c] = color[b] + 1;
                if(color[a] > 3)
                    color[a] -= 3;
                if(color[c] > 3)
                    color[c] -= 3;
            }
            else if(color[c] != 0) {
                color[a] = color[c] + 1;
                color[b] = color[c] + 2;
                if(color[a] > 3)
                    color[a] -= 3;
                if(color[b] > 3)
                    color[b] -= 3;
            }
            else {
                color[a] = 1;
                color[b] = 2;
                color[c] = 3;
            }
        }
        for(int i = 1; i <= n; i++) {
            if(i == 1)
                printf("%d", color[i]);
            else
                printf(" %d", color[i]);
        }
        printf("\n");
    }
    return 0;
}


简化一下代码量:

#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int c[100005];
int n,m,x,y,z;

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(c,0,sizeof(c));
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(c[x]!=0)
            {
                c[y]=c[x]%3+1;
                c[z]=c[y]%3+1;
            }
            else if(c[y]!=0)
            {
                c[z]=c[y]%3+1;
                c[x]=c[z]%3+1;
            }
            else if(c[z]!=0)
            {
                c[x]=c[z]%3+1;
                c[y]=c[x]%3+1;
            }
            else
            {
                c[x]=1;
                c[y]=2;
                c[z]=3;
            }
        }
        for(int i=1;i<=n;i++)
        printf("%d ",c[i]);puts("");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值