Codeforces Round #346 (Div. 2) E - New Reform 无相图求环
题目链接:
题目
E. New Reform
time limit per test 1 second
memory limit per test 256 megabytes
inputstandard input
outputstandard 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.
样例
input
4 3
2 1
1 3
4 3
output
1
题意
给你一个无向图,现在要把双向边变成有向边,问使得入度为零的边最小的方案
题解
对每个连通分量求环
如果存在环,则这个连通分量的贡献为1
否则,这个连通分量的贡献为0
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=1e5+10;
int n,m;
vector<int> G[maxn];
int vis[maxn];
bool dfs(int u,int fa){
vis[u]=1;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
if(vis[v]||dfs(v,u)) return true;
}
return false;
}
int main(){
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
while(m--){
int u,v;
scanf("%d%d",&u,&v),u--,v--;
G[u].push_back(v);
G[v].push_back(u);
}
int ans=0;
for(int i=0;i<n;i++){
if(!vis[i]){
if(!dfs(i,-1)) ans++;
}
}
printf("%d\n",ans);
return 0;
}
Codeforces Round #346 (Div. 2) E - New Reform 无相图求环的更多相关文章
- 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 ...
- Codeforces Round #346 (Div. 2)E - New Reform(DFS + 好题)
E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #346 (Div. 2) E. New Reform
E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #346 (Div. 2) E题 并查集找环
E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to it ...
- Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)
Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...
- Codeforces Round #346 (Div. 2) A Round-House
A. Round House 题目链接http://codeforces.com/contest/659/problem/A Description Vasya lives in a round bu ...
- Codeforces Round #375 (Div. 2) E. One-Way Reform 欧拉路径
E. One-Way Reform 题目连接: http://codeforces.com/contest/723/problem/E Description There are n cities a ...
- Codeforces Round #346 (Div. 2) A. Round House 水题
A. Round House 题目连接: http://www.codeforces.com/contest/659/problem/A Description Vasya lives in a ro ...
- Codeforces Round #346 (Div. 2)
前三题水 A #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 + 5; int main() { int ...
随机推荐
- boost.ASIO-可能是下一代C++标准的网络库
曾几何时,Boost中有一个Socket库,但后来没有了下文,C++社区一直在翘首盼望一个标准网络库的出现,网络上开源的网络库也有不少,例如Apache Portable Runtime就是比较著名的 ...
- jQuery性能优化(转)
摘要:jQuery是我们经常使用的强大的JS类库,因此它的性能优化十分重要,下面就重几点来说明 原文作者:szyuxueliang 原文地址:http://www.cnblogs.com/yxl ...
- response小结(三)—输出随机图片(验证码功能实现)
本文是一个很简单的向网页上输出验证码的实现喲!通过注释解释清楚了每一步! Myeclipse下的Package Explorer显示文件结构如下: ResponseDemo.java实现了输出随机图片 ...
- 搭建私有git代码托管服务就是这么简单(简单5步)
部署一个git代码托管服务就是这么简单 --基于阿里云ecs以docker容器运行gogs代码托管服务 部署步骤: 1.新建ecs云主机,选定操作系统为ubuntu 12.4tls 2.搭建docke ...
- boost库区间range基本原理及使用实例
由 www.169it.com 搜集整理 区间的概念类似于STL中的容器概念.一个区间提供了可以访问半开放区间[first,one_past_last)中元素的迭代器,还提供了区间中的元素数量的信息. ...
- Objective-C 【完整OC项目-购票系统-系统分析-代码实现】
电影院买票系统/演唱会买票系统 需求分析: 首先我们进入系统,然后会选择买电影票还是买演唱会票,所以这牵扯两个系统的合成.但是我们知道都是买票系统,所以我们可以先创建一个类,属于购买电影票和演唱会的票 ...
- Libcurl笔记三
一,post请求和回报处理 //"host/path?extra" //strHttp=" http://portal.liuhan.com:/web/getConfig ...
- 《java编程思想》--多线程基础--Runnable
一.简单说下Runnable是什么 1.它是一个接口 2.只提供了run方法 3.这个接口提供了一个协议:实现这个接口的类是active的(不必成为Thread的子类) 4.run方法没有返回值 /* ...
- linux 安装mysql后修改密码出现问题
新安装的mysql 执行命令时候出现错误: 一 错误信息: ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using ...
- 韩顺平细说Servlet视频系列意外收获之用命令行编译带有包的java类解决方案
命令行编译带有包的java类 在命令行编译这一块,基本上都是新手入门时了解一下,然后就直奔IDE而去.这样固然没错,就怕那些--.然后今天在视频中看到了这种方法,觉得可能会用到,所以就记录下来了,以备 ...