• Codeforces Round #346 (Div. 2) E


    题目链接:

    题目

    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
    ",ans);
    	return 0;
    }
  • 相关阅读:
    css3边框阴影属性
    web移动端浮层滚动阻止window窗体滚动JS/CSS处理
    css3字体尺寸font-size-adjust属性
    css3文本溢出使用省略标记(…)
    exports 和 module.exports 的区别
    微信小程序——try {} catch (e) {}
    flex布局
    微信小程序——data-*自定义属性
    CSS 隐藏滚动条 但可以滚动
    event.target 和 event.currentTarget 的区别
  • 原文地址:https://www.cnblogs.com/fenice/p/5628669.html
Copyright © 2020-2023  润新知