codeforces 659E New Reform

CodeForces 659E New Reform (图的遍历判环) Description 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 y 阅读详情

E. New Reform
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.

Input

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.

Output

Print a single integer — the minimum number of separated cities after the reform.

Examples
input
4 3
2 1
1 3
4 3
output
1
input
5 5
2 1
1 3
2 3
2 5
4 3
output
0
input
6 5
1 2
2 3
4 5
4 6
5 6
output
1
Note

In the first sample the following road orientation is allowed: , , .

The second sample: , , , , .

The third sample: , , , , .



只要有一个环的连通分支,那么肯定可以做到环内的点都有入度,对于环外的点可以看成DFS遍历的生成树的节点,肯定可以有一条有向的通路,

通路上的点都有入度,这个连通分支可以做到最少为0个没有入度的点

如果没有环的连通分支,那么只需要找一个点作为生成树的根节点,那么就可以做到最少只有1个点没有入度



#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int maxn = 100000 + 10;
vector<int> G[maxn];
bool vis[maxn], flag;
int n, m, ans;

void dfs(int s, int pre) {
	vis[s] = true;
	for (int i = 0; i < G[s].size(); i++) {
		if (vis[G[s][i]] && G[s][i] != pre) {
			flag = true;
		}
		else if (!vis[G[s][i]]) {
			dfs(G[s][i], s);
		}
	}
}

int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	memset(vis, false, sizeof(vis));
	ans = 0;
	for (int i = 1; i <= n; i++) {
		if (!vis[i]) {
			flag = false;
			dfs(i, i);
			if (!flag) ans++;
		}
	}
	printf("%d\n", ans);
	return 0;
}





uniapp监听扫码枪键盘事件|无输入框式监听 一般的扫码枪通过USB或蓝牙连接手机或电脑,充当的是一个外接设备。当扫码后,扫码枪会自动识别内容,然后向连接的电脑或手机发送键盘事件keydown或keyup。 阅读详情

相关推荐

sql server安装并SSMS连接

  SQL Server是一种广泛应用于网络业务数据处理的关系型数据库管理系统。SQL Server 2016是微软公司研发的新一代旗舰级数据库和分析平台,突出高级分析和丰富可视化,并融合了关键创新功能。其可提供对企业基础架构最高级别的支持,专门针对关键业务应用的多种功能与解决方案提供最高级别的可用性,并提供更多、更全面的功能以满足不同人群对数据的需求,包括支持来自不同网络环境的数据交互、全面的自助分析等创新功能。百度搜索sql server,在官网下载 地址是:SQL Server 下载| Microso

jiankang66的博客 5205

Codeforces 659E New Reform【DFS】

题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无法到达的话即入度为0。 DFS判断每一个连通块中是否存在环,如果存在环,就能保证环中每个点的入度都大于等于1。否则,有头有尾,头的入度为0。 代码: ...

An55511的博客 264

Sentaurus TCAD 实战指南:几何、掺杂与物理模型对Ion/Ioff的协同调控

本文详细介绍了Sentaurus TCAD在器件仿真中的实战应用,重点解析了Ion(开态电流)与Ioff(关态电流)的协同优化方法。通过几何结构设计、掺杂工程和物理模型选择三大要素的精细调整,实现器件性能的显著提升。文章结合28nm和7nm工艺案例,展示了如何平衡Ion与Ioff的矛盾关系,为TCAD工程师提供实用指导。

weixin_29159711的博客 141

Codeforces 659E 找环+(并查集/dfs)

题目: http://codeforces.com/problemset/problem/659/E 题意: 给你一个图,现在要你给这个图里面的边定方向,使得入度为0的点最少。 分析: 对于一个连通块而言,如果里面存在一个环,那么必然所有点的入度都可以大于等于1 否则的话,就存在一个点的入度为0。 并查集: #include <bits/stdc...

205

