P3731 题解
简要题意是找到一条边连接使得最大团大小增加。
在补图上最大团等于最大独立集。
所以问题转化为删掉一条边使得最大独立集增加,又因为团不超过两个,所以原图是二分图,也就是使得最大匹配减少。
考虑什么样的匹配边是可以被代替的,先跑一遍网络流求最大匹配,不难发现假若一条匹配在残量网络流上的一个环中,那么它就是可以被代替的,所以对残量网络进行缩点,两个端点不在同一个强连通分量中的匹配边可以作为答案。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 114;
const int maxm = 2e6 + 114;
const int inf = 0x3f3f3f3f;
int maxflow, vis[maxn], tot = 1, cnt;
int s, t, n, m, Q, E;
vector<int> edge[maxn], Road[maxn];
int hd[maxn], road[maxn], dis[maxn];
map<int, int> f[maxn];
struct edge {
int next, to, w;
} e[maxm * 2];
void add(int u, int v, int w) {
e[++tot].to = v;
f[u][v] = tot;
e[tot].w = w;
e[tot].next = hd[u];
hd[u] = tot;
e[++tot].to = u;
e[tot].w = 0;
e[tot].next = hd[v];
hd[v] = tot;
}
bool bfs() {
memset(dis, 0, sizeof(dis));
dis[s] = 1;
queue<int>Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
road[u] = hd[u];
for (int i = hd[u]; i; i = e[i].next) {
int v = e[i].to;
if (!dis[v] && e[i].w) {
dis[v] = dis[u] + 1;
Q.push(v);
}
}
}
return dis[t] != 0;
}
int dinic(int now, int res) {
if (now == t)
return res;
int tp = res;
for (int i = road[now]; i; i = e[i].next) {
int v = e[i].to;
road[now] = i;
if (dis[v] == dis[now] + 1 && e[i].w) {
int k = min(e[i].w, tp);
int del = dinic(v, k);
e[i].w -= del;
e[i ^ 1].w += del;
tp -= del;
if (!tp)
break;
}
}
return res - tp;
}
int col[maxn], Use[maxn];
void dfs(int u) {
if (Use[u] == 1)
return ;
Use[u] = 1;
for (int v : edge[u])
col[v] = col[u] ^ 1, dfs(v);
}
int dfsn[maxn];
int low[maxn];
stack<int> S;
int Vis[maxn], use[maxn];
int color[maxn], sum = 0;
int deep = 0;
void paint(int u) {
S.pop();
color[u] = sum;
Vis[u] = 0;
}
void tanjan(int u) {
dfsn[u] = ++deep;
low[u] = deep;
Vis[u] = 1;
use[u] = 1;
S.push(u);
for (int i = hd[u]; i; i = e[i].next) {
if (e[i].w == 0)
continue;
int v = e[i].to;
if (dfsn[v] == 0) {
tanjan(v);
low[u] = min(low[u], low[v]);
} else {
if (Vis[v] != 0) {
low[u] = min(low[u], low[v]);
}
}
}
if (dfsn[u] == low[u]) {
sum++;
while (S.top() != u) {
paint(S.top());
}
paint(u);
}
return ;
}
set< pair<int, int>> chifan;
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
edge[u].push_back(v);
edge[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (Use[i] == 0)
col[i] = 0, dfs(i);
}
for (int i = 1; i <= n; i++) {
if (col[i] == 0) {
for (int nxt : edge[i])
Road[i].push_back(nxt), add(i, nxt, 1);
}
}
s = n + 1, t = n + 2;
for (int i = 1; i <= n; i++)
if (col[i] == 0)
add(s, i, 1);
else
add(i, t, 1);
while (bfs() == true)
maxflow += dinic(s, inf);
for (int i = 1; i <= n + 2; i++) {
if (use[i] == 0)
tanjan(i);
}
for (int i = 1; i <= n; i++) {
for (int nxt : Road[i]) {
if (e[f[i][nxt]].w == 0 && color[i] != color[nxt]) {
chifan.insert(make_pair(min(i, nxt), max(i, nxt)));
}
}
}
cout << chifan.size() << '\n';
for (pair<int, int> OUT : chifan) {
cout << OUT.first << ' ' << OUT.second << '\n';
}
}
P3731 题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
- JSOI2016R3 瞎BB题解
题意请看absi大爷的blog http://absi2011.is-programmer.com/posts/200920.html http://absi2011.is-programmer.co ...
随机推荐
- LLaMA 3 源码解读-大语言模型5
本来不是很想写这一篇,因为网上的文章真的烂大街了,我写的真的很有可能没别人写得好.但是想了想,创建这个博客就是想通过对外输出知识的方式来提高自身水平,而不是说我每篇都能写得有多好多好然后吸引别人来看. ...
- 记录——Qt Qcreator 顶部菜单栏的隐藏与恢复
问题 我有一个朋友(嗯~无中生友),手残点击了 QCreator 中视图下的这个玩意儿: 当人的眼神不好时,可能不会看到这些快捷键以及无视这些弹窗. 解决方案 快捷键 ctrl + alt + M 可 ...
- 用Python画一个冰墩墩!
北京2022年冬奥会的召开,吉祥物冰墩墩着实火了,真的是一墩难求,为了实现冰墩墩的自由,经过资料搜集及参考冰墩墩网上的开源代码(https://github.com/HelloCoder-HaC/bi ...
- C 语言编程 — 高级数据类型 — 枚举
目录 文章目录 目录 前文列表 声明枚举类型 定义枚举类型的变量 枚举类型变量的枚举值 枚举在 switch 语句中的使用 将整型转换为枚举类型 前文列表 <程序编译流程与 GCC 编译器> ...
- c#获取开机时间
public static DateTime OpenCom() { TimeSpan t = TimeSpan.FromMilliseconds(System.Environment.TickCou ...
- zfile 在线云盘、网盘、OneDrive、云存储、私有云、对象存储、h5ai、上传、下载
基于 Java 的在线网盘程序,支持对接 S3.OneDrive.SharePoint.又拍云.本地存储.FTP.SFTP 等存储源,支持在线浏览图片.播放音视频,文本文件.Office.obj(3d ...
- 安装numpy:conda install nampy==1.16 时报错An HTTP error occurred when trying to retrieve this URL.
安装numpy:conda install nampy==1.16 时报错An HTTP error occurred when trying to retrieve this URL. HTTP e ...
- ubuntu18.04最小化安装
ubuntu 18.04虚拟机安装 镜像下载地址: https://releases.ubuntu.com/18.04/ubuntu-18.04.6-live-server-amd64.iso 创建虚 ...
- AutoLayout + UILabel布局
一.内容决定宽度 实现AutoLayout模式下面,UILabel跟随内容大小自动扩张,在storyboard中拖拽一个UILabel,将其居中,然后定时改变内容,不需要特别设置,那么UILabel宽 ...
- 通过钩子函数+Traceid实现Flask链路追踪
背景 在flask web中我们通常需要一个traceid作为调用参数传递给全链路各个调用函数 需要针对一次请求创建一个唯一的traceid:这里用uuid去简化代替 我们需要保证traceid不被污 ...