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

    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 ≤ nxi ≠ 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.

    Examples
    input
    4 3
    2 1
    1 3
    4 3
    output
    1
    input
    5 5
    2 1
    1 3
    2 3
    2 5
    4 3
    output
    0
    input
    6 5
    1 2
    2 3
    4 5
    4 6
    5 6
    output
    1
    Note

    In the first sample the following road orientation is allowed: .

    The second sample: .

    The third sample: .

    一开始觉得 是联通分量的个数-1  后来画图发现,每个联通分量如果存在环,那么他就没有 separate点。

    一开始用并查集做,没调出来.....好弱..

    /* ***********************************************
    Author        :
    Created Time  :2016/4/2 16:53:35
    File Name     :346e.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 100010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    int vis[maxn];
    int n,m;
    vector<int>v[maxn];
    int mark=0;
    void dfs(int u,int fa){
        if(vis[u]){
            mark=1;return;
        }
        vis[u]=1;
        for(int i=0;i<v[u].size();i++){
            int V=v[u][i];
            if(V==fa)continue;
            dfs(V,u);
        }
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int x,y;
        while(cin>>n>>m){
            cle(vis);mark=0;
            for(int i=1;i<=m;i++){
                scanf("%d %d",&x,&y);
                v[x].push_back(y);
                v[y].push_back(x);
            }
            int ans=0;
            for(int i=1;i<=n;i++){
                mark=0;
                if(!vis[i]){
                    dfs(i,-1);
                    if(mark==0)ans++;
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    ETL利器Kettle实战应用解析系列一【Kettle使用介绍】
    彻底理解webservice SOAP WSDL
    5天玩转C#并行和多线程编程 —— 第三天 认识和使用Task
    5天玩转C#并行和多线程编程 —— 第一天 认识Parallel
    Data Leakage 因果性
    一张图,关于 Bayes error rate,贝叶斯错误率等的分析
    玩转Node.js单元测试
    Fundebug上线Node.js错误监控啦
    聊聊"jQuery is not defined"
    深究WeixinJSBridge未定义之因
  • 原文地址:https://www.cnblogs.com/pk28/p/5348031.html
Copyright © 2011-2022 走看看