【codeforces 505D】Mr. Kitayuta's Technology
【题目链接】: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的更多相关文章
- 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter
[题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...
- 【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化
神题……胡乱讲述一下思维过程……首先,读懂题.然后,转化问题为构造一个长度为|T|+n的字符串,使其内含有T这个子序列.之后,想到一个简单的dp.由于是回文串,我们就增量构造半个回文串,设f(i,j, ...
- 【codeforces 255D】Mr. Bender and Square
[题目链接]:http://codeforces.com/problemset/problem/255/D [题意] 给你一个n*n的方框; 给你一个方块;(以下说的方块都是单位方块) 每一秒钟,可以 ...
- 【CF505D】Mr. Kitayuta's Technology
题目大意: 在一个有向图中,有n个顶点,给出m对数字(u,v)表示顶点u和顶点v必须直接或者间接相连,让你构造一个这样的图,输出最少需要多少条边. 挖坑待填 官方题解链接:http://codefor ...
- 【CF506E】Mr. Kitayuta's Gift dp转有限状态自动机+矩阵乘法
[CF506E]Mr. Kitayuta's Gift 题意:给你一个字符串s,你需要在s中插入n个字符(小写字母),每个字符可以被插在任意位置.问可以得到多少种本质不同的字符串,使得这个串是回文的. ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- CodeForces 506B/505D Mr. Kitayuta's Technology
Portal:http://codeforces.com/problemset/problem/506/B http://codeforces.com/problemset/problem/505/D ...
- 【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 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
随机推荐
- 【codeforces 803E】Roma and Poker
[题目链接]:http://codeforces.com/contest/803/problem/E [题意] 给你一个不完整的胜负平序列(不完整是指中间有些地方为问号,让你自己选择胜负平) 让你复原 ...
- NYIST 119 士兵杀敌(三)
士兵杀敌(三)时间限制:2000 ms | 内存限制:65535 KB难度:5 描述南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进行比较,计算出 ...
- ZOJ 2314 Reactor Cooling
Reactor Cooling Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...
- HDU5926 Mr. Frog’s Game
/* HDU5926 Mr. Frog’s Game http://acm.hdu.edu.cn/showproblem.php?pid=5926 杂题水题 * */ #include <cst ...
- Python Study (05)装饰器
装饰器(decorator)是一种高级Python语法.装饰器可以对一个函数.方法或者类进行加工.在Python中,我们有多种方法对函数和类进行加工,比如在Python闭包中,我们见到函数对象作为某一 ...
- ScrollView+RadioGroup
今天要分享一下关于动态添加RadioButton的东西: 要实现的效果就是,这个控件可以左右滑动,里面的按钮都是互斥的,类似RadioButton,我的第一反应就是用ScrollView+RadioG ...
- 朴素的UNIX之-调度器细节
0.多进程调度的本质 我们都知道UNIX上有一个著名的nice调用.何谓nice,当然是"好"了.常规的想法是nice值越大越好,实际上,nice值越好,自己的优先级越低.那么为何 ...
- 【转】SQL语句删除和添加外键、主键
--删除外键 语法:alter table 表名 drop constraint 外键约束名 如: alter table Stu_PkFk_Sc drop constraint FK_s alter ...
- C++代码审查---参考林锐高质量C/C++
功能模块名称 审查人 审查日期 代码名称 代码作者 文件结构 重要性 审查项 结论 头文件和定义文件的名称是否合理? 头文件和定义文件的目录结构是否合理? ...
- LeetCode224. Basic Calculator (用栈计算表达式)
解题思路 用两个栈分别存字符和数字. 顺序读入字符,处理方式分为字符和数字两种. 处理字符分为')'和非')'两种. 处理数字需要读取字符栈栈顶,分为'+'.'-'和非'+'.'-'. 代码 clas ...