cf D George and Interesting Graph
题意:给你一个有趣图的定义:在这个图中有一个根,根与每个点都有边和回边,除了根之外,其他的点的出度和入度都为2,然后给你一个图让你经过几步操作可以使此图变为有趣图,操作为:删边或者加边。
思路:枚举根,然后删除与根有关的边,重新建图,用二分图求最大匹配,可以用匈牙利算法,加的边数:满足题中有关根的加边数+(点数-匹配数),删掉的边数:边数-满足题中有关根的使用的边数-匹配时使用的边数。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int inf=<<; int g[][];
int n,m;
bool chk[];
int match[];
vector<int>x[]; int dfs(int p)
{
for(int i=; i<(int)x[p].size(); i++)
{
int v=x[p][i];
if(!chk[v])
{
chk[v]=true;
int t=match[v];
match[v]=p;
if(t==-||dfs(t))
{
return ;
}
match[v]=t;
}
}
return ;
} int Pro(int c)
{
memset(match,-,sizeof(match));
int res=;
for(int i=; i<=n; i++)
{
if(i==c) continue;
memset(chk,false,sizeof(chk));
res+=dfs(i);
}
return res;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
vector<int>gg[];
for(int i=; i<=m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
gg[u].push_back(v);
g[u][v]=;
}
int ans=inf;
for(int i=; i<=n; i++)
{
int t1=n-gg[i].size();
for(int j=; j<=n; j++)
{
if(!g[j][i]) t1++;
}
if(g[i][i]==) t1--;
for(int j=; j<=n; j++)
{
x[j].clear();
}
int cnt=;
for(int j=; j<=n; j++)
{
if(j==i) continue;
for(int k=; k<(int)gg[j].size(); k++)
{
int v=gg[j][k];
if(v==i)continue;
cnt++;
x[j].push_back(v);
}
}
ans=min(ans,t1+cnt+n--*Pro(i));
}
printf("%d\n",ans);
}
return ;
}
cf D George and Interesting Graph的更多相关文章
- 【CF387D】George and Interesting Graph(二分图最大匹配)
题意:给定一张n点m边没有重边的有向图,定义一个有趣图为:存在一个中心点满足以下性质: 1.除了这个中心点之外其他的点都要满足存在两个出度和两个入度. 2.中心 u 需要对任意顶点 v(包括自己)有一 ...
- Codeforces 916C - Jamie and Interesting Graph
916C - Jamie and Interesting Graph 思路:构造. 对于1到n最短路且素数,那么1到n之间连2 对于最小生成树,找一个稍微大点的素数(比1e5大)构造一个和为这个素数的 ...
- Codeforces Beta Round #9 (Div. 2 Only) E. Interesting Graph and Apples 构造题
E. Interesting Graph and Apples 题目连接: http://www.codeforces.com/contest/9/problem/E Description Hexa ...
- cf B George and Cards
题意:给你一个只有‘.’和'#'的n*n的格子,问所有的'#'是不是只属于一个十字叉,如果不是输出NO,否则输出YES. #include <cstdio> #include <cs ...
- cf E. George and Cards
http://codeforces.com/contest/387/problem/E 题意:给你n个数,然后在输入k个数,这k个数都在n个数中出现,进行每一次操作就是在n个数中选择长度为w的连续序列 ...
- cf C. George and Number
http://codeforces.com/problemset/problem/387/C 题意:给你一个大数,让你求个集合,可以通过操作得到这个数,求集合中个数最大值,操作 :从集合中任意取两个数 ...
- cf B George and Round
题意:输入n,m,下一行为n个数a1<a2<a3......<an:然后再输入m个数b1<=b2<=b3<.....<=bm: 每个ai都必须在b中找到相等的 ...
- cf D. Dima and Trap Graph
http://codeforces.com/contest/366/problem/D 遍历下界,然后用二分求上界,然后用dfs去判断是否可以. #include <cstdio> #in ...
- CodeForces 916C Jamie and Interesting Graph (构造)
题意:给定两个数,表示一个图的点数和边数,让你构造出一个图满足 1- n 的最短路是素数,并且最小生成树也是素数. 析:首先 1 - n 的最短路,非常好解决,直接 1 连 n 就好了,但是素数尽量 ...
随机推荐
- shell 获取网关 以及修改ip 启用网卡
shell 获取网关 以及修改ip 启用网卡 #!/bin/bash #autho freefei #script is a init computer eth #data 2014 10 09 19 ...
- Linux入门基础 #5:Linux文件系统挂载管理
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- [PWA] 19. Cache the avatars
Cache the avatars is little different from cache photos. We need to serve the page with our cache da ...
- Java编程陷阱-类成员初始化
原文地址:http://blog.csdn.net/veryitman/article/details/6450523 如果你忽略Java的细节,恐怕你的代码会充满bug,下面讨论关于类成员初始化问题 ...
- sqlite使用blob类型存储/访问 结构体
/* open fire host and slora report data database */ int open_report_db(void) { ; char sql[SQL_COMMAN ...
- 关于NetworkInfo对象的isConnected()与isAvailable()
public class MainActivity extends Activity{ /** Called when the activity is first created. */ ...
- You must not call setTag() on a view Glide is targeting
概述 在使用Glide加载图片时,如果出现"You must not call setTag() on a view Glide is targeting"的错误,八成是在使用Li ...
- [笔记]SD卡相关资料
ESD静电放电模块 我知道的flash分为两种NOR flash和NAND flash,NOR falsh容量一般为1~16M用于单片机代码存储,NAND flash最小的是8M最大的现在听说有90G ...
- oc常用正则表达式
常用的第三方正则库: http://regexkit.sourceforge.net/RegexKitLite/index.html 匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹 ...
- JS 通过系统时间限定 动态添加 select option
虽然是个简单的效果,还是需要积累一下,记录一下: 在八月一号之后,删除最后一项,新添加2016级 — — 2015级 2014级 2013级 2012级 在六月一号之后,删除最后一项,新添加2016级 ...