codeforces 659E E. New Reform(图论)
题目链接:
1 second
256 megabytes
standard input
standard output
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
The first line of the input contains two positive integers, n and m — the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
Print a single integer — the minimum number of separated cities after the reform.
4 3
2 1
1 3
4 3
1
5 5
2 1
1 3
2 3
2 5
4 3
0
6 5
1 2
2 3
4 5
4 6
5 6
1
In the first sample the following road orientation is allowed: , , .
The second sample: , , , , .
The third sample: , , , , .
题意:
给一个图的描述,注意有没有连接的点,把双向边变成单向边,问最后最少有多少个点没有边连向它;
思路:
贪心可以发现,没有连接的点首先就是,然后形成环的最后都能有边连向它们,然后就是像拓扑排序那样减入度,删边找到最后剩下的那些点;
AC代码:
/*
2014300227 659E - 36 GNU C++11 Accepted 78 ms 6668 KB
*/ #include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
int n,m,x,y,flag[N],num[N],vis[N];
vector<int>ve[N];
queue<int>qu;
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
ve[x].push_back(y);
ve[y].push_back(x);
num[x]++;
num[y]++;
}
for(int i=;i<=n;i++)
{
if(num[i]==)
{
qu.push(i);
}
}
while(!qu.empty())
{
int to=qu.front();
vis[to]=;
qu.pop();
if(num[to]){
flag[to]=;
int len=ve[to].size(),b;
for(int i=;i<len;i++)
{
b=ve[to][i];
if(!vis[b])
{
num[b]--;
if(num[b]==)
{
qu.push(b);
}
break;
}
}
}
}
int ans=;
for(int i=;i<=n;i++)
{
if(flag[i]==&&num[i]==)
{
ans++;
}
}
cout<<ans<<"\n"; return ;
}
codeforces 659E E. New Reform(图论)的更多相关文章
- CodeForces 659E New Reform
题意:给你一个无向图,如今要求你把边改成有向的. 使得入度为0的点最少,输出有多少个点入度为0 思路:脑补一波结论.假设有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就能够了 ...
- Codeforces 659E New Reform【DFS】
题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...
- CodeForces 659E New Reform (图的遍历判环)
Description Berland has n cities connected by m bidirectional roads. No road connects a city to itse ...
- [图中找环] Codeforces 659E New Reform
New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- codeforces 659E . New Reform 强连通
题目链接 对于每一个联通块, 如果有一个强连通分量, 那么这个联通块对答案的贡献就是0. 否则对答案贡献是1. #include <iostream> #include <vecto ...
- codeforces 723E:One-Way Reform
Description There are n cities and m two-way roads in Berland, each road connects two cities. It is ...
- Codeforces 444A DZY Loves Physics(图论)
题目链接:Codeforces 444A DZY Loves Physics 题目大意:给出一张图,图中的每一个节点,每条边都有一个权值.如今有从中挑出一张子图,要求子图联通,而且被选中的随意两点.假 ...
- 【codeforces 732F】Tourist Reform
[题目链接]:http://codeforces.com/contest/732/problem/F [题意] 给你一张无向图; n个点,m条边; 让你把这张图改成有向边 然后定义r[i]为每个点能够 ...
- 【codeforces 723E】One-Way Reform
[题目链接]:http://codeforces.com/contest/723/problem/E [题意] 给你一个无向图; 让你把这m条边改成有向图; 然后使得出度数目等于入度数目的点的数目最多 ...
随机推荐
- NodeJS待重头收拾旧山河(重拾)
介绍 Node.js®是一个基于Chrome V8 JavaScript引擎构建的JavaScript运行时. Node.js使用事件驱动的非阻塞I / O模型,使其轻便且高效. Node.js的包生 ...
- 【翻译自mos文章】执行utlpwdmg.sql之后报ORA-28003, ORA-20001, ORA-20002, ORA-20003, ORA-20004 错误
执行utlpwdmg.sql之后报ORA-28003, ORA-20001, ORA-20002, ORA-20003, ORA-20004 错误. 适用于: Oracle Server - Ente ...
- Cocoapods完整使用篇
温馨提示:在篇文章中所使用的Xcode版本为Xcode7. 一.什么是CocoaPods? 简单来说,就是专门为iOS工程提供对第三方库的依赖的管理工具,通过CocoaPods,我们可以单独管理每 ...
- openwrt patch
一: 这几天使用一款电信的4G网卡,发现了一些问题,所以决定打个pitch来解决问题,顺便把patch的生成与使用学习一下 二:安装patch的管理工具quilt 1. sudo apt-get in ...
- pycharm 5 注册码
BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJCSUczQ0xJSzZGIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...
- 开源大数据引擎:Greenplum 数据库架构分析
Greenplum 数据库是最先进的分布式开源数据库技术,主要用来处理大规模的数据分析任务,包括数据仓库.商务智能(OLAP)和数据挖掘等.自2015年10月正式开源以来,受到国内外业内人士的广泛关注 ...
- yum 安装apache php 使php支持memcached扩展
在公司上新项目的时候,无论生产环境还是测试环境,都会让运维安装php 环境(lamp/lnmp),并让php支持memcached 的扩展.这里搭建php环境其实主要就是搭建apache 和php.m ...
- 五个知识体系之-SQL学习-第四天
5. MySQL常用函数 5.1字符串函数 concat(s1,s2....,s3)合并字符串,如果参数有null,则返回null: CONCAT_WS(SEP,s1,s2…,sn) 合并字符串,并且 ...
- gulp的使用方法
---恢复内容开始--- 什么是gulp? Gulp.js是一个自动化构建工具,开发者可以使用它在项目开发过程中自动执行常见任务. 使用步骤: 1.全局安装gulp: npm install - ...
- slide.js
define(['jquery'], function (jquery) { function buildSmooth(config, motivateCallBack) { var timer = ...