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;
}
Codeforces Round #589 (Div. 2) D. Complete Tripartite(染色)的更多相关文章
- Codeforces Round #589 (Div. 2) D. Complete Tripartite(模拟)
题意:给你n个点 和 m条边 问是否可以分成三个集合 使得任意两个集合之间的任意两个点都有边 思路:对于其中一个集合v1 我们考虑其中的点1 假设点u和1无边 那么我们可以得到 u一定和点1在一个集合 ...
- 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\) ...
- Codeforces Round #589 (Div. 2)
目录 Contest Info Solutions A. Distinct Digits B. Filling the Grid C. Primes and Multiplication D. Com ...
- Codeforces Round #589 (Div. 2) (e、f没写)
https://codeforces.com/contest/1228/problem/A A. Distinct Digits 超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每 ...
- 【Codeforces Round #589 (Div. 2) D】Complete Tripartite
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 其实这道题感觉有点狗. 思路大概是这样 先让所有的点都在1集合中. 然后随便选一个点x,访问它的出度y 显然tag[y]=2 因为和他相连了嘛 ...
- Codeforces Round 589 (Div. 2) 题解
Is that a kind of fetishism? No, he is objectively a god. 见识了一把 Mcdic 究竟出题有多神. (虽然感觉还是吹过头了) 开了场 Virt ...
- 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 ...
- Codeforces Round #589 (Div. 2) C - Primes and Multiplication(数学, 质数)
链接: https://codeforces.com/contest/1228/problem/C 题意: Let's introduce some definitions that will be ...
- 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 ...
随机推荐
- 如何克服社交恐惧症?zz
zhang Bavol 清华大学核能与新能源技术研究院——核科学与技术/电子爱好者 你说的这个恐惧症我也有一点点,不过现在我是只对那种不是很熟悉的七姑八婆之类的亲戚才会有这种憋屈感觉,对朋友和 ...
- java学习(东软睿道)2019-09-07(预课html基础)《随堂笔记》
1. 文件名称.html 2. 后缀表示文件类型.txt .doc .ppt 3. 标签<html> <head> <body> 显示内容 <font siz ...
- C库函数:scanf、fscanf、printf、fprintf、sprintf、 snprintf
1. scanf 函数原型 int scanf(const char *format, ...); 功能:从标准输入 stdin 读取格式化输入. 2.fscanf 函数原型 int fscanf( ...
- S02_CH01_Hello World实验
S02_CH01_Hello World实验 ZYNQ是一款SOC芯片,在前面第一季的学习当中,我们只是粗略的学习了ZYNQ的PL部分,对于ZYNQ最突出的功能,其内部的双核Cortex-A9内核并未 ...
- MyEclipse优化攻略搜集
1 首先内存设置 不会报讨厌的内存溢出out of memory 和 henp space 在 myeclipse.ini把大小调成一样是因为不让myeclipse频繁的换内存区域的大小. #utf8 ...
- ie/chorme 清除缓存 刷新js,css
1 有时候你发现你刚改过的js 没有用,然后就是你的浏览器 没有清楚缓存,它可能还是保存的之前的 网页文件: chorme 浏览器下(版本:ver 59.0.3071.104(正式版本) (64 位) ...
- LeetCode:184.部门工资最高的员工
题目链接:https://leetcode-cn.com/problems/department-highest-salary/ 题目 Employee 表包含所有员工信息,每个员工有其对应的 Id, ...
- HTML and CSS basis
classes 和 IDs 的不同 class 选择器用于描述一组元素的样式,class 选择器有别于id选择器,class可以在多个元素中使用. HTML 元素 elements 从开始标签(sta ...
- Win Server 2012 配置运行 .net core 环境
今天拿到一台 全新的win 2012 服务器配置服务器环境 记录一下 首先装好IIS 打开服务器管理器 - 添加功能和角色 好 安装完IIS 看一下服务器有没有安装 core的运行环境(全新 ...
- android默认获取敏感权限
1.通过系统签名获取权限 定制系统中,可以通过源码的签名文件对应用进行签名,在应用的AndroidManifest.xml中配置好参数,如图 <manifest xmlns:android=&q ...