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 ...
随机推荐
- Linux环境下:程序的链接, 装载和库[动态链接]
静态链接库在程序编译阶段就完成了链接工作,完成链接后,依赖的库就都打入了可执行文件中,所以文件大小一般会比较大. 而动态库链接库是在程序运行时才被链接的,所以磁盘上只要保留一份副本,因此节约了磁盘空间 ...
- mongodb的replication与shard分片结合使用详解
部署脚本 #!/bin/bash #复制集配置 IP='10.0.0.12' #主机ip NA='rs3' #复制集名称 if [ "$1" = "reset" ...
- .net Mvc5Webapi接口接收参数为null的一种情况分享
同样的前后端项目,其他接口用post接收自定义对象形式的参数,是能成功接收的.在这个前提下,出现某个接口接收的参数为null或值全是默认值,可能的原因是这样: 前端定义的参数的字段比后台定义的dto对 ...
- C# 使用 NPOI 导出excel 单击单元格背景变黑色的解决办法
需要手动指定单元格的背景色为一种颜色, 特别注意,我在使用 var color=new XSSFColor(new color...)创建的颜色,即使设置成其他颜色,查看样式属性中,发现color.i ...
- Abp vNext 框架 文章
http://www.vnfan.com/helinbin/tag/Abp%20vNext框架/
- 一款功能强大的Python工具,一键打包神器,一次编写、多平台运行!
1.项目介绍 Briefcase是一个功能强大的工具,主要用于将Python项目转化为多种平台的独立本地应用.它支持多种安装格式,使得Python项目能够轻松打包并部署到不同的操作系统和设备上,如ma ...
- php程序出现乱码
// 1, PHP程序中的 中文乱码 // php7.0以下程序,没有默认设定 编码格式 , 需要添加响应头 // header("Conte ...
- LeetCode 682. Baseball Game 棒球比赛(C++/Java)
题目: You're now a baseball game point recorder. Given a list of strings, each string can be one of th ...
- 一文了解 - -> SpringMVC
一.SpringMVC概述 Spring MVC 是由Spring官方提供的基于MVC设计理念的web框架. SpringMVC是基于Servlet封装的用于实现MVC控制的框架,实现前端和服务端的交 ...
- P6259
problem 考虑使用 dfs 模拟. 由于一个程序可能在不进入无限循环的情况下运行很多步,这将会非常缓慢.因此,接下来要加速模拟,可以用记忆化搜索. 在网格中,机器人的可能状态(位置和朝向)只有 ...