【题目链接】: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. socketserver模块初识

    python提供了两个级别访问的网络服务: 低级的网络服务支持基本的socket,它提供了标准的BSD sockets API,可以访问底层操作系统socket接口的全部方法 高级别的网络服务模块so ...

  2. MAVEN项目的搭建

    MAVEN能为我们做什么? 1.Jar的声明式依赖性管理 2.项目的自动构建 搭建流程 环境配置 http://maven.apache.org/download.html 下载最新版本Maven 3 ...

  3. 【配置属性】—Entity Framework实例详解

    Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...

  4. 解决was6版本号过期问题

    原创作品.出自 "深蓝的blog" 博客,欢迎转载.转载时请务必注明出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...

  5. POJ 1091

    这题确实是好. 其实是求x1*a1+x2*a2+....M*xn+1=1有解的条件.很明显,就是(a1,a2,...M)=1了.然后,可以想象,直接求有多少种,很难,所以,求出选择哪些数一起会不与M互 ...

  6. Android error--No implementation found for native Lcomd

    在利用NDK编译Cpp执行时,出现了No implementation found for native Lcom等错误,调试好久,才发现 XXX.h和XXX.cpp.在XXX.cpp里#includ ...

  7. struts自己定义拦截器--登录权限控制

    说明:该自己定义的拦截器实现用户登录的权限控制. login.jsp--->LoginAction--重定向-->MainAction--->main.jsp 一.1.整体的步骤: ...

  8. 积跬步,聚小流------Bootstrap学习记录(1)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  9. codecombat之KithGard地牢19-37关代码分享

    codecombat中国游戏网址:http://www.codecombat.cn/ 全部代码为javascript代码分享 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 19 ...

  10. JavaScript的那些坑之变量提升

    想总结一下JS的变量提升特性,都是由于一道题.先上题. var name = 'World!'; (function () { if (typeof name === 'undefined') { v ...