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;
}





c语言【分糖果】2023蓝桥杯4124(枚举,递归,dfs) 但是这是不符合我们糖果刚好分完的条件的,我们应该往回退到上一层,也就是孩子6这层,然后j+1=3,孩子7分到3个第二种糖果,这也不符合我们糖果刚好分完的条件,然后就会再次回退到孩子6这里,然后j+1=4,让我们的孩子7获得5块第二种糖果,然后还是不符合糖果刚分完的条件,于是再一次退回到孩子6这里,然后就会有外循环i+1=1,j=1,孩子7分到1块第一种糖果1块第二块糖果......然后就是这样的不停地回溯,试探,直到把整棵树遍历一遍,并且记录所有的满足条件的组合。先放一下我的代码,我的代码超时啦救命! 阅读详情

相关推荐

VTK四视图联动避坑指南:解决vtkImagePlaneWidget窗宽窗位同步与切片序号更新的那些坑

本文详细解析了VTK四视图联动开发中的关键技术点与常见问题,包括切片位置同步、窗宽窗位同步的实现原理及回调机制设计。针对开发中遇到的回调事件不触发、渲染性能低下等问题,提供了实用的解决方案和优化技巧,帮助开发者高效构建医学影像和科学可视化应用。

weixin_42638178的博客 395

Codeforces 659E New Reform【DFS】

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

An55511的博客 262

SAP ABAP VL09 BAPI VL 216

ls_batch_item-fact_unit_denom = ls_lips_split-umvkn. "销售数量转换为 SKU 的值(分母)ls_batch_item-fact_unit_nom = ls_lips_split-umvkz. "销售数量转换成SKU的分子(因子)ls_batch_item-sales_unit_iso = ls_lips_split-meins. "被拆分DN基本单位。

ChampaignWolf的专栏 2304

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

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

205

RT-Thread Studio使用笔记(四):带片外Flash(W25Q128)的OTA(YModem)

前言 在学习OTA之前,请先参考上两篇博客写的关于SPI设备及W25Q128的使用方法,传送门: https://blog.csdn.net/tigerots/article/details/107423633 https://blog.csdn.net/tigerots/article/details/107933991 如何使用OTA 一. 制作BootLoader 打开Bootloader 在线获取地址,注册,并根据RT-thread官方文档生成BootLoader,官方文档已经写的很清楚,再此

tigerots的专栏 4016

手把手教你配置SAP PP供应区域(Supply Area):实现线边仓精准发料与物料流优化

本文详细解析了SAP PP供应区域(Supply Area)的配置方法,帮助企业实现线边仓精准发料与物料流优化。通过三级优先级逻辑和四步配置法,系统可自动识别最优组件来源位置,显著提升生产订单处理效率和库存准确率。文章还提供了典型问题解决方案和性能优化技巧,适用于离散制造和重复制造场景。

weixin_29275257的博客 438

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

200_matlab处理NCEP数据并画全球彩色的风场图

200_matlab处理NCEP数据并画全球彩色的风场图。

miaobo0的博客 1245

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...

暖心的博客 283

Flow Matching实战:5步搞定图像生成模型优化(附PyTorch代码)

本文提供了一份基于Flow Matching框架优化图像生成模型的实战指南。通过五个关键步骤,详细讲解了从环境配置、核心概念梳理到构建向量场网络、设计损失函数、编写训练循环以及选择ODE求解器进行采样的完整流程,并附有可运行的PyTorch代码,帮助开发者快速掌握这一高效的生成式建模技术。

weixin_29065959的博客 327

系统安全性评估 (SSA) 详细解析

系统安全性评估(SSA)是一种自下而上的验证过程,目的是确保系统设计中的每个功能、子系统、硬件和软件的实现,符合早期通过 FHA 和 PSSA 设定的安全性需求。它通过对已实现的系统进行系统性综合评价,检验各项设计和安装是否符合预期的安全性目标,最终支持飞机的适航认证。飞行控制系统是飞机安全性设计的核心子系统之一,负责控制飞机的姿态和方向。为了确保飞行控制系统能够在各种飞行条件下保持安全运行,SSA 被用于验证该系统的设计是否满足 FHA 中设定的安全性目标。

qq_41854911的博客 1660

【Codeforces659E】New Reform【BFS】

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

BraketBN 648

【用unity实现100个游戏之7】实现一个类《杀戮尖塔》的卡牌回合制游戏(附项目源码)

【用unity实现100个游戏之7】实现一个类《杀戮尖塔》的卡牌回合制游戏(附项目源码)

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 1万+

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

世界很大 557

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

并查集 - New Reform - CodeForces - 659E

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

njuptACMcxk的博客 253

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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的博客 245

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博客 1220

CodeForces 659E New Reform (DFS)

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

大太阳 831

[图中找环] 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的博客 182
上一篇: 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、付费专栏及课程。

余额充值