Altium Designer铺铜陷阱:为什么你的死铜总是清除不干净?

本文深入探讨Altium Designer中铺铜操作导致死铜无法清除的常见问题,分析死铜形成的五大诱因,并提供高级参数优化和网络连接性验证技巧。通过调整边界计算精度和泪滴算法设置,有效解决死铜残留问题,提升PCB设计质量与制造良率。

weixin_29169505的博客 367

Materials Studio实战技巧(八)——利用片段库高效构建有机金属配合物

本文详细介绍了在Materials Studio中利用Fragment Browser(片段浏览器)高效构建有机金属配合物的实战技巧。通过调用预置的Metal Templates和Ligands库,用户可以快速搭建如茂金属催化剂等复杂分子骨架,并利用对称性操作精确控制Meso与Rac构型。文章还涵盖了自定义片段库的创建与使用,能显著提升建模效率,适用于计算化学与材料模拟领域的研究者。

wine的专栏 948

Codeforces Round #346 (Div. 2) E. New Reform dfs

E. New Reform 题目连接: http://www.codeforces.com/contest/659/problem/E Description Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is c...

weixin_34255055的博客 160

Openwork 入门指南:开源 AI 桌面助手快速上手

Openwork是一款开源的AI桌面代理工具,提供类似Claude Cowork的免费替代方案。它支持本地运行,确保隐私安全,并能高效处理文件整理、文档读写等任务。支持多种AI模型,通过MIT许可完全免费。安装简单,仅需运行命令行或下载桌面版,需配置API Key后即可使用。操作直观,支持多步骤规划和权限控制,适合自动化重复工作。目前主要支持macOS,未来将扩展至Windows/Linux。建议从简单任务入手,逐步掌握这一高效的数字助手工具。

3870

CodeForces - 659E New Reform (邻接表+dfs)

E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Berland has n cities connected by m bidirectional roads. No road con...

暖心的博客 284

DaVinci配置DID之二:工具链配置

本系列主要基于DaVinCi工具链来展开具体DID读写的配置以及最终实现。1,CDD准备2,工具链配置3,代码实现4,验证结果本文讲述流程2话不多说,走你~博主也是AutoSar小白,如果哪里表达的有问题,还请大佬们指点指点哈。接下来,让我们在下章关于代码具体实现的博文再相会哈~

weixin_48498880的博客 5749

一步一步教你如何安装IP-Guard(客户端WINDOWS篇)

1、安装完服务器后,在服务器右下角有一个IP-GUARD图标:如图 2、点图标右键选注册: 3、注册成功后, 4、在开始菜单里选生成客户端安装包。 5、生成客户端安装包成功,安装名字可以任意,但是最好是英文名字。 6、将客户端安装程序复制到桌面。 7、退出所有杀毒软件、安全卫士、电脑管家等然后运行 8、安装完成后,立即重启计算机。IPguard客户端安装完成。 9、这样在控制里就可以看到已经安装客户端的计算机,在线显示蓝色,灰色表示计算机已经离线货关机 ...

IP-Guard专栏 5664

Codeforces 659E New Reform 【DFS 求环】

题目链接:Codeforces 659E New Reform E. New Reform time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Berland has n cities connected by m bidirec

世界很大 558

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

codeforces 659 E New Reform

E. New Reform 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

哇咔咔 1154

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

并查集 - New Reform - CodeForces - 659E

并查集 - New Reform - CodeForces - 659E 题意: 给定一个n个点,m条边的无向图。给定一个n个点,m条边的无向图。给定一个n个点,m条边的无向图。 现要将所有边均改为有向边,若有某个点的入度为0,则认为该点被孤立了。现要将所有边均改为有向边,若有某个点的入度为0,则认为该点被孤立了。现要将所有边均改为有向边,若有某个点的入度为0,则认为该点被孤立了。 计算出最少会有多少个点被孤立。计算出最少会有多少个点被孤立。计算出最少会有多少个点被孤立。 首行输入n,m,表示n个点,m条边

