POJ 1966 Cable TV Network (算竞进阶习题)
拆点+网络流
拆点建图应该是很常见的套路了。。一张无向图不联通,那么肯定有两个点不联通,但是我们不知道这两个点是什么。
所以我们枚举所有点,并把每个点拆成入点和出点,每次把枚举的两个点的入点作为s和t(这样方便,当然也可以把第一个点的出点当成s,第二个点的入点当成t,但其实我们把s和t的入点和出点之间的边容量设为INF之后就没有影响了)
每条原图的边连接着u的出点和v的入点,v的出点和u的入点,容量设为INF,保证不给割,其他点的入点和出点之间的容量当然是1。这样我们的割就一定会割在容量为1的边上,每割一条,就相当于去掉了一个点。暴力跑最大流早最小值就好了
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 1000;
int n, m, cnt, head[N<<2], x[N<<2], y[N<<2], s, t, depth[N<<2], ans;
struct Edge { int v, next, f; } edge[N<<5];
void addEdge(int a, int b, int f){
edge[cnt].v = b, edge[cnt].f = f, edge[cnt].next = head[a], head[a] = cnt ++;
edge[cnt].v = a, edge[cnt].f = 0, edge[cnt].next = head[b], head[b] = cnt ++;
}
bool bfs(){
full(depth, 0);
queue<int> q;
depth[s] = 1, q.push(s);
while(!q.empty()){
int cur = q.front(); q.pop();
//cout << cur << endl;
for(int i = head[cur]; i != -1; i = edge[i].next){
int u = edge[i].v;
if(!depth[u] && edge[i].f > 0){
depth[u] = depth[cur] + 1;
q.push(u);
}
}
}
return depth[t] != 0;
}
int dfs(int cur, int a){
if(cur == t) return a;
int flow = 0;
for(int i = head[cur]; i != -1; i = edge[i].next){
int u = edge[i].v;
if(depth[u] == depth[cur] + 1 && edge[i].f > 0){
int k = dfs(u, min(a, edge[i].f));
a -= k, flow += k, edge[i].f -= k, edge[i^1].f += k;
}
if(!a) break;
}
if(a) depth[cur] = -1;
return flow;
}
int dinic(){
int ret = 0;
while(bfs()) ret += dfs(s, INF);
return ret;
}
void solve(){
full(head, -1), cnt = 0;
for(int i = 0; i < n; i ++){
if(i == s || i == t) addEdge(i, i + n, INF);
else addEdge(i, i + n, 1);
}
for(int i = 0; i < m; i ++){
addEdge(x[i] + n, y[i], INF), addEdge(y[i] + n, x[i], INF);
}
ans = min(ans, dinic());
}
int main(){
while(scanf("%d%d", &n, &m) != EOF){
for(int i = 0; i < m; i ++){
int j; char str[100]; scanf("%s", str);
for(x[i] = 0, j = 1; str[j] != ','; j ++) x[i] = x[i] * 10 + (str[j] - '0');
for(y[i] = 0, j ++; str[j] != ')'; j ++) y[i] = y[i] * 10 + (str[j] - '0');
}
ans = INF;
for(int i = 0; i < n; i ++){
for(int j = i + 1; j < n; j ++){
s = i, t = j, solve();
}
}
if(n <= 1 || ans == INF) ans = n;
printf("%d\n", ans);
}
return 0;
}
POJ 1966 Cable TV Network (算竞进阶习题)的更多相关文章
- POJ 1966 Cable TV Network
Cable TV Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4702 Accepted: 2173 ...
- POJ 1966 Cable TV Network(顶点连通度的求解)
Cable TV Network Time Limit: 1000MS Memory Limit: 30000K Total Submissi ...
- POJ 1966 Cable TV NETWORK(网络流-最小点割集)
Cable TV NETWORK The interconnection of the relays in a cable TV net ...
- POJ 1966 Cable TV Network (无向图点连通度)
[题意]给出一个由n个点,m条边组成的无向图.求最少去掉多少点才能使得图中存在两点,它们之间不连通. [思路]回想一下s->t的最小点割,就是去掉多少个点能使得s.t不连通.那么求点连通度就枚举 ...
- poj 1966 Cable TV Network 顶点连通度
题目链接 给一个图, n个点m条边, 求至少去掉多少个点可以使得图不再联通.随便指定一个点为源点, 枚举其他点为汇点的情况, 跑网络流, 求其中最小的情况. 如果最后ans为inf, 说明是一个完全图 ...
- POJ 1966 Cable TV Network (点连通度)【最小割】
<题目链接> 题目大意: 给定一个无向图,求点连通度,即最少去掉多少个点使得图不连通. 解题分析: 解决点连通度和边连通度的一类方法总结见 >>> 本题是求点连通度, ...
- POJ 1966 Cable TV Network 【经典最小割问题】
Description n个点的无向图,问最少删掉几个点,使得图不连通 n<=50 m也许可以到完全图? Solution 最少,割点,不连通,可以想到最小割. 发现,图不连通,必然存在两个点不 ...
- POJ - 1966 Cable TV Network (最大流求点连通度)
题意:求一个无向图的点连通度.点联通度是指,一张图最少删掉几个点使该图不连通:若本身是非连通图,则点连通度为0. 分析:无向图的点连通度可以转化为最大流解决.方法是:1.任意选择一个点作为源点:2.枚 ...
- POJ 1966 Cable TV Network (最大流最小割)
$ POJ~1966~Cable~TV~Network $ $ solution: $ 第一眼可能让人很难下手,但本就是冲着网络流来的,所以我们直接一点.这道题我们要让这个联通图断开,那么势必会有两个 ...
随机推荐
- Leetcode -- 258 数位相加
258. Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- 关于jsp中jstl-core标签循环遍历的使用
JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能. JSTL支持通用的.结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标签. 除了这些,它还提供 ...
- Solrcloud(Solr集群)
Solrcloud(Solr集群) Solrcloud介绍: SolrCloud(solr集群)是Solr提供的分布式搜索方案. 当你需要大规模,容错,分布式索引和检索能力时使用SolrCloud. ...
- 【学习总结】Git学习-参考廖雪峰老师教程十-自定义Git
学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...
- ocrosoft 1015 习题1.22 求一元二次方程a*x^2 + b*x + c = 0的根
http://acm.ocrosoft.com/problem.php?id=1015 题目描述 求一元二次方程a*x2 + b*x + c = 0的根.系数a.b.c为浮点数,其值在运行时由键盘输入 ...
- Ubuntu16系统中安装htpasswd
htpasswd是Apache附带的程序, htpasswd生成包含用户名和密码的文本文件, 每行内容格式为“用户名:密码”, 用于用户文件的基本身份认证. 当用户浏览某些网页的时候, 浏览器会提示输 ...
- 并发包 concurrent(一) Atomic
1:基础概念 悲观锁(Pessimistic Lock), 顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁.传 ...
- UTC时间、GMT时间、本地时间、Unix时间戳
引用: https://blog.csdn.net/u012102306/article/details/51538574 https://blog.csdn.net/foxir/article/de ...
- bootstrap modal垂直居中(简单封装)
1.使用modal 弹出事件方法: 未封装前: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- CentOS7 网络NAT模式
问题:安装完毕ping命令不能用,然后改为桥接模式,ping可以用. 先了解桥接,NAT 的含义. 桥接:在bridged模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机,它可以访 ...