CF387D.George and Interesting Graph

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

George loves graphs. Most of all, he loves interesting graphs. We will assume that a directed graph is interesting, if it meets the following criteria:

  • The graph doesn't contain any multiple arcs;
  • There is vertex v (we'll call her the center), such that for any vertex of graph u, the graph contains arcs (u, v) and (v, u). Please note that the graph also contains loop (v, v).
  • The outdegree of all vertexes except for the center equals two and the indegree of all vertexes except for the center equals two. The outdegree of vertex u is the number of arcs that go out of u, the indegree of vertex u is the number of arcs that go in u. Please note that the graph can contain loops.

However, not everything's that simple. George got a directed graph of n vertices and m arcs as a present. The graph didn't have any multiple arcs. As George loves interesting graphs, he wants to slightly alter the presented graph and transform it into an interesting one. In one alteration he can either remove an arbitrary existing arc from the graph or add an arbitrary arc to the graph.

George wonders: what is the minimum number of changes that he needs to obtain an interesting graph from the graph he's got as a present? Help George and find the answer to the question.

乔治热爱图论。他尤其钟爱“有趣的图”。我们称一个有向图为有趣的,当且仅当它满足以下条件:

  • 该图不包含任何重边(即任意两个顶点之间至多存在一条方向相同的有向边);
  • 存在某个顶点 vv(我们称其为中心),使得对图中任意顶点 uu,图中均包含有向边 (u, v)(u,\,v) 和 (v, u)(v,\,u)。请注意:图中也必然包含自环 (v, v)(v,\,v);
  • 除中心外,其余所有顶点的出度均为 22,入度也均为 22。顶点 uu 的出度指从 uu 出发的有向边数量,入度指指向 uu 的有向边数量。请注意:图中允许存在自环。

然而,事情并非如此简单。乔治收到一份礼物:一个含 nn 个顶点、mm 条有向边的有向图,且该图不含重边。由于乔治钟爱“有趣的图”,他希望对该图进行最少的修改,使其变为一个有趣的图。每次修改操作可以是以下二者之一:

  • 删除图中任意一条已存在的有向边;
  • 添加图中任意一条尚未存在的有向边(即不产生重边)。

乔治想知道:将手中这张图变为有趣的图,所需的最少修改次数是多少?请帮助乔治解答这个问题。

输入格式

The first line contains two space-separated integers n and m (2 ≤ n ≤ 500, 1 ≤ m ≤ 1000) — the number of vertices and arcs in the presented graph.

Each of the next m lines contains two space-separated integers a__i, b__i (1 ≤ a__i, b__i ≤ n) — the descriptions of the graph's arcs. Pair (a__i, b__i) means that the graph contains an arc from vertex number a__i to vertex number b__i. It is guaranteed that the presented graph doesn't contain multiple arcs.

Assume that the grah vertices are numbered 1 through n.

第一行包含两个以空格分隔的整数 nn 和 mm(2 ≤ n ≤ 5002 \leq n \leq 500,1 ≤ m ≤ 10001 \leq m \leq 1000)—— 分别表示所给图的顶点数和有向边(弧)数。

接下来的 mm 行中,每行包含两个以空格分隔的整数 aia_i、bib_i(1 ≤ ai, bi ≤ n1 \leq a_i, b_i \leq n)—— 描述图中的有向边(弧)。二元组 (ai, bi)(a_i, b_i) 表示图中存在一条从顶点 aia_i 指向顶点 bib_i 的有向边(弧)。保证所给图中不存在重边(即任意两个顶点间至多只有一条方向相同的有向边)。

假设图的顶点编号为 11 到 nn。

输出格式

Print a single integer — the answer to George's question.

输出一个整数——即乔治问题的答案。

输入输出样例

  • 输入#1

    3 7
    1 1
    2 2
    3 1
    1 3
    3 2
    2 3
    3 3

    输出#1

    0
  • 输入#2

    3 6
    1 1
    2 2
    3 1
    3 2
    2 3
    3 3

    输出#2

    1
  • 输入#3

    3 1
    2 2

    输出#3

    6

说明/提示

For more information about directed graphs, please visit: http://en.wikipedia.org/wiki/Directed_graph

In the first sample the graph already is interesting, its center is vertex 3.

有关有向图的更多信息,请访问:http://en.wikipedia.org/wiki/Directed_graph

在第一个样例中,该图本身已是有趣图,其中心为顶点 3。

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

首页