CF95E.Lucky Country

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

佩佳喜欢幸运数字。众所周知,如果一个正整数的十进制表示中仅包含数字 4 和 7,则该数为幸运数。例如,47、744、4 是幸运数,而 5、17、467 不是幸运数。

一天夜里,佩佳正在睡觉。他梦见自己成为了一个岛国的总统。该国由若干岛屿以及连接这些岛屿的双向道路构成。某些岛屿之间(即使经由其他岛屿中转)也不存在通路,因此该国被划分为若干个区域。更准确地说:每个岛屿恰好属于一个区域;同一区域内的任意两个岛屿之间均存在路径;不同区域中的任意两个岛屿之间均不存在路径。若一个区域所含岛屿的数量是一个幸运数,则称该区域为幸运区域。

作为一名真正的总统,佩佳首先决定建造一座总统官邸。由于佩佳钟爱幸运数字,他希望将官邸建在某个幸运区域内。然而,初始状态下该国可能并不存在幸运区域。此时,佩佳可以通过在不同区域之间新建道路,将它们合并。请找出为创建至少一个幸运区域所需新建的最少道路数量。

输入格式

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.

第一行包含两个整数 nn 和 mm(1≤n,m≤1051 \leq n, m \leq 10^5),分别表示岛屿的数量和道路的数量。接下来的 mm 行描述了各条道路。每条道路由其所连接的两个岛屿的编号定义,即由两个整数 uu 和 vv(1≤u,v≤n1 \leq u, v \leq n)给出。某些道路可能连接同一座岛屿(即自环);一对岛屿之间可能存在多条道路。每行中的数字之间恰好用一个空格分隔。

输出格式

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.

如果无解,输出唯一的数字 “-1”(不带引号)。否则,输出为获得幸运区域所需修建的最少道路数 $ r $。

输入输出样例

  • 输入#1

    4 3
    1 2
    2 3
    1 3

    输出#1

    1
  • 输入#2

    5 4
    1 2
    3 4
    4 5
    3 5

    输出#2

    -1

输入解题思路,AI测评打分。不知道怎么写?

首页