链接:

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;
}

Codeforces Round #589 (Div. 2) D. Complete Tripartite(染色)的更多相关文章

  1. Codeforces Round #589 (Div. 2) D. Complete Tripartite(模拟)

    题意:给你n个点 和 m条边 问是否可以分成三个集合 使得任意两个集合之间的任意两个点都有边 思路:对于其中一个集合v1 我们考虑其中的点1 假设点u和1无边 那么我们可以得到 u一定和点1在一个集合 ...

  2. Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理

    Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...

  3. Codeforces Round #589 (Div. 2)

    目录 Contest Info Solutions A. Distinct Digits B. Filling the Grid C. Primes and Multiplication D. Com ...

  4. Codeforces Round #589 (Div. 2) (e、f没写)

    https://codeforces.com/contest/1228/problem/A A. Distinct Digits 超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每 ...

  5. 【Codeforces Round #589 (Div. 2) D】Complete Tripartite

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 其实这道题感觉有点狗. 思路大概是这样 先让所有的点都在1集合中. 然后随便选一个点x,访问它的出度y 显然tag[y]=2 因为和他相连了嘛 ...

  6. Codeforces Round 589 (Div. 2) 题解

    Is that a kind of fetishism? No, he is objectively a god. 见识了一把 Mcdic 究竟出题有多神. (虽然感觉还是吹过头了) 开了场 Virt ...

  7. Codeforces Round #589 (Div. 2) E. Another Filling the Grid(DP, 组合数学)

    链接: https://codeforces.com/contest/1228/problem/E 题意: You have n×n square grid and an integer k. Put ...

  8. Codeforces Round #589 (Div. 2) C - Primes and Multiplication(数学, 质数)

    链接: https://codeforces.com/contest/1228/problem/C 题意: Let's introduce some definitions that will be ...

  9. Codeforces Round #589 (Div. 2) B. Filling the Grid

    链接: https://codeforces.com/contest/1228/problem/B 题意: Suppose there is a h×w grid consisting of empt ...

随机推荐

  1. [转帖]centos7设置CPU的运行频率为performance

    centos7设置CPU的运行频率为performance http://www.512873.com/archives/612.html Publish: March 6, 2019 Categor ...

  2. Cacls和ICacls

    解释:  Cacls:显示或修改文件的访问控制列表(ACL) ICACLS:显示或修改自由访问控制表(Dacl) 上指定的文件,并指定目录中的文件应用于存储的 Dacl. 总结:显示或修改文件访问控制 ...

  3. Python基础学习路径

    1. Python数据结构 1. 基本数据类型(整数.小数.字符) 1.基本数据类型有5种:int bool float complex long 2.每一种数据类型该如何定义 3.数据类型之间可以强 ...

  4. Python学习笔记:格式化输出

    %d digit%s string%f float程序运用:name = input("please input your name:")age = int(input(" ...

  5. 如何找到程序的真正入口mainCRTStartup

    相信大家都知道以为程序的入口为main函数,但是程序的真正的入口不是main而是mainCRTStartup,那么我们如何找到他的地址呢? 先用第一种方法,就是直接代码显示 #include<s ...

  6. jquery tablesorter 动态加载数据时,排序。过滤失效解决方案

    解决方案:重置更新: $("table").trigger("update"); 1 官方 ajax表格数据添加实例: $(document).ready(fu ...

  7. MySQL SELECT语法(四)UNION语法详解

    源自MySQL 5.7 官方手册:13.2.9.3 UNION Syntax 一.UNION语法 UNION用于将多个SELECT语句的结果合并到一个结果集中. SELECT ... UNION [A ...

  8. NodeJS express框架的使用

    首先,可以通过npm或者淘宝镜像cnpm全局安装epress框架,这里不具体说了 npm install -g expressnpm install -g express-generator 新建一个 ...

  9. for in和for of的区别

    for in:一般用于遍历普通对象(即没有部署Iterator接口),遍历的是属性. for of:ES6新增的遍历方式,能遍历大部分的类型,遍历的是值.for...of 允许你遍历 Arrays(数 ...

  10. SpringBoot实现定时器定时处理任务

    最近在项目中遇到了一个问题, 对于新建的活动, 活动设置了开始时间和结束时间, 也就是数据库中的一个状态码的改变而已. 但是,这里就有问题了, 如何去实现到时间更改活动状态呢? 1. 刚开始的时候,我 ...