题目链接: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;
}
分享到:
相关推荐
Codeforces 1925D Good Trip 题解
暴枚最长桌脚的长度$l$,然后长度比$l$长的桌脚全部都要砍掉长度比$l$短的桌脚选择代价前$k$小的砍掉用线段树维护;示例程序 :typedef long l
Codeforces 题库 101-200 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
Codeforces 题库 001-100 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
codeforces编程网站预测分数插件
codeforces每日一题。 题意: 给出一个数组,让你挑选出能组成任意pair差值为2的幂的序列,并且使这个序列长度尽可能大。 思路: 针对于挑选出来的序列,任意pair对的差值为2的幂数。 假设有多个pair对,设dis(a,b)=2...
Educational Codeforces Round 157D. XOR Construction
Codeforces 1105B - Zuhair and Strings 测试点37个(全)
使用于Google Chrome的Codeforces Enhancer 1.1.2插件安装包。 版本:codeforces enhancer 1.1.2 使用浏览器:Google Chrome
【并查集】Codeforces 566D Restructuring Company题面在这里对于本题,只需要再维护一个并查集表示i所在联通块的最右位置因为相邻
Codeforces 149 D-Coloring Brackets,动态规划求解
Codeforces 185A - Plant 全测试点49个
codeforces 19 E Fairy 一道比较难的题目的解题报告 推荐阅读
Codeforces global round 10 codes
题意: 给出 nnn 个点,n−1n-1n−1 条边,最多询问 n2\frac{n}{2}2n 次,每次询问 u,vu,vu,v,会给出 uvuvuv的最近公共祖先,求树的根。 ...操作就是一个删除叶子节点的过程。 AC代码: const int N = 1010;...
Codeforces round 678 division 2 codes
Some of the Codeforces problems codes
题目大意:给出一个由 n 个点组成的树,现在可以询问 n/2 次(向下取整)LCA,确定根节点是哪个节点 题目分析:因为最多只能求 n/2 次lca,每次需要两个节点作为参数,也就是说每个点我们至多遍历一遍,读完题后没什么...
Codeforces round 678 D2_Codeforces_源码
一个Codeforces、牛客竞赛、AtCoder平台的编程竞赛查询插件,ACMer必备.zip