Codeforces 95E-Lucky Country

来源:互联网 发布:手机短信软件哪个好 编辑:程序博客网 时间:2024/06/08 03:34

Lucky Country
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 477444 are lucky and 517467 are not.

One night Petya was sleeping. He was dreaming of being the president of some island country. The country is represented by islands connected by two-way roads. Between some islands there is no road way, even through other islands, that's why the country is divided into several regions. More formally, each island belongs to exactly one region, there is a path between any two islands located in the same region; there is no path between any two islands from different regions. A region is lucky if the amount of islands in it is a lucky number.

As a real president, Petya first decided to build a presidential palace. Being a lucky numbers' fan, Petya wants to position his palace in one of the lucky regions. However, it is possible that initially the country has no such regions. In this case Petya can build additional roads between different regions, thus joining them. Find the minimum number of roads needed to build to create a lucky region.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 105). They are the number of islands and the number of roads correspondingly. Next m lines contain road descriptions. Each road is defined by the numbers of islands that it connects: that is, by two integers u and v(1 ≤ u, v ≤ n). Some roads can connect an island with itself; there can be more than one road between a pair of islands. Numbers in each line are separated by exactly one space character.

Output

If there's no solution, output the only number "-1" (without the quotes). Otherwise, output the minimum number of roads r that need to be built to get a lucky region.

Examples
input
4 31 22 31 3
output
1
input
5 41 23 44 53 5
output
-1
题意:如果一个数中不包含除4和7之外的数字则是幸运数。有n个岛屿,通过双向道路连接。这些岛屿被分为几个地区。每个岛属于恰好一个区域,同一区域中的任何两个岛之间存在道路,不同区域的任何两个岛之间没有路径。如果一个地区的岛屿数量是一个幸运数字,则这个地区是幸运的。问最少增加几条道路能创建一个幸运地区。

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <stack>#include <queue>#include <vector>using namespace std;const int INF=0x3f3f3f3f;int f[100005];int n,m,sum;int visit[100005],dp[100005],v;int Find(int x){    int s=x;    while(f[x]>=0)        x=f[x];    while(s!=x)    {        int tmp=f[s];        f[s]=x;        s=tmp;    }    return x;}void Union(int a,int b){    int aa=Find(a),bb=Find(b);    if(aa!=bb)    {        f[aa]=f[aa]+f[bb];        f[bb]=aa;    }}void ZeroOnePack(int cost,int val){    for(int i=v; i>=cost; i--)        dp[i]=min(dp[i],dp[i-cost]+val);}void CompletePack(int cost,int val){    for(int i=cost; i<=v; i++)        dp[i]=min(dp[i],dp[i-cost]+val);}void MultiplePack(int cost,int val,int amount){    if(cost*amount>=v) CompletePack(cost,val);    else    {        int k=1;        while(k<amount)        {            ZeroOnePack(k*cost,k*val);            amount-=k;            k*=2;        }        ZeroOnePack(amount*cost,amount*val);    }}bool check(int x){    for(; x; x/=10)if(x%10!=4&&x%10!=7) return 0;    return 1;}int main(){    while(~scanf("%d %d",&n,&m))    {        memset(f,-1,sizeof f);        memset(visit,0,sizeof visit);        int a,b;        for(int i=0; i<m; i++)        {            scanf("%d %d",&a,&b);            Union(a,b);        }        int mi=INF,ma=-1;        for(int i=1; i<=n; i++)        {            if(f[i]<0)            {                if(-f[i]>ma) ma=-f[i];                if(-f[i]<mi) mi=-f[i];                visit[-f[i]]++;            }        }        sum=INF;        memset(dp,INF,sizeof dp);        dp[0]=0;        v=n;        for(int i=mi; i<=ma; i++)            if(visit[i])                MultiplePack(i,1,visit[i]);        for(int i=1; i<=n; i++)            if(check(i))                sum=min(sum,dp[i]);        if(sum==INF) printf("-1\n");        else printf("%d\n",sum-1);    }    return 0;}

0 0
原创粉丝点击