Codeforces 387D George and Interesting Graph(二分图匹配)
来源:互联网 发布:电动机绕组数据大全 编辑:程序博客网 时间:2024/06/05 12:06
题目链接:Codeforces 387D George and Interesting Graph
题目大意:定义一种有趣图:有向无子环图,存在一个根磨合任意节点有一去路和一回路(本身也有),然后除去根以外的所有点的入度和出度均为2。现在给出一张图,问说最少给进行几次操作可以使得图变成有趣图,操作可以是删除和添加一条边。
解题思路:枚举根,判断以该节点为根需要几次操作,维护最小值。
确定根后,计算出为满足根与所有点的有回路去路需要几步操作,n*2-1-s(s为与根相连的边,s=ind[root]+out[root]-g[root][root]);然后除去根后,其他的点的出度和入度均为1,首尾相接即可,用二分图匹配计算出已经存在的边可以满足几个。
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int N = 505;int n, m, g[N][N], ind[N], out[N], v[N], p[N];void init () {memset(g, 0, sizeof(g));memset(ind, 0, sizeof(ind));memset(out, 0, sizeof(out));int a, b;scanf("%d%d", &n, &m);for (int i = 0; i < m; i++) {scanf("%d%d", &a, &b);g[a][b] = 1;out[a]++; ind[b]++;}}bool find (int r, int u) {for (int i = 1; i <= n; i++) {if (i == r || !g[u][i]) continue;if (v[i]) continue;v[i] = 1;if (p[i] == -1 || find(r, p[i])) {p[i] = u;return true;}}return false;}int handle(int r) {int ans = 0;memset(p, -1, sizeof(p));for (int i = 1; i <= n; i++) {if (i == r) continue;memset(v, 0, sizeof(v));if (find(r, i)) ans++;}return ans;}int solve (int r) {int s = ind[r] + out[r] - g[r][r];int ans = 2 * n - 1 - s;int f = handle(r);ans = ans + m - s - f;ans = ans + n - 1 - f;return ans;}int main () {init();int ans = 1 << 30;for (int i = 1; i <= n; i++) ans = min(ans, solve(i));printf("%d\n", ans);return 0;}
1 0
- 【二分图匹配】Codeforces 387D George and Interesting Graph
- Codeforces 387D George and Interesting Graph【思维+二分匹配】
- Codeforces 387D George and Interesting Graph(二分图匹配)
- codeforces 387D George and Interesting Graph (二分图匹配)
- codeforces 387D George and Interesting Graph(二分图最大匹配)
- CodeForces 387D George and Interesting Graph
- Codeforces 387E George and Cards(二分+树状数组)
- Codeforces 387E George and Cards(二分+树状数组)
- Codeforces 366D Dima and Trap Graph【二分+Dfs】
- CodeForces 624C Graph and String(二分图染色)
- Codeforces Round #214 (Div. 2) D. Dima and Trap Graph (枚举+二分+搜索)
- Codeforces 366D Dima and Trap Graph(搜索剪枝/二分)
- CodeForces 703D Mishka and Interesting sum
- Codeforces-703D Mishka and Interesting sum
- CodeForces 703D Mishka and Interesting sum
- CodeForces 703D Mishka and Interesting sum
- Codeforces 387B George and Round(贪心)
- Codeforces 387C George and Number(贪心)
- centos6.5使用光盘作为本地的yum源
- C# 中的委托和事件
- 树的相关概念
- C#中的委托和事件(续)
- hibernate-2014-02-01
- Codeforces 387D George and Interesting Graph(二分图匹配)
- android 之 socket编程(TCP&UDP)
- 【索引】CodeForces Round #227 (Div. 2)
- WORD与DWORD
- tmpfs内存文件挂载和使用
- linux fstab uuid
- 网络流|二分图+uva11045
- 最新3D思维导图软件imindmap6破解版/中文汉化版免费下载~乐趣尽在其中
- 这个是转载的关于 linux fstab文件 uuid 的疑惑,写的太好了,完整转载,没修改。是原文。