拆点+网络流

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

所以我们枚举所有点,并把每个点拆成入点和出点,每次把枚举的两个点的入点作为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. NLP基础——词集模型(SOW)和词袋模型(BOW)

    (1)词集模型(Set Of Words): 单词构成的集合,集合自然每个元素都只有一个,也即词集中的每个单词都只有一个. (2)词袋模型(Bag Of Words): 如果一个单词在文档中出现不止一 ...

  2. Web 应用 WEB框架 HTTP协议 初识Django

    ----------------------------财富存在于人的思想里,你没找到路,不等于没有路,你想知道将来要得到什么,你必须知道现在应该先做什么和先放弃什么! [web 应用] web应用 ...

  3. vertical-align和图片下方空白问题

    <style> .box1,.box2{ display: inline-block; background-color:#f0f3f9; width:150px; height: 150 ...

  4. nginx负载均衡精简配置实例

    [root@localhost ~]# vim nginx.conf user nginx; worker_processes ; error_log /var/log/nginx/error.log ...

  5. IP判断

    题目描述 在基于Internet的程序中,我们常常需要判断一个IP字符串的合法性. 合法的IP是这样的形式: A.B.C.D 其中A.B.C.D均为位于[0, 255]中的整数.为了简单起见,我们规定 ...

  6. hibernate坑边闲话

    使用hibernate各种各样的坑 Remember that ordinal parameters are 1-based node to traverse cannot be null 这两个错误 ...

  7. JS 原型与原型链

    图解: 一.普通对象 跟 函数对象 JavaScript 中,一切皆对象.但对象也有区别,分为 普通对象 跟 函数对象,Object 和 Function 是 JavaScript 自带的函数对象. ...

  8. Echatrs 中PIE饼图中间位置怎么显示总数值?

    title: { text: '总资产', subtext: '2000000.00', x: 'center', y: 'center' }图例:

  9. iOStextField/textView在输入时限制emoji表情的输入

    https://www.jianshu.com/p/5227e6aab4d4 2017.02.27 13:08* 字数 146 阅读 6109评论 6喜欢 14 又遇到输入框输入表情的情况了,之前写了 ...

  10. org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manu

    这个是sql 语句 错误     仔细检查 SQL语句是否写错了 org.apache.ibatis.exceptions.PersistenceException: ### Error queryi ...