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 ...
随机推荐
- Jersey(1.19.1) - Hello World, Get started with a Web application
1. Maven Dependency <properties> <jersey.version>1.19.1</jersey.version> </prop ...
- Linux 命令 - arp: 操作系统的 ARP 缓存
arp 命令可以查看 ARP 缓存或者手动添加.删除缓存中的条目. 命令格式 arp [-evn] [-H type] [-i if] -a [hostname] arp [-v] [-i if] - ...
- Contoso 大学 - 7 – 处理并发
原文 Contoso 大学 - 7 – 处理并发 By Tom Dykstra, Tom Dykstra is a Senior Programming Writer on Microsoft's W ...
- Memcached学习(二)
4.协议简介 Memcached服务与各客户端间通过Tcp链接通讯(也可通过Udp链接). 各客户端间与服务间不需要发送特别的命令关闭链接,只要在不需要的时候直接关闭链接即可.建议客户端与服务间保持长 ...
- ASP.NET下运用Memcached
对于大型网站的高并发,在ASP.NET网站下的session性能并不高,所以造成人们一种印象,大型WEB项目使用JAVA的错觉,致使很多人吐槽微 软不给力,其实这好比拉不出怪地球引力,本文介绍Memc ...
- Oracle 10g设置IP访问限制
出于数据安全考虑,对Oracle数据库的IP做一些限制,只有固定的IP才能访问. 修改 db_1/NETWORK/ADMIN/sqlnet.ora文件 增加以下内容(红色表示注释): #开启ip限制功 ...
- OC1_协议语句
// // Programmer.h // OC1_协议语句 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhan ...
- 利用js来实现一些常用的算法
示例代码中的arr指的是给出的数组,s指的是数组的起始坐标0,end指的是数组的最后一个坐标arr.length-1,n指的是要查找的数字 查找某个值: 1.线性法 function findInAr ...
- spring读取prperties配置文件(1)
博客地址http://www.cnblogs.com/shizhongtao/p/3438431.html 属性文件命名是*.properties,在java中,用类java.util.Propert ...
- 致vi老大 2011.1
文/安然 亲爱的,你即将离去 飞机起飞的一刻, 请珍藏起我们2010的回忆 桃源仙谷,曾留下我们踏青的足迹 难忘,石头上小憩 小北门外,我们在大排档里尽情欢喜 见证,杯盘狼藉 饺子店,是冬日里四面八方 ...