njuptACMcxk的博客 254

inori mmd 7z

楪祈的模型资源....................................................................................................................................................................................................................

Codeforces 659E New Reform(并查集维护连通块)

题目链接 参考博客 题意 有一些双向边连接的点,现在要把每条边变成单向边,问最少有多少点没有入度 思路 1.可以建成一个有向图,可能有k个联通块,如果一个联通块没有环,就说明这个联通块,至少有一个城市单独的,因此就化成找联通块和环的问题 2.联通块的话可以用并查集来维护,然后用cir[maxn]数组来标记是否有环,如果这个联通块的根节点存在环,那么该联通块不存在单独的城市,如果不存在环的话ans+...

Novice_的博客 339

CodeForces 659E New Reform (并查集)

题意:n个点m条边,现在要把这些边变为单向,求没有边连入的最少的点。 题解:并查集 对于并查集,其实就是一棵树,从根节点往下,那么只有根节点没有边连入,但是对于自环来说,并不存在这样的点。所以只要判断一下在构成集合的时候是否存在自环即可,若存在自环,则将根节点标记。 最后答案就是不存在自环的集合数量。 #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<cstdio> #include<string> #

gongyuandaye的博客 248

【Codeforces659E】New Reform【BFS】

【题目链接】 题意:给出一个n个点,m条边的无向图,现在你可以任意规定每个边的方向,使得这个图变为有向图,要求入度为0的点尽量少。 仔细分析一下就看出来了。 如果一个连通块里有n - 1条边,即一棵树,那么答案为1。 如果大于n - 1条边,那么可以在环上选一个起点规定方向,那么答案为0。 BFS一次就好了。 /* Footprints In The Blood Soaked

BraketBN 650

CodeForces - 659E New Reform(无向图找环)

题目链接:http://codeforces.com/problemset/problem/659/E 题意:给一个不一定连通的无向图,问将它变成有向图以后图中最少又多少个单独的点。其中单独的点指的是这个点的入度为0。 思路:就是找出题目给的图中有几个连通图,如果这个连通图有环,答案+1。所以,题目转化成了无向连通图中找环的问题。至于这个,可以用dfs或者并查集都可以做,我用两种方法都试了一下,效...

qq_43423395的博客 252

New Reform [CodeForces-659E]

题意 有n个点和m条不确定方向的边。 现在要求确定这m条不确定方向的边,使得入度为0的点的个数最少。 输出最小的个数。 分析 如果某个点保证入度&gt;0 那么可以把其它所有连向的点的入度都&gt;0 n,m&gt;=10^5不算特别大,感觉要么O(n)要么O(n log n) 可以一个一个联通块,考虑某一条边的方向,然后跑两次找较小值然后加入答案。 不过方案不能确定。 如...

Umbrella__的博客 397

CodeForces 659E New Reform

题意:给你一个无向图,现在要求你把边改成有向的, 使得入度为0的点最少,输出有多少个点入度为0 思路:脑补一波结论,如果有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就可以了 #include #include #include #include #include #include #include #include #include #in

围巾的ACM博客 1221

CodeForces 659E New Reform (DFS)

http://codeforces.com/contest/659/problem/E 题意:n个点中存在m条无向边,如果在这m条边加上方向,方向唯一,但是可以是任意的,求入度为0的点的个数最多是多少 思路:循环搜索每一个点,如果存在环,则环内的点入度都不为0,环外的点如果能由环内的点到达,则入度不为0 #include #include #include #include #i

大太阳 833

[图中找环] Codeforces 659E New Reform

New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Berland hasncities connected bymbidirectional ro...

dixiangee374493的博客 184
上一篇: hdu5676 ztr loves lucky numbers DFS+二分
下一篇: PAT (Advanced Level) L1-6. 连续因子 for循环暴力
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址