zoukankan      html  css  js  c++  java
  • Codeforces Round #589 (Div. 2) D. Complete Tripartite(染色)

    链接:

    https://codeforces.com/contest/1228/problem/D

    题意:

    You have a simple undirected graph consisting of n vertices and m edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected.

    Let's make a definition.

    Let v1 and v2 be two some nonempty subsets of vertices that do not intersect. Let f(v1,v2) be true if and only if all the conditions are satisfied:

    There are no edges with both endpoints in vertex set v1.
    There are no edges with both endpoints in vertex set v2.
    For every two vertices x and y such that x is in v1 and y is in v2, there is an edge between x and y.
    Create three vertex sets (v1, v2, v3) which satisfy the conditions below;

    All vertex sets should not be empty.
    Each vertex should be assigned to only one vertex set.
    f(v1,v2), f(v2,v3), f(v3,v1) are all true.
    Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex.

    思路:

    先染色, 然后根据度数判断是否满足每个都联通.

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 1e5+10;
    
    vector<int> G[MAXN];
    int col[MAXN], deg[MAXN];
    int n, m;
    
    int main()
    {
        cin >> n >> m;
        int u, v;
        for (int i = 1;i <= m;i++)
        {
            cin >> u >> v;
            G[u].push_back(v);
            G[v].push_back(u);
            deg[u]++;
            deg[v]++;
        }
        for (int i = 1;i <= n;i++)
            col[i] = 1;
        for (int i = 1;i <= n;i++)
        {
            if (col[i] == 1)
            {
                for (int j = 0; j < G[i].size(); ++j)
                {
                    if (col[G[i][j]] == 1)
                        col[G[i][j]] = 2;
                }
            }
        }
        for (int i = 1;i <= n;i++)
        {
            if (col[i] == 2)
            {
                for (int j = 0; j < G[i].size(); ++j)
                {
                    if (col[G[i][j]] == 2)
                        col[G[i][j]] = 3;
                }
            }
        }
        vector<int> Num[4];
        for (int i = 1;i <= n;i++)
            Num[col[i]].push_back(i);
        if (Num[3].size() == 0 || Num[2].size() == 0 || Num[1].size() == 0)
        {
            puts("-1");
            return 0;
        }
        bool flag = true;
        for (int i = 1;i <= n;i++)
        {
            int sum = 0;
            for (int j = 1;j <= 3;j++)
            {
                if (j == col[i])
                    continue;
                sum += Num[j].size();
            }
            if (deg[i] != sum)
            {
                flag = false;
                break;
            }
        }
        if (!flag)
            puts("-1");
        else
        {
            for (int i = 1;i <= n;i++)
                cout << col[i] << ' ';
        }
        puts("");
    
        return 0;
    }
    
  • 相关阅读:
    (3)C++复合类型
    (2)C++基本类型
    (7)js调试
    Oracle语句优先级
    oracle排序问题
    jsp四大对象
    postgresql时间加减计算
    全角空格,跟汉字一样宽
    bzoj1433 [ZJOI2009]假期的宿舍 最大流
    BZOJ 1264 AHOI2006 基因匹配Match 动态规划+树状数组
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11620101.html
Copyright © 2011-2022 走看看