【题目】
2179: Lucky Country
Time Limit: 2 Sec Memory Limit: 256 MB
Submit: 10 Solved: 8
[Submit][Status][Web Board]
Description
E. 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 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,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 3
1 2
2 3
1 3
Output
1
Input
5 4
1 2
3 4
4 5
3 5
Output
-1
【题解】
这题补到哭泣。
题意:幸运数即只包含4和7的数。第一行输入两个整数n,m,分别表示岛屿数量与桥的数量,接下来m行每行输入两个整数u,v,表示相连接的两个岛屿,所有可连通的岛屿为一个区域。求建造最少的桥使连接成的区域的岛屿总数为幸运数,若不可行则输出-1。
思路:用并查集把每个区域的岛屿数表示出来,再用背包选择可行最优解。
【代码】
#include<bits/stdc++.h>
using namespace std;
const int inf=200010;
int pre[100010];
int num[100010];
int n,m;
int f(int n) //判断是否为幸运数
{
while(n>0)
{
int a=n%10;
if(a!=4&&a!=7)
return 0;
n/=10;
}
return 1;
}
int find(int x) //寻找根结点
{
if(x==pre[x])
return x;
return pre[x]=find(pre[x]);
}
void join(int x,int y) //连接岛屿
{
x=find(x),y=find(y);
if(x!=y)
pre[x]=y;
}
int main()
{
scanf("%d%d",&n,&m);
int i,j;
int yes[100010];
for(i=1;i<100010;i++)
yes[i]=inf;
for(i=1;i<=n;i++)
{
pre[i]=i;
num[i]=0;
}
for(i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
join(a,b);
}
for(i=1;i<=n;i++)
num[find(i)]++; //记录根结点的结点数量
int k=0;
for(i=1;i<=n;i++)
if(find(i)==i)
num[k++]=num[i];
yes[0]=-1;
int ans=-1;
for(i=0;i<k;i++) //背包
{
for(j=100010;j>=0;j--)
if(yes[j]!=inf)
{
if(j+num[i]<100010)
yes[j+num[i]]=min(yes[j+num[i]],yes[j]+1);
if(f(j+num[i]))
if(ans==-1||ans>yes[j+num[i]])
ans=yes[j+num[i]];
}
yes[num[i]]=0;
if(f(num[i]))
ans=0;
}
printf("%d\n",ans);
return 0;
}