链接:

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. 生产环境中的 redis 是怎么部署的

    redis cluster,10 台机器,5 台机器部署了 redis 主实例,另外 5 台机器部署了 redis 的从实例,每个主实例挂了一个从实例,5 个节点对外提供读写服务,每个节点的读写高峰q ...

  2. 【转帖】MBW内存测试

    MBW内存测试 https://www.cnblogs.com/dongdongwq/p/5431561.html 在测试前,理应了解本机所具备的特点,比如CPU频率.内存频率.内存大小,等等信息. ...

  3. Mac终端 bash和zsh切换方法

    切换到bash chsh -s /bin/bash 切换到zsh chsh -s /bin/zsh 终端重启后生效

  4. 【LOJ】#3051. 「十二省联考 2019」皮配

    LOJ#3051. 「十二省联考 2019」皮配 当时我在考场上觉得这题很不可做... 当然,出了考场后再做,我还是没发现学校和城市是可以分开的,导致我还是不会 事实上,若一个城市投靠了某个阵营,学校 ...

  5. cmd寻找tomcat的命令和删除进程的命令

    netstat -ano | findstr 8080taskkill -f -pid 端口 idea 异常关闭,无法启动Tomcat提示Error running ‘server_web’: Una ...

  6. Scala当中什么是Transformation和 Action,以及它们俩的区别是什么?

    [学习笔记] 一个完整的RDD任务由两部分组成:Transformation和 Action.Transformation用于对RDD的创建,还可以把老的RDD通过Transformation来生成新 ...

  7. Laravel入门

    一.下载Laravel ①github上下载 ②通过composer下载,推荐 第一步,选择你要在哪个目录下载Laravel,打开cmd 第二步,打开https://docs.golaravel.co ...

  8. 优化方法总结以及Adam存在的问题(SGD, Momentum, AdaDelta, Adam, AdamW,LazyAdam)

    优化方法总结以及Adam存在的问题(SGD, Momentum, AdaDelta, Adam, AdamW,LazyAdam) 2019年05月29日 01:07:50 糖葫芦君 阅读数 455更多 ...

  9. js date对象传参获取特定日期的时间戳

    当我们想要通过js获取某一特定时间的时间戳时,会通过给date对象传参再通过getTime函数来获取,传递的参数格式也有不同形式.有些时候,可能会因为自己传入参数的格式不正确而导致date对象inva ...

  10. js组合继承

    //组合继承指的是将原型链和借用构造函数(call.apply)的技术组合到一起,从而发挥二者之长的一种继承模式,//其背后的思路就是使用原型链实现对原型属性和方法的继承://而通过借用构造函数来实现 ...