cf1228 D Complete Tripartite(哈希)
题意:
无向简单图,无自环,无重边,n个点,m条边,请你将这n个点分为3个互相没有交集的集合。并且满足以下条件:
1.同一个集合中的任意两点之间没有边。
2.每个点都要与除了它这个集合以外的所有点相连。
没有答案就输出-1,多种答案输出任意一种。
思路:
用hash值代表与一个点相连的 都有哪些点,
如果出现三种hash值就没有答案;
如果有一个点的度为0,也没有答案;
根据哈希值,点被分为三份,如果每一份的点数+它们所连的点的点数 != n,也没有答案;
如果同一条边相连的两个点的哈希值相同,也没有答案。
以上条件都不满足的话,就是答案,输出即可。
代码:
#include <stdio.h>
#include <queue>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <set>
#include <iostream>
using namespace std;
typedef long long int ll;
const int maxn = 1e5 + ;
const int inf = ;
const ll mod = 1e9 + ;
const ll seed = ;
vector<int>mp[maxn];
map<ll,int>s;
ll a[maxn];
int p[maxn];
ll _hash[maxn];
int main()
{
int n,m,x,y,num;
while(scanf("%d%d",&n,&m) != EOF){
s.clear();
num = ;
for(int i = ;i <= m;i++){
scanf("%d%d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
for(int i = ;i <= n;i++){
sort(mp[i].begin(),mp[i].end());
ll temp = ;
for(int j = ;j < mp[i].size();j++){
temp = ((temp * seed) % mod + mp[i][j]) % mod;
}
_hash[i] = temp;
if(s[temp] == ){
num++;
a[num] = temp;
p[num] = i;
} s[temp]++;
if(temp == || num > ){
printf("-1\n");
return ;
}
} for(int i = ;i <= ;i++){
if(s[a[i]] + mp[p[i]].size() != n){
printf("-1\n");
return ;
}
} for(int i = ;i <= n;i++){
for(int j = ;j < mp[i].size();j++){
x = i;
y = mp[i][j];
if(_hash[x] == _hash[y]){
printf("-1\n");
return ;
}
}
} for(int i = ;i <= n;i++){
if(_hash[i] == a[])
printf("1 ");
else if(_hash[i] == a[])
printf("2 ");
else
printf("3 ");
}
puts("");
}
return ;
}
cf1228 D Complete Tripartite(哈希)的更多相关文章
- Complete Tripartite
D - Complete Tripartite 思路:这个题是个染色问题.理解题意就差不多写出来一半了.开始的时候还想用离散化来储存每个点的状态,即它连接的点有哪些,但很无奈,点太多了,long lo ...
- CF1228D Complete Tripartite
思路: 任选一点a,和a没有边直接相连的点一定和a在同一个集合,由此构造得到一个集合A.用类似的方法再构造一个集合B,并将剩下的点放在集合C中,就得到了三个集合A,B,C.再检查A,B,C是否符合要求 ...
- CF #589 (Div. 2) D. Complete Tripartite 构造
这个 D 还是十分友好的~ 你发现这 $3$ 个集合形成了一个环的关系,所以随意调换顺序是无所谓的. 然后随便让 $1$ 个点成为第 $2$ 集合,那么不与这个点连边的一定也属于第二集合. 然后再随便 ...
- Codeforces Round #589 (Div. 2) D. Complete Tripartite(染色)
链接: https://codeforces.com/contest/1228/problem/D 题意: You have a simple undirected graph consisting ...
- Codeforces 1228D. Complete Tripartite
传送门 不妨设 $1$ 号点在集合 $1$ 里 那么对于其他点,有且只有所有和 $1$ 没有边的点都在集合 $1$ 里 考虑不在集合 $1$ 的任意一个点 $x$ ,不妨设它在集合 $2$ 里 那么所 ...
- 【Codeforces Round #589 (Div. 2) D】Complete Tripartite
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 其实这道题感觉有点狗. 思路大概是这样 先让所有的点都在1集合中. 然后随便选一个点x,访问它的出度y 显然tag[y]=2 因为和他相连了嘛 ...
- 【Code Force】Round #589 (Div. 2) D、Complete Tripartite
题目链接 大致题意 把一个图分成三块,要求任意两块之间是完全图,块内部没有连线 分析 首先根据块内没有连线可以直接分成两块 假定点1是属于块1的,那么所有与点1连接的点,都不属于块1:反之则是块1的 ...
- D - Complete Tripartite
三分图染色 链接:https://codeforces.com/contest/1228/problem/D 三分图染色步骤:First 首先找一个点1作为集合A中的点,再找到与1相连的一个点设为2, ...
- 【题解】CF1228D Complete Tripartite
Link 题目大意:给定一个无向图,将它划分为三个点集,要求在一个点集中的点没有边相连,且颜色相同,不同集合中的点互相有边相连. \(\text{Solution:}\) 我们发现,与一个点之间没有边 ...
随机推荐
- shell教程——bash入门
创建shell文件 vim test.sh 写内容 #!/bin/bash echo "Hello World !" 使脚本具有执行权限 chmod +x ./test.sh 执行 ...
- oracle的存储过程和函数有什么区别?
Oracle中的函数与存储过程的区别: A:函数必须有返回值,而过程没有. B:函数可以单独执行.而过程必须通过execute执行. C:函数可以嵌入到SQL语句中执行.而过程不行. 其实我 ...
- WARN No appenders could be found for logger 。。。。
对于类似与标题的警告信息,一般来说是环境在没有加载log4j的配置文件之前就读取了log4j的包. 解决方法就是先加载log4j的配置文件,然后再加载log4j的包. 另一个解决方案就是移除log4j ...
- Java笔记--异常
1.异常分为两类: --1)Error:Java虚拟机无法解决的严重问题(例如资源耗尽等): --2)Exception:其他编程错误或偶然的外在因素导致的一般性问题(例如空指针异常.读取的文件不存在 ...
- Scala 线性化规则和 super 操作
如果一个类有多个父类,且父类的有相同的函数 f,在子类和父类中调用 super.f 都是按从右到左的调用函数的顺序. 这个规则名为:Linearization Rules 如下的代码 trait Ba ...
- mybatis-plus 乐观锁
参见:https://mp.baomidou.com/guide/optimistic-locker-plugin.html#%E4%B8%BB%E8%A6%81%E9%80%82%E7%94%A8% ...
- grep -w ,grep -e的使用
1.grep -w -wDoes a word search.grep -w用于字符串精确匹配若文件中的内容包括如下:262 a326226如果 grep ‘26’ file,结果是三行全部都被显示若 ...
- Spring Boot2(003):简要回顾“HelloWorld” web 工程
1.注解: @RestController 和 @RequestMapping HelloWorldExample 中的第1个注解 @RestController 是一个被熟知的原型注解(stereo ...
- cf749 D. Leaving Auction
#include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL long long #define N 200005 #de ...
- Vulkan SDK 之 DrawCube
Waiting for a Swapchain Buffer Beginning the Render Pass Bind the Pipeline Bind the Descriptor Sets ...