

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
頂点 頂点 頂点 の 個の頂点からなる単純無向グラフ が与えられます。 には 本の辺があり、 本目 の辺は頂点 と頂点 を結んでいます。 には 本の辺があり、 本目 の辺は頂点 と頂点 を結んでいます。
あなたは、グラフ に対して次の操作を 回以上の好きな回数繰り返すことができます。
- を満たす整数の組 をひとつ選ぶ。 円を支払って、頂点 と頂点 を結ぶ辺がなければ追加し、あれば取り除く。
と を同型にするために少なくとも何円支払う必要があるか求めてください。
単純無向グラフとは?
単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。
グラフの同型とは?
頂点のグラフ と が同型であるとは、ある の並べ替え が存在して、どの に対しても
- に頂点 頂点 を結ぶ辺が存在するとき、かつそのときに限り に頂点 頂点 を結ぶ辺が存在する
制約
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
出力
答えを出力せよ。
入力例 1Copy
5 4 1 2 2 3 3 4 4 5 4 1 2 1 3 1 4 1 5 3 1 4 1 5 9 2 6 5 3
出力例 1Copy
9
与えられたグラフはそれぞれ以下のようになります。
たとえば、 に対して次のような つの操作を順に行うことで、 円を支払って と を同型にすることができます。
- として操作を行う。 には頂点 と頂点 を結ぶ辺があるので、 円を支払ってこれを取り除く。
- として操作を行う。 には頂点 と頂点 を結ぶ辺はないので、 円を支払ってこれを追加する。
- として操作を行う。 には頂点 と頂点 を結ぶ辺があるので、 円を支払ってこれを取り除く。
- として操作を行う。 には頂点 と頂点 を結ぶ辺はないので、 円を支払ってこれを追加する。
操作の結果、 は以下のようになります。
支払う金額を 円以下にして と を同型にすることはできないため、9
を出力してください。
入力例 2Copy
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 9 1 1 1 1 1 1 1 1 9
出力例 2Copy
3
たとえば、 とした 回の操作を行うことで と を同型にすることができます。
入力例 3Copy
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 5 4 4 4 4 4 4 4 4 5
出力例 3Copy
5
たとえば、 とした 回の操作を行うことで と を同型にすることができます。
入力例 4Copy
2 0 0 371
出力例 4Copy
0
や には辺が含まれていないこともあります。 また、一度も操作をする必要がないこともあります。
入力例 5Copy
8 13 1 8 5 7 4 6 1 5 7 8 1 6 1 2 5 8 2 6 5 6 6 7 3 7 4 8 15 3 5 1 7 4 6 3 8 7 8 1 2 5 6 1 6 1 5 1 4 2 8 2 6 2 4 4 7 1 3 7483 1694 5868 3296 9723 5299 4326 5195 4088 5871 1384 2491 6562 1149 6326 2996 9845 7557 4041 7720 1554 5060 8329 8541 3530 4652 3874 3748
出力例 5Copy
21214
Score : points
Problem Statement
You are given simple undirected graphs and , each with vertices: vertices , , , . Graph has edges, and its -th edge connects vertices and . Graph has edges, and its -th edge connects vertices and .
You can perform the following operation on graph any number of times, possibly zero.
- Choose a pair of integers satisfying . Pay yen, and if there is no edge between vertices and in , add one; if there is, remove it.
Find the minimum total cost required to make and isomorphic.
What is a simple undirected graph?
A simple undirected graph is a graph without self-loops or multi-edges, where edges have no direction.
What does it mean for graphs to be isomorphic?
Two graphs and with vertices are isomorphic if and only if there exists a permutation of such that for all :
- an edge exists between vertices and in if and only if an edge exists between vertices and in .
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1Copy
5 4 1 2 2 3 3 4 4 5 4 1 2 1 3 1 4 1 5 3 1 4 1 5 9 2 6 5 3
Sample Output 1Copy
9
The given graphs are as follows:
For example, you can perform the following four operations on to make it isomorphic to at a cost of yen.
- Choose . There is an edge between vertices and in , so pay yen to remove it.
- Choose . There is no edge between vertices and in , so pay yen to add it.
- Choose . There is an edge between vertices and in , so pay yen to remove it.
- Choose . There is no edge between vertices and in , so pay yen to add it.
After these operations, becomes:
You cannot make and isomorphic at a cost less than yen, so print 9
.
Sample Input 2Copy
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 9 1 1 1 1 1 1 1 1 9
Sample Output 2Copy
3
For example, performing the operations on will make it isomorphic to .
Sample Input 3Copy
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 5 4 4 4 4 4 4 4 4 5
Sample Output 3Copy
5
For example, performing the operation once will make and isomorphic.
Sample Input 4Copy
2 0 0 371
Sample Output 4Copy
0
Note that and may have no edges.
Also, it is possible that no operations are needed.
Sample Input 5Copy
8 13 1 8 5 7 4 6 1 5 7 8 1 6 1 2 5 8 2 6 5 6 6 7 3 7 4 8 15 3 5 1 7 4 6 3 8 7 8 1 2 5 6 1 6 1 5 1 4 2 8 2 6 2 4 4 7 1 3 7483 1694 5868 3296 9723 5299 4326 5195 4088 5871 1384 2491 6562 1149 6326 2996 9845 7557 4041 7720 1554 5060 8329 8541 3530 4652 3874 3748
Sample Output 5Copy
21214