D - Hoppers

You are part of an elite hacking group who has just invented a new type of malware, called the hoppers. Hoppers can operate in any high-security computer networks; however, they can only propagate themselves in a very limited way.

A computer network is a set of hosts V

(|V|=N) and direct links E (|E|=M). Each direct link connects two different hosts, allowing them to communicate in both directions. If a host u is infected with a hopper, then it will propagate itself to all hosts W where W is the set of hosts which are connected to any neighbor of u. Formally, W={w∣{(u,v),(v,w)}⊆E}. The newly-infected host w

can now proceed to infect other hosts in the same manner.

You are trying to infect all hosts in a particular high-security network by compromising a single host. Of course, a compromised host will be infected with a hopper. Though it may be an impossible mission, you realize that you can trick the administrator into installing new direct links between hosts by submitting IT requests.

Too many requests may arouse suspicion. Find the minimum number of IT requests you have to submit to ensure that there exists a single host such that if it is infected with a hopper, then it will propagate to the entire network.

Input

The first line contains two integers N

and M, where 3≤N≤5⋅105 and 2≤M≤5⋅105. The next M lines contain two integers u and v (1≤u,v≤N), representing a direct link between hosts u and v

. There is at most one direct link between any pair of hosts, and no host is directly linked to itself.

Output

The output contains an integer in a line representing the minimum number of IT requests you must send.

Explanation

In the first example, there are N=3

hosts and M=2 direct links {(1,2),(2,3)}. In this network, there is no way to spread the hopper to all hosts by compromising only a single host, e.g., if we compromise host 1, then only hosts 1 and 3 are infected, etc. We need to submit one IT request to connect (1,3) to achieve our goal. By doing so, if we compromise host 1, all hosts 1, 2, and 3 are infected. 1→3→2 (thus, host 2 is infected). 1→2→3 (thus, host 3

is infected).

Sample Input 1Sample Output 1
3 2
1 2
2 3
1
Sample Input 2Sample Output 2
5 10
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
0
Sample Input 3Sample Output 3
12 13
2 3
3 4
4 5
5 2
6 7
7 8
8 9
9 10
10 7
9 12
12 11
11 6
12 7
3

 

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
const int  maxn=5e5+10;
using namespace std;
typedef long long ll;
vector<int>ve[maxn];
int pos[maxn];
bool ok;
void dfs(int id, int x)
{
    pos[x]=id;
    int len=ve[x].size();
    for(int i=0; i<len; i++)
    {
        if(pos[ve[x][i]])
        {
            if(abs(pos[ve[x][i]]-pos[x])%2==0)
            {
                ok=true;
            }
            continue;
        }
        dfs(id+1,ve[x][i]);
    }
}
int main()
{
    int n,m;
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=0; i<m; i++)
    {
        int u,v;
        cin>>u>>v;
        ve[u].push_back(v);
        ve[v].push_back(u);
    }
    ll ans=0;
    ok=false;
    for(int i=1; i<=n; i++)
    {
        if(pos[i]==0)
        {
            ans++;
            dfs(1,i);
        }
    }
    if(!ok) cout<<ans<<endl;
    else cout<<ans-1<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值