【Code Force】Round #589 (Div. 2) D、Complete Tripartite
大致题意
把一个图分成三块,要求任意两块之间是完全图,块内部没有连线
分析
首先根据块内没有连线可以直接分成两块
假定点1是属于块1的,那么所有与点1连接的点,都不属于块1;反之则是块1的
然后在所有不属于块1的点内随意找一点k,设定其属于块2,那么所有与点k连接的点且不属于块1,则是块3。
块分完了,然后是判断每个块是否满足条件,我通过下面三条来判断
1、每个块都有点
2、每个块内部没有连线,即没有一条线的两个端点在同一个块内
3、每个块内的点的度等于其他两个块的点个数和也等于n减去当前块内的点数
AC Code
(暴力就完事)
#include <bits/stdc++.h>
using namespace std;
#define MAXN 101000
int fa[MAXN]; // 保存了点属于哪个块
int deg[MAXN]; // 保存了点的度
pair<int, int> edge[MAXN * 3];
void solve() {
int n, m;
cin >> n >> m;
int f2 = 2; // f2 用来找块2
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
deg[u]++;
deg[v]++;
edge[i] = {u, v};
if (u == 1) {
fa[v] = 1;
f2 = v;
} else if (v == 1) {
fa[u] = 1;
f2 = u;
}
}
// 找出第三块
for (int i = 0; i < m; ++i) {
if (edge[i].first == f2 && fa[edge[i].second] == 1)
fa[edge[i].second] = 2;
else if (edge[i].second == f2 && fa[edge[i].first] == 1)
fa[edge[i].first] = 2;
}
int cnt[3] = {n, n, n}; // 保存了每个块内点的个数
// 需要变成完全图需要多少条边
for (int i = 0; i < n; ++i)
cnt[fa[i + 1]]--;
// 块内的入度是否符合条件
for (int i = 0; i < n; ++i) {
if (deg[i + 1] != cnt[fa[i + 1]]) {
cout << -1 << endl;
return;
}
}
// 每个块是否为空
if (cnt[0] == n || cnt[1] == n || cnt[2] == n) {
cout << -1 << endl;
return;
}
// 内部连线
for (int i = 0; i < m; ++i) {
if (fa[edge[i].first] == fa[edge[i].second]) {
cout << -1 << endl;
return;
}
}
for (int i = 0; i < n - 1; ++i)
cout << fa[i + 1] + 1 << " ";
cout << fa[n] + 1 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long long test_index_for_debug = 1;
char acm_local_for_debug;
while (cin >> acm_local_for_debug) {
cin.putback(acm_local_for_debug);
if (test_index_for_debug > 20) {
throw runtime_error("Check the stdin!!!");
}
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
}
#else
solve();
#endif
return 0;
}
总之一句话,暴力就完事了。反正边不多,我已经懒得优化了
【Code Force】Round #589 (Div. 2) D、Complete Tripartite的更多相关文章
- 【Codeforces】Round #491 (Div. 2) 总结
[Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...
- 【Codeforces】Round #488 (Div. 2) 总结
[Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...
- Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理
Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...
- 【ABAP系列】SAP ABAP WS_DELIVERY_UPDATE 修改数量、过账日期并发货过账
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP WS_DELI ...
- 2.5 【配置环境】多浏览器驱动 (chrome、IE、Firefox)❀
来源:http://blog.csdn.net/huilan_same/article/details/51896672 http://www.cnblogs.com/thinkCoding/p/64 ...
- 关于注释【code templates】,如何导入本地注释文件
关于如何在eclipse.myeclipse导入本地注释文件 [xxx.xml] 请看操作方式 下面是code templates文件的内容 注意 把文件中的 @@@@@@@@@@@@@@@ ...
- Codeforces Round #589 (Div. 2)
目录 Contest Info Solutions A. Distinct Digits B. Filling the Grid C. Primes and Multiplication D. Com ...
- 【日常小记】统计后缀名为.cc、.c、.h的文件数【转】
转自:http://www.cnblogs.com/skynet/archive/2011/03/29/1998970.html 在项目开发时,有时候想知道源码文件中有多少后缀名为.cc..c..h的 ...
- 【树莓派】【网摘】树莓派与XBMC及Kodi、LibreELEC插件(三)
之前的相关文章参考: [树莓派]树莓派与XBMC及Kodi.LibreELEC插件(一) [树莓派]树莓派与XBMC及Kodi.LibreELEC插件(二) [树莓派]树莓派与XBMC及Kodi.Li ...
随机推荐
- ubuntu16.04安装mysql5.6
apt-get install software-properties-commonsudo add-apt-repository 'deb http://archive.ubuntu.com/ubu ...
- CoreGraphic
public func UIGraphicsBeginImageContextWithOptions( size: CGSize, opaque: Bool, _ scale: CGFloat) si ...
- 为什么要用location的hash来传递参数?
分页功能代码实现 <div> <a class="btn" href="#" style="..." @Click.pre ...
- C# InputStream获取后乱码处理
Post推送过来的数据流获取后部分中文出现乱码,晚上找了好多办法,不如朋友鼎力相助,哈哈哈~不说废话了上代码把 旧代码基本是网上普遍写法,字段不长用起来不会有乱码情况,但是传送字段一旦过长,超过byt ...
- (Win10)Java,Maven,Tomcat8.0,Mysql8.0.15安装与环境配置,以及IDEA2019.3使用JDBC连接MySQL、创建JavaEE项目
之前用windows+linux的双系统,最近不怎么舒服就把双系统给卸了,没想到除了问题,导致有linux残余,于是就一狠心重装了电脑,又把Java及其相关的一些东西重新装了回来,还好当初存了网盘链接 ...
- 正式学习MVC 02
1.cookie 继续讲解MVC的内置对象cookie 相对不安全 1)保存cookie public ActionResult Index() { // 设置cookie以及过期时间 Respons ...
- webstorm 提示 "scanning files to index..." 一直不能编译的问题
先说一下我的操作过程吧: 下载公司的vue项目后,要用到webpack打包工具,需要按照package.json安装一些依赖,我使用了镜像后,npm install模块时候生成了一个 node_mod ...
- win10执行Tensorflow,总是会报错“DLL load failed: 找不到指定的模块”的解决方式----终极版方式
win10上运行tensorflow时报错,“DLL load failed: 找不到指定的模块”的解决方式 我只想说,当你们遇到这个问题的时候,以下终极版的方式出来了,非常感谢知乎 leo lv ! ...
- [LeetCode] 1370. Increasing Decreasing String
1. 原题链接:https://leetcode.com/problems/increasing-decreasing-string/ 2. 解题思路 直观的想法是:用有序map<char, i ...
- Xml反序列化记录
1.概述 公司项目遇到一个需要对接webservice的,webservice大部分用的都是xml来传输的,这里记录一下xml反序列化遇到的问题 2.xml工具类 xml序列化: public sta ...