传送门

不妨设 $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. From 7.22 To 7.28

    From 7.22 To 7.28 大纲 竞赛 我们好像要跟队爷考试... 考试的时候做题吧 学科 还是跟之前一样吧, 完型和阅读几乎没做过... 运动 踢足球!!!!!! 可惜bb他们去上海了... ...

  2. svn 巧用,如果遇到问题解决不了,而上一个版本可以正常使用,则可以查记录

    svn 巧用,如果遇到问题解决不了 1.svn上一个版本可以正常使用的情况下 2.查看历史 3. 丢失的代码复制回来即可

  3. oracle性能诊断sql

    --1.阻塞及等待事件信息查询-- 查询所有会话的状态.等待类型及当前正在执行的SQL脚本select t.SID, t.SERIAL#, t.Status, t.Action, t.Event, t ...

  4. koa 路由、视图模块化(二)

    1.项目目录 2.路由 根目录/routes/index.js -- 首页 const router = require('koa-router')(); router.get('/', async ...

  5. java 百度地图判断两点距离2

    package baiduApi; public class BaiDuMap { static double DEF_PI = 3.14159265359; // PI static double ...

  6. jxbrowser 监听所有网络请求 jxbrowser 系列教程2

    原文:https://blog.csdn.net/shuaizai88/article/details/73649322

  7. TensorFlow 学习(3)——MNIST机器学习入门

    通过对MNIST的学习,对TensorFlow和机器学习快速上手. MNIST:手写数字识别数据集 MNIST数据集 60000行的训练数据集 和 10000行测试集 每张图片是一个28*28的像素图 ...

  8. Java爬虫爬取京东商品信息

    以下内容转载于<https://www.cnblogs.com/zhuangbiing/p/9194994.html>,在此仅供学习借鉴只用. Maven地址 <dependency ...

  9. 阶段3 3.SpringMVC·_07.SSM整合案例_07.ssm整合之编写MyBatis框架测试保存的方法

    再写一个测试的方法,测试save保存的方法 需要提交事务才能保存到数据库

  10. 《Neural Networks and Deep Learning》课程笔记

    Lesson 1 Neural Network and Deep Learning 这篇文章其实是 Coursera 上吴恩达老师的深度学习专业课程的第一门课程的课程笔记. 参考了其他人的笔记继续归纳 ...