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 思路 简单理解一下题之后就可以发现其实就是要求一个点,使得把它提为根之后整棵树显得非常对称. 很容易想到树哈希来判结构是否相同,而且由于只 ...
随机推荐
- typedef void(*Fun) (void)是什么意思 函数指针(回调函数) 和函数对象总结
https://blog.csdn.net/FreeApe/article/details/49124043 bool (*pf)(const string &,const string &a ...
- 【黑马JavaSE】1_1_9_流程控制:顺序结构、判断结构、选择结构、循环结构
文章目录 1_1_9_01_ 流程控制概述 1_1_9_01_ 顺序结构 1_1_9_ 判断结构 1_1_9_02_ 判断语句1--if 1_1_9_03_ 判断语句2--if...else 1_1_ ...
- pandas.Series.value_counts
pandas.Series.value_counts Series.value_counts(normalize=False, sort=True, ascending=False, bins=Non ...
- anroid学习笔记(1)
大概是2个月前,报名了慕课的android就业班课程. 算是补全了当初博客分类的最初设计. 安卓和前端比较: 1,java在安卓开发中的作用,现在我的认识是和JavaScript在前端web开发中有很 ...
- 查询出与jack互为好友的人名字
建表 /* Navicat MySQL Data Transfer Source Server : connect1 Source Server Version : 50611 Source Host ...
- js前台传数组,java后台接收转list,前后台用正则校验
前台,传参数时,将数组对象转换成json串,后台java收到后用 JSONArray.fromObject 转成集合. 前台js:var params = {"FileNameList&qu ...
- qt liunx 安装命令
qt SDK : apt-get install qt-sdkqt 安装 : apt-get install qtcreator文档安装 : cmake kdelibs5-data subversio ...
- centOS 开启端口
生产环境禁止关闭防火墙,只能开端口 [root@BetaD home]# firewall-cmd --add-port=/tcp --permanent [root@BetaD home]# fir ...
- Python之数组
前言 由于Python的数据结构较为灵活,长期使用java编程的我,有时候可能就搞混淆了.此时,记录一些骚操作. 不定长二维数组 array = [4,65,46,57,5]; array2d = [ ...
- 关于SVN的405错误Server sent unexpected return value (405 Method Not Allowed)的解决办法
一大早上捣鼓项目提交的时候出现这个错误: svn:server sent unexpected return value 405 method not allowed 百度了很多解决办法都没有解决,看 ...