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 ...
随机推荐
- windows下使用命令行编译、链接C++源文件
目录 1.流程 2.操作 1.流程 .cpp-->.o-->.exe 分别为 源文件-->中间目标文件-->可执行文件 两个-->的过程分别为编译.链接 p.s.多个 . ...
- [CF429E]Points ans Segments_欧拉回路
Points and Segments 题目链接:www.codeforces.com/contest/429/problem/E 注释:略. 题解: 先离散化. 发现每个位置如果被偶数条线段覆盖的话 ...
- 2019牛客暑期多校训练营(第三场)F Planting Trees 单调队列
F Planting Trees 题目链接 https://ac.nowcoder.com/acm/contest/883/F 题目描述 The semester is finally over an ...
- fiddler笔记:状态面板
Capturing 指示Fiddler是否开启抓包. Process-based Filter 显示Fiddler当前正在捕获的流量进程类型点击面板可以显示进程类型的过滤选项菜单 Automatic ...
- shell习题第14题:
[题目要求] 需求,根据web服务器的访问日志,把一些请求高的ip给拒绝掉,并且每隔半小时把不再发起请求或者请求量很小的ip给解封 假设: 1. 一分钟内请求量高于100次的ip视为不正常的请求 2. ...
- linux mysql 数据库操作导入导出 数据表导出导入
linux mysql 数据库操作导入导出 数据表导出导入 1,数据库导入 mysql -uroot -p show databases; create database newdb; use 数据库 ...
- k8s之自定义指标API部署prometheus
1.自定义指标-prometheus node_exporter是agent;PromQL相当于sql语句来查询数据; k8s-prometheus-adapter:prometheus是不能直接解析 ...
- JVM描述符标识字符含义
标识字符 含义 B byte C char D double F float I int J long S short Z boolean V void L 对象类型,如Ljava/lang/Obje ...
- 怎么处理sqlserver2017部署在winowsDocker上时区无法修改成功的方式,并且可以多创建新的容器调用简单的方式直接使用!
在创建该容器的时候我们执行的语句中添加了一个 从图中所看到的内容,上海时区,按照正常流程一般都是可疑正常执行的,但最后事情并不是我们所想的那么简单. 我们进入对应的容器里面 ,执行语句之后查找对应的文 ...
- [NOIP10.4模拟赛]3.z题解--思维
题目链接: 咕咕 闲扯: 哈哈这道T3考场上又敲了5个namespace,300+行,有了前车之鉴还对拍过,本以为子任务分稳了 结果只有30分哈哈,明明用极限数据对拍过不知怎么回事最后数据又是读不全, ...