ARC099E. Independence
考虑一个子问题。给定无向图 $G$,如何判断能否将 $G$ 的点集分成两部分 $S$、$T$ 使得 $S$ 和 $T$ 导出的子图都是完全图?
这个问题把我难住了。解法是考虑 $G$ 的补图 $G'$,$G$ 中的完全子图对应于 $G'$ 中的独立集。
$G'$ 的点集能划分为两个独立集等价于 $G'$ 是二分图。
回到原问题。对于补图 $G'$ 的每个连通分量,二分图的两个点集是确定的。于是我们可以通过 DP 算出 $S$ 中可能有几个点。
官方题解:
code
int main() {
int n, m;
scan(n, m);
vv a(n, vi(n));
rep (m) {
int x, y;
scan(x, y);
--x, --y;
a[x][y] = a[y][x] = 1;
}
vv g(n);
rng (i, 0, n) {
rng (j, 0, i) {
if (!a[i][j]) {
g[i].pb(j);
g[j].pb(i);
}
}
}
vi vis(n);
int c1, c2;
vpii num;
function dfs = [&](int u) {
FOR (v, g[u]) {
if (!vis[v]) {
vis[v] = -vis[u];
if (vis[v] == 1) {
++c1;
} else {
++c2;
}
dfs(v);
} else if (vis[v] != -vis[u]) {
println(-1);
exit(0);
}
}
};
rng (i, 0, n) {
if (!vis[i]) {
vis[i] = 1;
c1 = 1, c2 = 0;
dfs(i);
num.eb(c1, c2);
}
}
vi dp(n + 1);
dp[0] = 1;
int limit = 0;
FOR (p, num) {
down (i, limit, 0) {
if (dp[i]) {
dp[i] = 0; // 这里容易错。少了这一句就错了。
dp[i + p.first] = 1;
dp[i + p.second] = 1;
}
}
limit += max(p.first, p.second);
}
int ans = INT_MAX;
rng (i, 0, n + 1) {
if (dp[i]) {
chkmin(ans, i * (i - 1) / 2 + (n - i) * (n - i - 1) / 2);
}
}
println(ans);
return 0;
}
以上实现在 DP 部分采用了滚动数组的技巧,要注意及时清空上一轮的状态。
ARC099E. Independence的更多相关文章
- 控制反转(IOC: Inverse Of Control) & 依赖注入(DI: Independence Inject)
举例:在每天的日常生活中,我们离不开水,电,气.在城市化之前,我们每家每户需要自己去搞定这些东西:自己挖水井取水,自己点煤油灯照明,自己上山砍柴做饭.而城市化之后,人们从这些琐事中解放了出来,城市中出 ...
- Independence独立
Independence refers to the degree to which each test case stands alone. That is, does the success or ...
- [PGM] Bayes Network and Conditional Independence
2 - 1 - Semantics & Factorization 2 - 2 - Reasoning Patterns 2 - 3 - Flow of Probabilistic Influ ...
- AtCoder Regular Contest 099 (ARC099) E - Independence 二分图
原文链接https://www.cnblogs.com/zhouzhendong/p/9224878.html 题目传送门 - ARC099 E - Independence 题意 给定一个有 $n$ ...
- [Bayes] prod: M-H: Independence Sampler for Posterior Sampling
M-H是Metropolis抽样方法的扩展,扩展后可以支持不对称的提议分布. 对于M-H而言,根据候选分布g的不同选择,衍生出了集中不同的变种: (1)Metropolis抽样方法 (2)随机游动Me ...
- GYM 101064 2016 USP Try-outs G. The Declaration of Independence 主席树
G. The Declaration of Independence time limit per test 1 second memory limit per test 256 megabytes ...
- C# 控制反转(IOC: Inverse Of Control) & 依赖注入(DI: Independence Inject)
举例:在每天的日常生活中,我们离不开水,电,气.在城市化之前,我们每家每户需要自己去搞定这些东西:自己挖水井取水,自己点煤油灯照明,自己上山砍柴做饭.而城市化之后,人们从这些琐事中解放了出来,城市中出 ...
- 【线性代数】3-5:独立性,基和维度(Independence,Basis and Dimension)
title: [线性代数]3-5:独立性,基和维度(Independence,Basis and Dimension) categories: Mathematic Linear Algebra ke ...
- 【读书笔记】:MIT线性代数(4):Independence, Basis and Dimension
Independence: The columns of A are independent when the nullspace N (A) contains only the zero vecto ...
随机推荐
- CodeForces 535C Tavas and Karafs —— 二分
题意:给出一个无限长度的等差数列(递增),每次可以让从l开始的m个减少1,如果某个位置已经是0了,那么可以顺延到下一位减少1,这样的操作最多t次,问t次操作以后从l开始的最长0序列的最大右边界r是多少 ...
- 使用 suspend 和 resume 暂停和恢复线程
suspend 和 resume 的使用 在 Thread 类中有这样两个方法:suspend 和 resume,这两个方法是成对出现的. suspend() 方法的作用是将一个线程挂起(暂停), r ...
- LC 641. Design Circular Deque
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- eclipse syso 自动补全设置方法
eclipse syso 自动补全设置方法 转 https://blog.csdn.net/sinat_23536373/article/details/76512390 经常遇到打”sys ...
- Android:通过systrace进行性能分析
一.Systrace 简介 Systrace 允许您在系统级别(如SurfaceFlinger.WindowManagerService等Framework部分关键模块.服务.View系统等)收集和检 ...
- NLP - Log-linear Models
1.The Language Modeling Problem 现在抛开我们之前讲的马尔科夫模型的假设,对于一门语言的定义,肯定不能简单依赖于每个单词的前两个单词,这是常识.比如英语中 ...
- pip安装django出错 Could not install packages due to an EnvironmentError: [Errno 13]
pip install django 下载安装Django报错, 按照提示的建议改为 pip install --user django 安装完成
- Latex新手学习
1.Latex 套装下载安装 http://www.ctex.org/CTeXDownload 选择镜像稳定版 下载安装后 2.一个简单的例子 (1)打开WinEdt (2)新建一个文档 \docum ...
- 1、vinc = vict 胜、征服
- launchImage设置后在启动时无法显示
有人问我他的APP设置了启动页,然后居然不显示.....我觉得应该不可能啊,然后我自己再次实现了一下设置启动页,这个问题好像以前从来没有注意过,也没有很深刻的掌握APP启动页的设置和注意事项,今天遇到 ...