zoukankan      html  css  js  c++  java
  • 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 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.

    Input

    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.

    Output

    Print a single integer — the minimum number of separated cities after the reform.

    Sample Input

    4 3
    2 1
    1 3
    4 3

    Sample Output

    1

    Hint

    题意

    给你一个图,现在要你给这个图里面的边定方向,使得入度为0的点最少。

    题解:

    对于一个连通块而言,如果里面存在一个环,那么必然所有点的入度都可以大于等于1

    否则的话,就存在一个点的入度为0。

    dfs一波就好了。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+7;
    vector<int> E[maxn];
    int n,m,flag;
    int vis[maxn];
    void dfs(int x,int fa)
    {
        if(vis[x])
        {
            flag = 1;
            return;
        }
        vis[x]=1;
        for(int i=0;i<E[x].size();i++)
        {
            int v=E[x][i];
            if(v==fa)continue;
            dfs(v,x);
        }
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            E[x].push_back(y);
            E[y].push_back(x);
        }
        int ans = 0;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                flag = 0;
                dfs(i,-1);
                if(!flag)ans++;
            }
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    mysql联合主键,也就是两个数据字段一起做主键的情况
    PHP细节,empty,is_null,isset,if()
    PHP细节,PHP手册中常见的一句话:该函数是二进制安全的
    git和github的学习
    用WPS查看两篇word文档异同之处
    js全角字符转为半角字符
    坑(十七)—— Linux无法挂载NTFS格式的U盘
    subprocess模块
    吴裕雄--天生自然--Go 语言学习笔记--Go 语言数组
    吴裕雄--天生自然--Go 语言学习笔记--Go 语言变量作用域
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5342176.html
Copyright © 2011-2022 走看看