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 ...
随机推荐
- 【TOMCAT】Tomcat gzip压缩传输数据
概述 由于我们项目的三维模型文件非常大,为了提高传输速度,在服务端对其做zip压缩处理非常有必要,能够极大的提高传输速度. 配置 首先需要修改web.xml中请求的数据文件的mime类型的mappin ...
- js和jQuery创建元素和把元素插入到文档中所用的方法
js创建元素: document.createElement(" 创建的元素"); //“创建的元素”指:p ,h1,div,span........ js插入元素: docu ...
- 第一篇、Swift_搭建UITabBarController + 4UINavigationController主框架
import UIKit class MainViewController: UITabBarController { override func viewDidLoad() { super.view ...
- Cocos2d-x数据持久化-修改数据
修改数据时,涉及的SQL语句有insert.update和delete语句,这3个SQL语句都可以带参数.修改数据的具体步骤如下所示.(1) 使用sqlite3_open函数打开数据库.(2) 使用s ...
- swift闭包传值
不知道原理,就知道这么用的,皮毛上的那一点. 寻思着把以前的项目改成swift的,结果了,,, 反向传值 一. //类似于OC中的typedef typealias sendValueClosure= ...
- OC5_类别
// // NSString+Reverse.h // OC5_类别 // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 ...
- 浅谈css中的position属性
我觉得吧,css如果不考虑浏览器的兼容问题的话,最让人头疼的应该就是position了,反正我是这么觉得的,为了能基本上搞清楚position的几种情况,我找了一些资料,做了一个小实验,下面是实验的过 ...
- office2013发布博客
0, 喜欢用world记笔记,并查上一些配图.但是再想重新发到博客上,图片不得不重新上传十分蛋疼. world直接发布博客功能太棒了,直接绑定账号和url就可以发送了,爽YY!!! 1,新建一个博客文 ...
- must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int)问题
依照视屏编写代码如下 class MyButtonListener implements OnClickListener{ @Override public void onClick(View v){ ...
- window Jconsole链接到CenOS 监控Tomcat
前提条件 windows环境已安装JDK CentOS 环境已安装Tomcat 进入目录 /usr/local/TC6_A/bin 目录下修改 catalina.sh vi catalina.sh 添 ...