【题目链接】:http://codeforces.com/problemset/problem/505/D

【题意】



让你构造一张有向图;

n个点;

以及所要求的m对联通关系(xi,yi)

即要求这张有向图中的点xi能够联通到点yi;

问你最少需要添加多少条边才够;

【题解】



先将输入的m条边;

当成无向边,构成一张无向图;

然后对于构成这张图的各个联通块;

设len为这个联通块的节点个数;

如果这个联通块它对应的有向图内有环;

则这个联通块需要len条有向边;

(即这len个节点首尾相连构成一个环,只需要len条边)

这样不管你内部要怎么样的连通性都行,因为任意两个点都是联通的;

如果对应的有向图没环;

则这个联通块只需要len-1条有向边;

(总能用len-1条边构造出来符合要求的图的..因为没有环)

把各个联通块的答案都累加起来就好;

有向图找环用拓扑排序就好;

(防止爆栈什么的 。)



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100; int n,m,rudu[N],ans;
vector <int> G[N],G1[N],v;
queue <int> dl; bool vis[N]; void dfs(int x){
if (vis[x]) return;
vis[x] = true;
v.pb(x);
int len = G1[x].size();
rep1(i,0,len-1)
dfs(G1[x][i]);
} int main(){
//Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n >> m;
rep1(i,1,m){
int x,y;
cin >> x >> y;
G[x].pb(y);
rudu[y]++;
G1[x].pb(y);
G1[y].pb(x);
} rep1(i,1,n)
if (!vis[i]){
v.clear();
dfs(i);
while (!dl.empty()) dl.pop();
rep1(j,0,(int) v.size()-1)
if (rudu[v[j]]==0){
dl.push(v[j]);
}
int num = 0;
while (!dl.empty()){
int x = dl.front();
num++;
dl.pop();
rudu[x] = -1;
rep1(j,0,(int) G[x].size()-1){
rudu[G[x][j]]--;
if (rudu[G[x][j]]==0){
dl.push(G[x][j]);
}
}
}
ans+=(int) v.size() - (num==(int) v.size() ? 1:0);
}
cout << ans << endl;
return 0;
}

【codeforces 505D】Mr. Kitayuta's Technology的更多相关文章

  1. 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter

    [题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...

  2. 【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化

    神题……胡乱讲述一下思维过程……首先,读懂题.然后,转化问题为构造一个长度为|T|+n的字符串,使其内含有T这个子序列.之后,想到一个简单的dp.由于是回文串,我们就增量构造半个回文串,设f(i,j, ...

  3. 【codeforces 255D】Mr. Bender and Square

    [题目链接]:http://codeforces.com/problemset/problem/255/D [题意] 给你一个n*n的方框; 给你一个方块;(以下说的方块都是单位方块) 每一秒钟,可以 ...

  4. 【CF505D】Mr. Kitayuta's Technology

    题目大意: 在一个有向图中,有n个顶点,给出m对数字(u,v)表示顶点u和顶点v必须直接或者间接相连,让你构造一个这样的图,输出最少需要多少条边. 挖坑待填 官方题解链接:http://codefor ...

  5. 【CF506E】Mr. Kitayuta's Gift dp转有限状态自动机+矩阵乘法

    [CF506E]Mr. Kitayuta's Gift 题意:给你一个字符串s,你需要在s中插入n个字符(小写字母),每个字符可以被插在任意位置.问可以得到多少种本质不同的字符串,使得这个串是回文的. ...

  6. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  7. CodeForces 506B/505D Mr. Kitayuta's Technology

    Portal:http://codeforces.com/problemset/problem/506/B http://codeforces.com/problemset/problem/505/D ...

  8. 【38.02%】【codeforces 625B】War of the Corporations

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  9. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

随机推荐

  1. 做支付遇到的HttpClient大坑

    前言 HTTPClient大家应该都很熟悉,一个很好的抓网页,刷投票或者刷浏览量的工具.但是还有一项非常重要的功能就是外部接口调用,比如说发起微信支付,支付宝退款接口调用等:最近我们在这个工具上栽了一 ...

  2. asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值

    asp.net mvc 自定义模型绑定 有潜在的Requset.Form 自定义了一个模型绑定器.前端会传过来一些敏感字符.调用bindContext. valueProvider.GetValue( ...

  3. ExtJs之Ext.XTemplate:数组填充,访问父对象

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  4. [Beginning SharePoint Designer 2010]Chapter 3 分析SharePoint页面

    本章概要: 1.SharePoint中主要页面类型 2.SharePoint如何组织页面 3.如何编辑母板页 4.SharePoint母板页中的主要内容占位符

  5. openwrt针对RT5350代码下载,配置和编译

    转载地址:http://blog.csdn.net/dean_gdp/article/details/37091685 近期买了块官方板的RT5350: 先介绍代码下载.下面命令都是用登录用户运行,无 ...

  6. DNS递归查询和迭代查询的差别

    转载地址:http://blog.csdn.net/wuchuanpingstone/article/details/6720723 递归查询和迭代查询的差别 (1)递归查询 递归查询是一种DNS s ...

  7. phpstudy配置多个域名

    phpstudy配置多个域名 1.端口配置对应的文件为PHPTutorial\Apache\conf\httpd.conf 直接在这个文件里面搜索localhost就可以搜到上图对应的代码那一块 第一 ...

  8. C#+HtmlAgilityPack+Dappe

    C#+HtmlAgilityPack+Dappe (转发请注明来源:http://www.cnblogs.com/EminemJK/) 最近因为公司业务需要,又有机会撸winform了,这次的需求是因 ...

  9. Oracle多表连接效率,性能优化

    Oracle多表连接,提高效率,性能优化 (转) 执行路径:ORACLE的这个功能大大地提高了SQL的执行性能并节省了内存的使用:我们发现,单表数据的统计比多表统计的速度完全是两个概念.单表统计可能只 ...

  10. Redis学习笔记(五) 基本命令:Hash操作

    原文链接:http://doc.redisfans.com/hash/index.html 学习前先明确一下概念,这里我们把Redis的key称作key(键),把数据结构hash中的key称为fiel ...