zoukankan      html  css  js  c++  java
  • CF 272E Dima and Horses 染色,dfs 难度:2

    http://codeforces.com/problemset/problem/272/E

    把仇恨关系想象为边,

    因为度只能为0,1,2,3,所以有以下几种

    0,1 直接放即可

    2: 有(1,1),(0,2)两种情况,第一种随便放,第二种放0那里

    3:有(1,2),(0,3)两种情况,第一种放1,第二种放0那里

    也就是说,怎样都有解

    dfs寻找即可

    不过一开始觉得这样不靠谱,因为时间复杂度很高,不过没想到过了

    #include <cstdio>
    #include <vector>
    using namespace std;
    const int maxn=3e5+5;
    bool other[maxn];
    vector<int> e[maxn];
    int n,m;
    void print(){
            for(int i=1;i<=n;i++){
                    if(other[i])printf("1");
                    else printf("0");
            }
            puts("");
    }
    void judge(int s){
            int o=0;
            for(int i=0;i<(int)e[s].size();i++){
                    int t=e[s][i];
                    if(other[t]==other[s])o++;
            }
            if(o>1){
                    other[s]=!other[s];
                    for(int i=0;i<(int)e[s].size();i++){
                            int t=e[s][i];
                            if(other[t]==other[s])judge(t);
                    }
            }
    }
    
    int main(){
            scanf("%d%d",&n,&m);
            for(int i=0;i<m;i++){
                    int f,t;
                    scanf("%d%d",&f,&t);
                    e[f].push_back(t);
                    e[t].push_back(f);
            }
            for(int i=1;i<=n;i++){
                    judge(i);
            }
            print();
    
            return 0;
    }
    

      

  • 相关阅读:
    PHP面试题4
    php面试题2
    php基础面试题1
    mysql添加索引命令
    lnmp初步学习知识整理
    代码运行的自由
    Lein droid
    关于Domain Sepcific Lang
    JavaScript倒计时类
    三国小记
  • 原文地址:https://www.cnblogs.com/xuesu/p/4328848.html
Copyright © 2011-2022 走看看