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: ,
,
,
,
.
解题报告:
1、我的解决图形问题的数据结构,a、矩阵邻接图,b、STL,(vector)
#include <bits/stdc++.h> using namespace std; int n,m;
int ans=;
int ans1; bool vis[];///是否访问过,若访问过,则构成回路,则没有孤立的点 vector<int>vec[];///记录各点的路径 void dfs(int pos,int pre)
{
if(vis[pos]) ///如果构成回路,ans1=0;
{
ans1=;
return ;
} vis[pos]=; ///广搜下面的点
for(int i=;i<vec[pos].size();i++)
{
if(vec[pos][i]!=pre)///单向路径
dfs(vec[pos][i],pos);
}
} int main()
{
scanf("%d%d",&n,&m); ///存各个点的路径
for(int i=;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vec[b].push_back(a);
} ///搜索各个点
for(int i=;i<=n;i++)
{
///搜索过就不用搜了
if(vis[i])
continue;
else
{
ans1=; ///可能不能构成回路,先置为1,准备多一个孤立点
dfs(i,); ///开始搜索这条路
ans=ans+ans1;
}
}
printf("%d\n",ans);
return ;
}
E. New Reform_贪心,深搜,广搜。的更多相关文章
- HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)
THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- poj3083 Children of the Candy Corn 深搜+广搜
这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...
- DFS-BFS(深搜广搜)原理及C++代码实现
深搜和广搜是图很多算法的基础,很多图的算法都是从这两个算法中启发而来. 深搜简单地说就是直接一搜到底,然后再回溯,再一搜到底,一直如此循环到没有新的结点. 广搜简单地说就是一层一层的搜,像水的波纹一样 ...
- PTA 7-6 列出连通集(深搜+广搜)
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1 ...
- NYOJ-58最少步数,广搜思想!
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 -> Link <- 这个题深搜广搜都是可以的,迷宫已经给出了,就看怎么做了:一般起点终点确定用广搜 ...
- P2668 斗地主 贪心+深搜
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 图的基本操作(基于邻接矩阵):图的构造,深搜(DFS),广搜(BFS)
#include <iostream> #include <stdio.h> #include <cstdlib> #include <cstring> ...
随机推荐
- 使用Spring和JQuery实现视频文件的上传和播放
Spring MVC可以很方便用户进行WEB应用的开发,实现Model.View和Controller的分离,再结合Spring boot可以很方便.轻量级部署WEB应用,这里为大家介绍如何使用Spr ...
- 第一章javascript简介
javascript 当诞生于1995 最开始是目的是处理在服务器端进行的表单验证:让其在服务器端验证改为在服务端验证,大大提高速度(当时网络慢) 如今javascript是一门功能全面的语言,包含闭 ...
- silverlight vs2010 需要缺少的web组件才能加载
在打开一个开源的Silverlight项目是遇到如图所示的问题,点击是后没有反应. 查了资料,需要安装微软的webpi(Microsoft Web Platform Installer) webpi的 ...
- spark常用算子总结
算子分为value-transform, key-value-transform, action三种.f是输入给算子的函数,比如lambda x: x**2 常用算子: keys: 取pair rdd ...
- javascrip基础学习
JS是一种解释性脚本语言,在网页开发用经常用到(HTML CSS),用于控制网页的行为.现在RTT的柿饼UI也是用JS来开发的,所以很有必要学习一下. 注释:// ./* */ 语句分行: 折行\ ...
- 17082 两个有序数序列中找第k小(优先做) O(logn)
17082 两个有序数序列中找第k小(优先做) 时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC;VC Description 已 ...
- php数组转json对象并保留数字索引
本文要解决的问题是php不带索引的数组转换为json对象(而非默认的json数组) php中数组转json使用的方法是系统自带的:json_encode php中数组转json的规则是:当没有指定索引 ...
- C# FTPHelper帮助类
网上的FTPHelper类感觉用起来不方便,而且代码的质量也不高,因此自己重新写了一个FTPHelper.此文之前是发布在我的CSDN博客中的,现在转过来. 主要就是借鉴了DbHelper的Creat ...
- Mysql远程连接授权IP
新增法 我们现在增加一个'username'用户,密码为'password',让其能够从外部访问MYSQL. grant all on * to 'username' identified by ...
- Java基础入门 - Hello world
JDK安装完成后,将一下代码写入文件HelloWorld.java中 // HelloWorld.java public class HelloWorld { public static void m ...