Lucky Country

49 篇文章 4 订阅

2179: Lucky Country

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 1  Solved: 0
[Submit][Status][Web Board]

Description

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 47, 744, 4 are lucky and 5, 17, 467 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,vn). 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 3
1 2
2 3
1 3
Output
1
Input
5 4
1 2
3 4
4 5
3 5
Output
-1

【分析】

题意:只包含4和7的数字是lucky number,现在给出n个岛..和m条双向边,连在一起的岛称为一块区域,问最少需要添加几条边可以让某一块区域内的岛屿数量变成一个luncky number

其实没啥难的....一个并查集加上一个01背包...然而01背包可能会t因为最多有10w个物品,所以改成多重背包做就行了

并查集记录一下所有区域内岛的数量,然后做一次上限为n的背包,然后在所有背包里找一个最小的min(dp[i])满足i是lucky number即可

【代码】

#include <bits/stdc++.h>
using namespace std;
#define MXK 100005
#define INF 0x3f3f3f3f
int sz[MXK],P[MXK],dp[MXK];

map<int,int>name;
map<int,int>::iterator it;

int findo(int v){
    if(v==P[v])return v;
    return P[v]=findo(P[v]);
}
void make(int lo,int high,int kto){
    for(int i=high;i>=lo;i--)
        dp[i]=min(dp[i],dp[i-lo]+kto);
}

int check(int x){
    for(; x; x/=10)if(x%10!=4&&x%10!=7) return 0;
    return 1;
}

void rec(int l,int k,int n)
{
    for(int i=1;i<=k;i=i*2){
        make(l*i,n,i);
        k-=i;
    }
    if(k>0)rec(l,k,n);
}
int main()
{
    int i,j,k,l,m,n,v,w;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        P[i]=i;
        sz[i]=1;
        dp[i]=INF;
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&l,&k);
        v=findo(l);
        w=findo(k);
        if(v==w)continue;
        P[v]=w;
        sz[w]+=sz[v];
    }
    for(int i=1;i<=n;i++)
        if (P[i] == i)
            name[sz[i]]++;

    for(it=name.begin();it!=name.end();it++){
        l=it->first;
        k=it->second;
        rec(l,k,n);
    }
    int ans=INF;
    for(int i=1;i<=n;i++)
        if(check(i))
            ans=min(ans,dp[i]);
    if(ans==INF) ans=-1;
    cout<<max(ma-1,-1)<<endl;

    return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值