Problem:


Score : 700 points

Problem Statement

In the State of Takahashi in AtCoderian Federation, there are N cities, numbered 1, 2, ..., N.
M bidirectional roads connect these cities.
The i-th road connects City A_i and City B_i.
Every road connects two distinct cities.
Also, for any two cities, there is at most one road that directly connects them.

One day, it was decided that the State of Takahashi would be divided into two states, Taka and Hashi.
After the division, each city in Takahashi would belong to either Taka or Hashi.
It is acceptable for all the cities to belong Taka, or for all the cities to belong Hashi.
Here, the following condition should be satisfied:

Any two cities in the same state, Taka or Hashi, are directly connected by a road.

Find the minimum possible number of roads whose endpoint cities belong to the same state.
If it is impossible to divide the cities into Taka and Hashi so that the condition is satisfied, print -1.

Constraints

2 \leq N \leq 700

0 \leq M \leq N(N-1)/2

1 \leq A_i \leq N

1 \leq B_i \leq N

A_i \neq B_i

If i \neq j, at least one of the following holds: A_i \neq A_j and B_i \neq B_j.

If i \neq j, at least one of the following holds: A_i \neq B_j and B_i \neq A_j.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the answer.

Sample Input 1

5 5
1 2
1 3
3 4
3 5
4 5

Sample Output 1

4

For example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong to Hashi, the condition is satisfied.
Here, the number of roads whose endpoint cities belong to the same state, is 4.

Sample Input 2

5 1
1 2

Sample Output 2

-1

In this sample, the condition cannot be satisfied regardless of which cities belong to each state.

Sample Input 3

4 3
1 2
1 3
2 3

Sample Output 3

3

Sample Input 4

10 39
7 2
7 1
5 6
5 8
9 10
2 8
8 7
3 10
10 1
8 10
2 3
7 4
3 9
4 10
3 4
6 1
6 7
9 5
9 7
6 9
9 4
4 6
7 5
8 3
2 5
9 2
10 7
8 6
8 9
7 3
5 3
4 5
6 3
2 10
5 10
4 2
6 2
8 4
10 6

Sample Output 4

21



Sample input:
5 5
1 2
1 3
3 4
3 5
4 5


Sample output:
4

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import defaultdict


def ii():
    return int(input())


def lii():
    return list(map(int, input().split(' ')))


def lvi(N):
    l = []
    for _ in range(N):
        l.append(ii())
    return l


def lv(N):
    l = []
    for _ in range(N):
        l.append(input())
    return l


class UnionFind(object):

    def __init__(self, n):
        # 親のnodeを表す
        self.par = [i for i in range(n)]

    def root(self, x):
        if self.par[x] == x:
            return x
        else:
            # 経路圧縮
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return

        self.par[x] = y



