HDU-4687 Boke and Tsukkomi

题意:给定一个简单图,询问哪些边如果选择的话会使得最大的连边数减少。

解法:套用一般图的最大匹配算法(带花树)先算出最大匹配数,然后枚举一条边被选择(注意:如果改变被选择,则两端点相邻的边都应删除),看是否只减少一条匹配边。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std; const int MAXN = ;
int n, m;
int u[], v[]; struct Graph { bool mat[MAXN + ][MAXN + ];
int n; bool inque[MAXN + ];
int que[MAXN], head, tail; int match[MAXN + ], father[MAXN + ], base[MAXN + ]; int inpath[MAXN + ];
static int pcnt; // count of paths have existed int inblossom[MAXN + ];
static int bcnt; // count of blossoms have existed void init(int _n) {
n = _n;
for (int i = ; i <= n; ++i) {
match[i] = ;
for (int j = ; j <= n; ++j)
mat[i][j] = false;
}
} int pop() { return que[head++]; } void push(int x) {
que[tail++] = x;
inque[x] = true;
} void add_edge(int a, int b) {
mat[a][b] = mat[b][a] = true;
} int find_ancestor(int u, int v) {
++pcnt;
while (u) {
u = base[u];
inpath[u] = pcnt;
u = father[match[u]];
// if match[u] == 0, meaning u is the root node, it also works, because father[0] == 0
}
while (true) {
v = base[v];
if (inpath[v] == pcnt) return v;
v = father[match[v]];
}
} void reset(int u, int anc) {
while (u != anc) {
int v = match[u];
inblossom[base[v]] = bcnt;
inblossom[base[u]] = bcnt;
v = father[v];
if (base[v] != anc) father[v] = match[u];
u = v;
}
} void contract(int u, int v) {
int anc = find_ancestor(u, v);
++bcnt;
reset(u, anc);
reset(v, anc);
if (base[u] != anc) father[u] = v;
if (base[v] != anc) father[v] = u;
for (int i = ; i <= n; ++i)
if (inblossom[base[i]] == bcnt) {
base[i] = anc;
if (!inque[i]) push(i);
}
} int find_augment(int start) {
for (int i = ; i <= n; ++i) {
father[i] = ;
inque[i] = false;
base[i] = i;
}
head = ; tail = ; push(start);
while (head < tail) {
int u = pop();
for (int v = ; v <= n; ++v)
if (mat[u][v] && base[v] != base[u] && match[v] != u) {
if (v == start || (match[v] && father[match[v]]))
// node v is out-point
contract(u, v); // make blossom
else {
if (father[v] == ) { // not in-point
if (match[v]) { // has matched
push(match[v]); // match[v] becomes out-point
father[v] = u; // v becomes in-point
} else {
father[v] = u; // node v is another end
return v;
}
}
}
}
}
return ;
} void augment(int finish) {
int u = finish, v, w;
while (u) {
v = father[u];
w = match[v];
match[u] = v;
match[v] = u;
u = w;
}
} int graph_max_match() {
int ans = ;
for (int i = ; i <= n; ++i)
if (match[i] == ) {
int finish = find_augment(i);
if (finish) {
augment(finish);
ans += ;
}
}
return ans;
} } g; int Graph::bcnt = , Graph::pcnt = ;
vector<int>vt; int main() {
while (scanf("%d %d", &n, &m) != EOF) {
g.init(n);
vt.clear();
bool first = true;
for (int i = ; i < m; ++i) {
scanf("%d %d", &u[i], &v[i]);
g.add_edge(u[i], v[i]);
}
int MaxPair = g.graph_max_match() / ; // 返回的是匹配的点数
for (int i = ; i < m; ++i) {
g.init(n);
int a = u[i], b = v[i];
for (int j = ; j < m; ++j) { // 如果匹配这条边那么两个端点其他连边都是不能匹配的
if (u[j] == a || u[j] == b || v[j] == a || v[j] == b) continue;
g.add_edge(u[j], v[j]);
}
int tmp = g.graph_max_match() / ;
if (tmp != MaxPair - ) vt.push_back(i+);
}
printf("%d\n", vt.size());
for (int i = ; i < (int)vt.size(); ++i) {
printf(i == ? "%d" : " %d", vt[i]);
}
puts("");
}
return ;
}

2013 Multi-University Training Contest 9的更多相关文章

  1. Integer Partition(hdu4658)2013 Multi-University Training Contest 6 整数拆分二

    Integer Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  2. Partition(hdu4651)2013 Multi-University Training Contest 5

    Partition Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  3. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

  4. Partition(hdu4651)2013 Multi-University Training Contest 5----(整数拆分一)

    Partition Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. JSU 2013 Summer Individual Ranking Contest - 5

    JSU 2013 Summer Individual Ranking Contest - 5 密码:本套题选题权归JSU所有,需要密码请联系(http://blog.csdn.net/yew1eb). ...

  6. HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)

    Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/500 ...

  8. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  9. hdu 4946 2014 Multi-University Training Contest 8

    Area of Mushroom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  10. 2016 Multi-University Training Contest 2 D. Differencia

    Differencia Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

随机推荐

  1. 27、oracle(三)

    1)掌握增.删.改数据和事务操作 2)掌握[视图]和同义词 3)掌握[序列]和索引 4)了解有关用户和权限的控制 ------------------------------------------- ...

  2. oracle数据库表空间文件收缩实例

    Oracle数据文件收缩实例 数据文件的作用 HWM的基本概念 查看数据文件的使用情况 包括内容:数据文件大小,已经used空间,free空间,hwm信息 select /*+ ordered use ...

  3. List与Set的contains方法效率问题

    今天看到网上一篇文章说:Set检索元素效率低下,删除和插入效率高:List查找元素效率高,插入删除元素效率低.于是想到List虽然用get(index)方法查询效率高,但是若用contains方法查询 ...

  4. (jdbc)取得数据库自动生成的主键方法

    一些类,在前面的博客中有,就不重复了 public class Test2 { TestDAO t=new TestDAO(); /*前提是数据表的主键是自动增加的, *取得数据库自动生成的主键 * ...

  5. hasOwnproperty详细总结

    hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象.不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员. isPrototypeO ...

  6. 验证码识别--type7

    验证码识别--type7 一.干扰分析 有黑色边框,然后点干扰,线干扰 去边框 去点干扰 变成这样的结果,方便运算吗?也可以多种方式联合起来运算的.我相信在很多情况下,都可能会遇到类似的结果.我们人类 ...

  7. HashMap, HashTable, CurrentHashMap的区别

    转载:http://www.jianshu.com/p/c00308c32de4 HashMap vs ConcurrentHashMap 引入ConcurrentHashMap是为了在同步集合Has ...

  8. http模式

    http遵守请求响应模式: 请求: 请求行:HTTP方法-请求的URL-HTTP版本 请求行 空行 消息体 响应: 状态行:HTTP版本-响应码-响应描述 响应头 空行 消息体 其他的重点: http ...

  9. Radar之获取排列的UITexture数组

    1.定义UITexture二维数组 public UITexture[,] m_UITexture; 2.在 void start() 中分配内存 m_UITexture = , ]; 3.遍历排列的 ...

  10. dubbo源码之四——服务发布二

    dubbo版本:2.5.4 2. 服务提供者暴露一个服务的详细过程 上图是服务提供者暴露服务的主过程: 首先ServiceConfig类拿到对外提供服务的实际类ref(如:HelloWorldImpl ...