拆点+网络流

拆点建图应该是很常见的套路了。。一张无向图不联通,那么肯定有两个点不联通,但是我们不知道这两个点是什么。

所以我们枚举所有点,并把每个点拆成入点和出点,每次把枚举的两个点的入点作为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 (算竞进阶习题)的更多相关文章

  1. POJ 1966 Cable TV Network

    Cable TV Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4702   Accepted: 2173 ...

  2. POJ 1966 Cable TV Network(顶点连通度的求解)

                               Cable TV Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  3. POJ 1966 Cable TV NETWORK(网络流-最小点割集)

                                    Cable TV NETWORK The interconnection of the relays in a cable TV net ...

  4. POJ 1966 Cable TV Network (无向图点连通度)

    [题意]给出一个由n个点,m条边组成的无向图.求最少去掉多少点才能使得图中存在两点,它们之间不连通. [思路]回想一下s->t的最小点割,就是去掉多少个点能使得s.t不连通.那么求点连通度就枚举 ...

  5. poj 1966 Cable TV Network 顶点连通度

    题目链接 给一个图, n个点m条边, 求至少去掉多少个点可以使得图不再联通.随便指定一个点为源点, 枚举其他点为汇点的情况, 跑网络流, 求其中最小的情况. 如果最后ans为inf, 说明是一个完全图 ...

  6. POJ 1966 Cable TV Network (点连通度)【最小割】

    <题目链接> 题目大意: 给定一个无向图,求点连通度,即最少去掉多少个点使得图不连通. 解题分析: 解决点连通度和边连通度的一类方法总结见   >>> 本题是求点连通度, ...

  7. POJ 1966 Cable TV Network 【经典最小割问题】

    Description n个点的无向图,问最少删掉几个点,使得图不连通 n<=50 m也许可以到完全图? Solution 最少,割点,不连通,可以想到最小割. 发现,图不连通,必然存在两个点不 ...

  8. POJ - 1966 Cable TV Network (最大流求点连通度)

    题意:求一个无向图的点连通度.点联通度是指,一张图最少删掉几个点使该图不连通:若本身是非连通图,则点连通度为0. 分析:无向图的点连通度可以转化为最大流解决.方法是:1.任意选择一个点作为源点:2.枚 ...

  9. POJ 1966 Cable TV Network (最大流最小割)

    $ POJ~1966~Cable~TV~Network $ $ solution: $ 第一眼可能让人很难下手,但本就是冲着网络流来的,所以我们直接一点.这道题我们要让这个联通图断开,那么势必会有两个 ...

随机推荐

  1. JAVA验证身份证格式及合法性

    旅游电子商务中,预订酒店或订购门票时会以身份证作为消费凭证,为了防止客户误填身份证带来不必要麻烦,需要验证码格式及合法性,代码如下: /** * 判断身份证格式 * * @param idNum * ...

  2. How to Install MemSQL

    MemSQL runs natively on 64-bit Linux operating systems. Your system hardware must have at least 4 CP ...

  3. Leetcode 686 Repeated String Match

    Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...

  4. H5上传图片之canvas

    H5上传图片之canvas,使用canvas处理压缩图片再上传 html代码: <form action="" method="post"> < ...

  5. ibeacon和蓝牙有什么区别_它们的区别在哪里

    iBeacon概述 iBeacon是苹果公司2013年9月发布的移动设备用OS(iOS7)上配备的新功能.其工作方式是,配备有低功耗蓝牙(BLE)通信功能的设备使用BLE技术向周围发送自己特有的ID, ...

  6. selenium模拟登陆淘宝

    from selenium import webdriver import time from selenium.webdriver.common.by import By from selenium ...

  7. 自签名证书 nginx tomcat

    给Nginx配置一个自签名的SSL证书 - 廖雪峰的官方网站 https://www.liaoxuefeng.com/article/0014189023237367e8d42829de24b6eaf ...

  8. 局域网 服务器 https

    局域网内搭建一个服务器,可以使用 https 吗 - V2EXhttps://www.v2ex.com/t/472394 局域网内多台机器使用自签发证书架设https网站二:实施 - 左直拳的马桶_日 ...

  9. nmon for Linux & Java

    nmon for Linux | Main / HomePagehttp://nmon.sourceforge.net/pmwiki.php Java Nmon Analyser download | ...

  10. Hibernate two table same id

    Hibernate更新数据(不用update也可以) - 森林木马 - 博客园 https://www.cnblogs.com/owenma/p/3481497.html hibernate级联更新会 ...