class UnionFindRankCount(UnionFind):

    def __init__(self, n):
        UnionFind.__init__(self, n)

        self.rank = [0 for _ in range(n)]
        self.count = [1 for _ in range(n)]

    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return

        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.count[y] += self.count[x]
        else:
            self.par[y] = x
            self.count[x] += self.count[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1


def graph_coloring(G):
    N = len(G)
    color = np.array([0 for _ in range(N)])

    def dfs(v, c):
        """
        :param v: node
        :param c: color (1 or -1)
        :return:
        """
        color[v] = c
        for j in range(N):
            if G[v, j] == 1:
                if color[j] == c:
                    # 隣が同じ色
                    return False
                elif color[j] == 0:
                    # まだ塗ってなければ塗る
                    if not dfs(j, c * -1):
                        return False

        # 全ての頂点を塗れた
        return True

    # # グラフが連結でない
    # for i in range(N):
    #     if color[i] == 0:
    #         if not dfs(i, 1):
    #             return None  # impossible

    # 連結の前提
    if not dfs(0, 1):
        return None

    return color


def c2(n):
    return n * (n-1) // 2


def partial_sum(integer_list, target_sum, i=0, sum_=0):
    if target_sum < sum_:
        return False

    if i == len(integer_list):
        return sum_ == target_sum

    if partial_sum(integer_list, target_sum, i + 1, sum_):
        return True

    if partial_sum(integer_list, target_sum, i + 1, sum_ + integer_list[i]):
        return True

    return False


def partial_sum_list(integer_list):
    B = sum(integer_list)
    N = len(integer_list)
    dp = np.zeros([N, B+1])
    dp[0][integer_list[0]] = 1

    for v in range(1, N):
        for b in range(B+1):
            w = integer_list[v]
            if dp[v-1, b]:
                dp[v, b] += dp[v-1, b]
                dp[v, b+w] += dp[v-1, b]

    return dp[-1]


def main():
    N, M = lii()
    G = np.eye(N)

    for _ in range(M):
        x, y = lii()
        G[x-1, y-1] = 1
        G[y-1, x-1] = 1

    G = 1 - G  # 補グラフ

    u = UnionFindRankCount(N)
    for i in range(N):
        for j in range(i, N):
            if G[i, j]:
                u.union(i, j)

    d = defaultdict(list)
    for n in range(N):
        d[u.par[n]].append(n)

    base = 0
    int_list = []
    # print(G, d)
    for s in d.values():
        Gs = np.array(G[s][:, s])
        color = graph_coloring(Gs)

        if color is None:
            return -1

        p = len(color[color == 1])
        q = len(color) - p

        base += min(p, q)
        diff = max(p, q) - min(p, q)
        if diff > 0:
            int_list.append(diff)

    if int_list:
        res = partial_sum_list(int_list)

        for s in range(sum(res) // 2, sum(res)):
            if res[s]:
                return c2(base + s) + c2(N - base - s)

    else:
        return c2(base) * 2


if __name__ == '__main__':
    print(main())

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import defaultdict


def ii():
    return int(input())


def lii():
    return list(map(int, input().split(' ')))


def lvi(N):
    l = []
    for _ in range(N):
        l.append(ii())
    return l


def lv(N):
    l = []
    for _ in range(N):
        l.append(input())
    return l


class UnionFind(object):

    def __init__(self, n):
        # 親のnodeを表す
        self.par = [i for i in range(n)]

    def root(self, x):
        if self.par[x] == x:
            return x
        else:
            # 経路圧縮
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return

        self.par[x] = y



class UnionFindRankCount(UnionFind):

    def __init__(self, n):
        UnionFind.__init__(self, n)

        self.rank = [0 for _ in range(n)]
        self.count = [1 for _ in range(n)]

    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return

        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.count[y] += self.count[x]
        else:
            self.par[y] = x
            self.count[x] += self.count[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1


def graph_coloring(G):
    N = len(G)
    color = np.array([0 for _ in range(N)])

    def dfs(v, c):
        """
        :param v: node
        :param c: color (1 or -1)
        :return:
        """
        color[v] = c
        for j in range(N):
            if G[v, j] == 1:
                if color[j] == c:
                    # 隣が同じ色
                    return False
                elif color[j] == 0:
                    # まだ塗ってなければ塗る
                    if not dfs(j, c * -1):
                        return False

        # 全ての頂点を塗れた
        return True

    # # グラフが連結でない
    # for i in range(N):
    #     if color[i] == 0:
    #         if not dfs(i, 1):
    #             return None  # impossible

    # 連結の前提
    if not dfs(0, 1):
        return None

    return color


def c2(n):
    return n * (n-1) // 2


def partial_sum(integer_list, target_sum, i=0, sum_=0):
    if target_sum < sum_:
        return False

    if i == len(integer_list):
        return sum_ == target_sum

    if partial_sum(integer_list, target_sum, i + 1, sum_):
        return True

    if partial_sum(integer_list, target_sum, i + 1, sum_ + integer_list[i]):
        return True

    return False


def partial_sum_list(integer_list):
    B = sum(integer_list)
    N = len(integer_list)
    dp = np.zeros([N, B+1])
    dp[0][integer_list[0]] = 1

    for v in range(1, N):
        for b in range(B+1):
            w = integer_list[v]
            if dp[v-1, b]:
                dp[v, b] += dp[v-1, b]
                dp[v, b+w] += dp[v-1, b]

    return dp[-1]


def main():
    N, M = lii()
    G = np.eye(N)

    for _ in range(M):
        x, y = lii()
        G[x-1, y-1] = 1
        G[y-1, x-1] = 1

    G = 1 - G  # 補グラフ

    u = UnionFindRankCount(N)
    for i in range(N):
        for j in range(i, N):
            if G[i, j]:
                u.union(i, j)

    d = defaultdict(list)
    for n in range(N):
        d[u.par[n]].append(n)

    base = 0
    int_list = []
    # print(G, d)
    for s in d.values():
        Gs = np.array(G[s][:, s])
        color = graph_coloring(Gs)

        if color is None:
            return -1

        p = len(color[color == 1])
        q = len(color) - p

        base += min(p, q)
        diff = max(p, q) - min(p, q)
        if diff > 0:
            int_list.append(diff)

    if int_list:
        int_list = sorted(int_list, reverse=True)
        res = partial_sum_list(int_list)

        for s in range((N - base) // 2, N - base):
            if res[s]:
                return c2(base + s) + c2(N - base - s)

    else:
        return c2(base) * 2


if __name__ == '__main__':
    print(main())

Submission status: Runtime Error

Solution code:
import numpy as np

def ii():
    return int(input())


def lii():
    return list(map(int, input().split(' ')))


def lvi(N):
    l = []
    for _ in range(N):
        l.append(ii())
    return l


def lv(N):
    l = []
    for _ in range(N):
        l.append(input())
    return l


def graph_coloring(G, color):
    N = len(G)

    def dfs(v, c):
        """
        :param v: node
        :param c: color (1 or -1)
        :return:
        """
        color[v] = c
        for j in range(N):
            if G[v, j] == 1:
                if color[j] == c:
                    # 隣が同じ色
                    return False
                elif color[j] == 0:
                    # まだ塗ってなければ塗る
                    if not dfs(j, c * -1):
                        return False

        # 全ての頂点を塗れた
        return True

    # グラフが連結でない
    c = 1
    for i in range(N):
        if color[i] == 0:
            if not dfs(i, c):
                return G, None  # impossible
            c += 1

    return G, color


def c2(n):
    return n * (n-1) // 2


def partial_sum(integer_list, target_sum, i=0, sum_=0):
    if target_sum < sum_:
        return False

    if i == len(integer_list):
        return sum_ == target_sum

    if partial_sum(integer_list, target_sum, i + 1, sum_):
        return True

    if partial_sum(integer_list, target_sum, i + 1, sum_ + integer_list[i]):
        return True

    return False


def partial_sum_list(integer_list):
    B = sum(integer_list)
    N = len(integer_list)
    dp = np.zeros([N, B+1])
    dp[0][integer_list[0]] = 1

    for v in range(1, N):
        for b in range(B+1):
            w = integer_list[v]
            if dp[v-1, b]:
                dp[v, b] += dp[v-1, b]
                dp[v, b+w] += dp[v-1, b]

    return dp[-1]


def main():
    N, M = lii()
    G = np.eye(N)
    for _ in range(M):
        x, y = lii()
        G[x-1, y-1] = 1
        G[y-1, x-1] = 1

    G = 1 - G  # 補グラフ
    color = np.array([0 for _ in range(N)])

    G, color = graph_coloring(G, color=color)
    if color is None:
        return -1

    c = 1
    base = 0
    int_list = []
    while True:
        p = len(color[color == c])
        q = len(color[color == -c])
        if p == 0 and q == 0:
            break

        base += min(p, q)
        diff = max(p, q) - min(p, q)
        if diff > 0:
            int_list.append(diff)

        c += 1

    if int_list:
        int_list = sorted(int_list, reverse=True)
        res = partial_sum_list(int_list)

        for s in range((N - base) // 2, N - base):
            if res[s]:
                return c2(base + s) + c2(N - base - s)

    else:
        return c2(base) * 2


if __name__ == '__main__':
    print(main())

Submission status: Runtime Error

Solution code:
k=int(input())
if k<10:
    print("\n".join(list(map(str,range(1,k+1)))))
else:
    cnt = 0
    for i in range(k):
        if i%9==0 and i>0:
            cnt+=1
        num = int(str(i%9+1)+"9"*cnt)
        print(num)