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;
    }
    
  • 相关阅读:
    开发小技巧:移除不用的接口和代码
    打印维护调整整体偏移值
    设置table表格的单元格间距两种方式
    html中测试div、ul和li、table排列多个块
    LODOP常见问题连接(含常见小问答博文)
    常见问答的点击到链接1
    LODOP中打印项水平居中简短问答
    LODOP设置某打印项锁定下边距
    css选择器测试2-用ul和li简单排版
    LODOP打印超文本有边距不居中的情况2
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11620101.html
Copyright © 2011-2022 走看看