惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
博客园_首页
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
V
Visual Studio Blog
Jina AI
Jina AI
N
Netflix TechBlog - Medium
量子位
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
InfoQ
J
Java Code Geeks
T
Tailwind CSS Blog
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
腾讯CDC

Shiroha白羽的博客

Golang 踩坑 —— interface 为参数的时候传 nil 指针 Codeforces Round 925 (Div. 3) Codeforces Round 924 (Div. 2) Codeforces Round 923 (Div. 3) Codeforces Round 922 (Div. 2) Codeforces Round 921 (Div. 2) Educational Codeforces Round 161 (Rated for Div. 2) Codeforces Round 920 (Div. 3) Codeforces Round 919 (Div. 2) Hello 2024 Good Bye 2023 Codeforces Round 918 (Div. 4) 个人备份的常用 macOS 清理命令 Codeforces Round 917 (Div. 2) Pinely Round 3 (Div. 1 + Div. 2) Educational Codeforces Round 160 (Rated for Div. 2) Codeforces Round 915 (Div. 2) Codeforces Round 914 (Div. 2) Codeforces Round 913 (Div. 3) Educational Codeforces Round 159 (Rated for Div. 2) Codeforces Round 912 (Div. 2) Codeforces Round 911 (Div. 2) CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!) Educational Codeforces Round 158 (Rated for Div. 2) Codeforces Round 910 (Div. 2) Codeforces Round 909 (Div. 3) Codeforces Round 908 (Div. 2) Educational Codeforces Round 157 (Rated for Div. 2) C++自定义的字面量 Codeforces Round 907 (Div. 2)
Codeforces Round#589 (Div. 2) D、Complete Tripartite
Shiroha · 2019-09-30 · via Shiroha白羽的博客

题目链接

大致题意

把一个图分成三块,要求任意两块之间是完全图,块内部没有连线

分析

首先根据块内没有连线可以直接分成两块
假定点1是属于块1的,那么所有与点1连接的点,都不属于块1;反之则是块1的
然后在所有不属于块1的点内随意找一点k,设定其属于块2,那么所有与点k连接的点且不属于块1,则是块3。

块分完了,然后是判断每个块是否满足条件,我通过下面三条来判断

1、每个块都有点
2、每个块内部没有连线,即没有一条线的两个端点在同一个块内
3、每个块内的点的度等于其他两个块的点个数和也等于n减去当前块内的点数

AC Code

(暴力就完事)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#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;
}

总之一句话,暴力就完事了。反正边不多,我已经懒得优化了