Codeforces 1228D. Complete Tripartite
不妨设 $1$ 号点在集合 $1$ 里
那么对于其他点,有且只有所有和 $1$ 没有边的点都在集合 $1$ 里
考虑不在集合 $1$ 的任意一个点 $x$ ,不妨设它在集合 $2$ 里
那么所有不在集合 $1$ 的,和 $x$ 没有边的点都在集合 $2$ 里,剩下的点都一定在集合 $3$ 里
所以集合划分完毕,然后就是判断合法性了,特判当然是越多越好啦!
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=6e5+;
int n,m,bel[N],cnt[];
int fir[N],from[N<<],to[N<<],cntt;
inline void add(int a,int b) { from[++cntt]=fir[a]; fir[a]=cntt; to[cntt]=b; }
int fa[N];
inline int find(int x) { return x!=fa[x] ? fa[x]=find(fa[x]) : x; }
int main()
{
n=read(),m=read(); int a,b;
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=m;i++)
{
a=read(),b=read();
add(a,b); add(b,a); fa[find(a)]=find(b);
}
bel[]=; cnt[]++;
for(int i=;i<=n;i++)
{
bool GG=;
for(int j=fir[i];j;j=from[j])
{
int &v=to[j]; if(v==) GG=;
}
if(!GG&&!bel[i]) bel[i]=,cnt[]++;
}
int x=;
for(int i=fir[];i;i=from[i])
{
x=to[i]; break;
}
bel[x]=; cnt[]++;
for(int i=;i<=n;i++)
{
bool GG=; if(bel[i]) continue;
for(int j=fir[i];j;j=from[j])
{
int &v=to[j]; if(v==x) GG=;
}
if(!GG) bel[i]=,cnt[]++;
else bel[i]=,cnt[]++;
}
for(int i=;i<=;i++) if(!cnt[i]) { printf("-1\n"); return ; }
if(cnt[]*cnt[]+cnt[]*cnt[]+cnt[]*cnt[]!=m) { printf("-1\n"); return ; }
for(int i=;i<=n;i++)
{
if(find(i)!=find()) { printf("-1\n"); return ; }
int tot=,res=; if(!bel[i]) { printf("-1\n"); return ; }
for(int j=fir[i];j;j=from[j])
{
int &v=to[j]; if(bel[v]==bel[i]) { printf("-1\n"); return ; }
tot++;
}
for(int j=;j<=;j++) if(bel[i]!=j) res+=cnt[j];
if(tot!=res) { printf("-1\n"); return ; }
}
for(int i=;i<=n;i++) printf("%d ",bel[i]);
puts(""); return ;
}
Codeforces 1228D. Complete Tripartite的更多相关文章
- codeforces 372 Complete the Word(双指针)
codeforces 372 Complete the Word(双指针) 题链 题意:给出一个字符串,其中'?'代表这个字符是可变的,要求一个连续的26位长的串,其中每个字母都只出现一次 #incl ...
- Complete Tripartite
D - Complete Tripartite 思路:这个题是个染色问题.理解题意就差不多写出来一半了.开始的时候还想用离散化来储存每个点的状态,即它连接的点有哪些,但很无奈,点太多了,long lo ...
- Codeforces Round #589 (Div. 2) D. Complete Tripartite(染色)
链接: https://codeforces.com/contest/1228/problem/D 题意: You have a simple undirected graph consisting ...
- 【Codeforces Round #589 (Div. 2) D】Complete Tripartite
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 其实这道题感觉有点狗. 思路大概是这样 先让所有的点都在1集合中. 然后随便选一个点x,访问它的出度y 显然tag[y]=2 因为和他相连了嘛 ...
- Codeforces Round #589 (Div. 2) D. Complete Tripartite(模拟)
题意:给你n个点 和 m条边 问是否可以分成三个集合 使得任意两个集合之间的任意两个点都有边 思路:对于其中一个集合v1 我们考虑其中的点1 假设点u和1无边 那么我们可以得到 u一定和点1在一个集合 ...
- Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 716B Complete the Word
题目链接:http://codeforces.com/problemset/problem/716/B 题目大意: 给出一个字符串,判断其是否存在一个子串(满足:包含26个英文字母且不重复,字串中有‘ ...
- CodeForces 715B Complete The Graph 特殊的dijkstra
Complete The Graph 题解: 比较特殊的dij的题目. dis[x][y] 代表的是用了x条特殊边, y点的距离是多少. 然后我们通过dij更新dis数组. 然后在跑的时候,把特殊边都 ...
- Codeforces 1182D Complete Mirror [树哈希]
Codeforces 中考考完之后第一个AC,纪念一下qwq 思路 简单理解一下题之后就可以发现其实就是要求一个点,使得把它提为根之后整棵树显得非常对称. 很容易想到树哈希来判结构是否相同,而且由于只 ...
随机推荐
- 线程系列1--Java创建线程的几种方式及源码分析
线程--创建线程的几种方式及源码分析 开始整理下线程的知识,感觉这块一直是盲区,工作中这些东西一直没有实际使用过,感觉也只是停留在初步的认识.前段时间一个内推的面试被问到,感觉一脸懵逼.面试官说,我的 ...
- springboot 项目部署后 404的问题
是因为打包的时候,没有把webapp打包进去 pom.xml 在build 里加入下面的依赖即可 <!-- resources插件,在打jar包时可以将webapp目录下的文件进行打包 --&g ...
- Spring Boot学习笔记(1)
@SpringBootApplication用于注解Spring启动类,如下所示 @SpringBootApplication public class Application { public st ...
- java中线程状态
刚开始接触java时,就觉得多线程是一个障碍,不容易理解,当时选择了跳过,不过工作一段时间后,发现这块还是需要深入研究一下的,及时平时工作中不使用多线程,但一定会使用web容器,比如tomcat,也是 ...
- koa 基础(十八)es6中的类、静态方法、继承
1.app.js /** * es6中的类.静态方法.继承 */ // 定义Person类 class Person { constructor(name, age) { /*类的构造函数,实例化的时 ...
- windows驱动程序
官网文档 https://docs.microsoft.com/zh-cn/windows-hardware/drivers/
- 大数相乘 java
<pre name="code" class="java">package bigMultiply; import java.math.BigInt ...
- Mac下WebStorm直接运行ReactNative项目
1.首先点击WebStorm右上方的下拉箭头弹出的Edit Configurations…. 2.然后会进入一个配置页面.点击左上方的+.在弹出的列表中选中npm. 3.在右边的配置框中,先选择Com ...
- Windows下Elasticsearch安装问题处理
按ES官网的安装方法正常安装就行了.可能遇到的其他问题. 1.报jvm.dll不存在. 只需要重新安装JDK过后,会有jdk1.8.0_73目录和jre1.8.0_73目录.因为java就喜欢玩这种“ ...
- 阶段3 3.SpringMVC·_07.SSM整合案例_06.ssm整合之编写MyBatis框架
需要先搭建Mybits的环境. 用Mybits的注解的方式.把两个方法的sql语句写完 SqlMapConfig.xml resources下新建xml文档 把约束粘贴过来 两步操作 环境标签叫做en ...