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 ...
随机推荐
- Intel Wireless AC9560 160MHZ 联网黄色感叹号的问题
Intel Wireless 开始支持5G设备了,AC9560 160MHZ可以工作在5G模式了.新入的设备驱动程序升级到了最新版本,发现联网出现“黄色感叹号”,DHCP的IP地址获取正常,从无线路由 ...
- 【luoguP1955 】[NOI2015]程序自动分析--普通并查集
题目描述 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3...代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变 ...
- @Value和@PropertySource实现*.properties配置文件读取过程和实现原理
@Value和@PropertySource实现*.properties 配置文件读取过程和实现原理 1 配置使用步骤 (1)右击resource目录添加*.prooerties配置文件
- From 7.29 To 8.4
From 7.29 To 8.4 大纲 英语按时背 做点思维题 可能还有时间学点东西, 这周我也不知道应该干什么 7.29 上午考试, 终于有一回不是自闭的考试了 题目比较简单, 就不说了 7.30 ...
- 1.3 JAVA规范以及基础语法(if条件和循环)
一.规范以及运算符 1.命名规则 类名大驼峰规则方法名.变量名小驼峰原则常量大写.下划线分开见名释义.不与关键字冲突 关键字链接:https://www.runoob.com/java/java-ba ...
- async for的使用
import random import asyncio async def random_number_gen(delay, start, end): while True: yield rando ...
- 预处理、const、static、sizeof-说明内联函数使用的场合
1:首先使用inline函数可以完全取代表达式形式的宏定义. 内联函数在C++类中的应用最广的应该是用来定义存取函数.我们定义的类中一般会把数据成员定义成私有的或者保护的,这样,外界就不能直接读写我们 ...
- 苹果电脑(Macbook Pro)开机后没有声音的解决
有时候 Mac 从睡眠状态恢复之后没有声音,这是 Mac OS X 系统的一个 Bug.这是因为 Mac OS X 的核心音频守护进程「coreaudiod」出了问题,虽然简单的重启电脑就能解决,但是 ...
- pytorch-LeNet网络
LeNet网络的结构 输入的32x32x1的单通道图片, 第一层网络: 3x3x1x6的卷积层,步长为1, padding = 1, 经过2x2的池化操作 第二层网络: 5x5x6x16的卷积层, 步 ...
- 如何在uboot下列出使用的设备树信息?
答: 使用fdt命令 1. fdt addr <fdt addr> (将设备树加载到fdt addr指定的位置,如tftpboot 0x80000000 my.dtb,那么fdt add ...