传送门

不妨设 $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的更多相关文章

  1. codeforces 372 Complete the Word(双指针)

    codeforces 372 Complete the Word(双指针) 题链 题意:给出一个字符串,其中'?'代表这个字符是可变的,要求一个连续的26位长的串,其中每个字母都只出现一次 #incl ...

  2. Complete Tripartite

    D - Complete Tripartite 思路:这个题是个染色问题.理解题意就差不多写出来一半了.开始的时候还想用离散化来储存每个点的状态,即它连接的点有哪些,但很无奈,点太多了,long lo ...

  3. Codeforces Round #589 (Div. 2) D. Complete Tripartite(染色)

    链接: https://codeforces.com/contest/1228/problem/D 题意: You have a simple undirected graph consisting ...

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

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

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

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

  6. 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 ...

  7. CodeForces 716B Complete the Word

    题目链接:http://codeforces.com/problemset/problem/716/B 题目大意: 给出一个字符串,判断其是否存在一个子串(满足:包含26个英文字母且不重复,字串中有‘ ...

  8. CodeForces 715B Complete The Graph 特殊的dijkstra

    Complete The Graph 题解: 比较特殊的dij的题目. dis[x][y] 代表的是用了x条特殊边, y点的距离是多少. 然后我们通过dij更新dis数组. 然后在跑的时候,把特殊边都 ...

  9. Codeforces 1182D Complete Mirror [树哈希]

    Codeforces 中考考完之后第一个AC,纪念一下qwq 思路 简单理解一下题之后就可以发现其实就是要求一个点,使得把它提为根之后整棵树显得非常对称. 很容易想到树哈希来判结构是否相同,而且由于只 ...

随机推荐

  1. hadoop yarn日志分离

    根据hdfs的auditlog以及fsimage分析,yarn的日志文件占用了10%-20%的rpc请求以及文件量,这对namenode的性能有比较大的影响,特别是当集群规模越来越大,会影响生产业务. ...

  2. TCP->IP输出 之 ip_queue_xmit、ip_build_and_send_pkt、ip_send_unicast_reply

    概述 ip_queue_xmit是ip层提供给tcp层发送回调,大多数tcp发送都会使用这个回调,tcp层使用tcp_transmit_skb封装了tcp头之后,调用该函数,该函数提供了路由查找校验. ...

  3. Ubuntu14.04升级cmake版本的方法

    在Ubuntu14.04用以下命令默认安装的cmake版本为2.8.x,有时我们需要更高版本的cmake,所以需要升级. $ sudo apt-get install cmake 可通过以下命令查询c ...

  4. VNC Viewer配置

    VNC概述 VNC (Virtual Network Computing)是虚拟网络计算机的缩写.VNC 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的.VNC 是在 ...

  5. 文字和符号组合成图 Banner

    springboot 启动 logo.... 文字符号组合成的图. <<< | /\\\ /--\\\ / \\\\ <*| /________\\\\ | ___ | | | ...

  6. leetcode-hard-array-76. Minimum Window Substring -NO

    mycode 不会.. 参考: class Solution(object): def minWindow(self, s, t): """ :type s: str : ...

  7. Hadoop : MapReduce中的Shuffle和Sort分析

    地址 MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Sch ...

  8. CSS中 Padding和Margin两个属性的详细介绍和举例说明

    代码示例: <!doctype html> <html lang="en"> <head> <meta charset="UTF ...

  9. [spring]AOP(切面)编程

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  10. (转)java8实现对象列表去重

    java8实现列表去重,java8的stream和lambda的使用实例 通过普通的方式也可以达到去重的效果,但是借助java8新特性可以很方便的实现列表去重,测试demo如下 实体类: public ...