考虑一个子问题。给定无向图 $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的更多相关文章

  1. 控制反转(IOC: Inverse Of Control) & 依赖注入(DI: Independence Inject)

    举例:在每天的日常生活中,我们离不开水,电,气.在城市化之前,我们每家每户需要自己去搞定这些东西:自己挖水井取水,自己点煤油灯照明,自己上山砍柴做饭.而城市化之后,人们从这些琐事中解放了出来,城市中出 ...

  2. Independence独立

    Independence refers to the degree to which each test case stands alone. That is, does the success or ...

  3. [PGM] Bayes Network and Conditional Independence

    2 - 1 - Semantics & Factorization 2 - 2 - Reasoning Patterns 2 - 3 - Flow of Probabilistic Influ ...

  4. AtCoder Regular Contest 099 (ARC099) E - Independence 二分图

    原文链接https://www.cnblogs.com/zhouzhendong/p/9224878.html 题目传送门 - ARC099 E - Independence 题意 给定一个有 $n$ ...

  5. [Bayes] prod: M-H: Independence Sampler for Posterior Sampling

    M-H是Metropolis抽样方法的扩展,扩展后可以支持不对称的提议分布. 对于M-H而言,根据候选分布g的不同选择,衍生出了集中不同的变种: (1)Metropolis抽样方法 (2)随机游动Me ...

  6. 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 ...

  7. C# 控制反转(IOC: Inverse Of Control) & 依赖注入(DI: Independence Inject)

    举例:在每天的日常生活中,我们离不开水,电,气.在城市化之前,我们每家每户需要自己去搞定这些东西:自己挖水井取水,自己点煤油灯照明,自己上山砍柴做饭.而城市化之后,人们从这些琐事中解放了出来,城市中出 ...

  8. 【线性代数】3-5:独立性,基和维度(Independence,Basis and Dimension)

    title: [线性代数]3-5:独立性,基和维度(Independence,Basis and Dimension) categories: Mathematic Linear Algebra ke ...

  9. 【读书笔记】:MIT线性代数(4):Independence, Basis and Dimension

    Independence: The columns of A are independent when the nullspace N (A) contains only the zero vecto ...

随机推荐

  1. 我理解的windows中断管理

    只谈外部中断的windows内核管理,异常和trap不在此文的讨论之列. 1. windows中断总貌 在windows中,物理上的中断源被抽象为KINTERRUPT结构.一个中断源在windows中 ...

  2. Android项目实战登录&注册

    由于项目中大部分界面都有一个后退键和一个标题栏,为避免代码冗杂以及便于利用,我们可以将后推荐和标题栏单独抽取出来定义一个标题栏布局,在 res/layout 目录下新建一个 Layout resour ...

  3. c 判断一个字符是否为空格

    #include <stdio.h> #include <wctype.h> int main () { wchar_t c; ; wchar_t str[] = L" ...

  4. Leetcode题目104.二叉树的最大深度(DFS+BFS简单)

    题目描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...

  5. SRS之SrsHls::on_audio详解

    1. SrsHls::on_audio 将音频数据封装到 ts 文件中. /* * mux the audio packet to ts. * @param shared_audio, directl ...

  6. hadoop用户和权限

    当前Apache Hadoop认证(authentication)支持simple和kerberos,simple是默认的,其实是信任操作系统的认证结果(也就是直接使用操作系统的用户).kerbero ...

  7. Python —— sklearn.feature_selection模块

    Python —— sklearn.feature_selection模块 sklearn.feature_selection模块的作用是feature selection,而不是feature ex ...

  8. nginx详解(代理服务器的解释+nginx 在linux 下的安装+nginx.conf 中的配置解释)

    一.概论 1.什么是代理服务器 代理服务器,客户机在发送请求时,不会直接发送给目的主机,而是先发送给代理服务器,代理服务接受客户机请求之后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器的硬 ...

  9. 【论文学习】Is the deconvolution layer the same as a convolutional layer

    结合上升采样upsample和卷积操作.Sub-piexl convolution. Efficient Sub-pixel-convolutional-layers. LR network,即低分辨 ...

  10. mybatis一对多映射【班级与学生】

    1)如图 2)创建grades.sql和students.sql drop table students; drop table grades; create table grades( gid in ...