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: Wrong Answer

Solution code:

import math
import sys

def nCr(n,r):
    return math.factorial(n) // (math.factorial(n-r) * math.factorial(r))

def wantp(n):

    wpl = []
    while n != p[n]:
        wpl.append(n)
        n = p[n]

    for i in wpl:
        p[i] = n
    return n


N,M = map(int,input().split())

link = [[False] * N for i in range(N)]

for i in range(M):

    A,B = map(int,input().split())
    A -= 1
    B -= 1

    link[A][B] = True
    link[B][A] = True


#まず普通にUnion-Findする
p = [i for i in range(N)]
rank = [1] * N
for i in range(N):

    for j in range(N):

        if not link[i][j]:
            ip = wantp(i)
            jp = wantp(j)

            if ip != jp:

                if rank[ip] > rank[jp]:
                    p[jp] = ip
                elif rank[ip] < rank[jp]:
                    p[ip] = jp
                else:
                    p[ip] = jp
                    rank[jp] += 1
                    

dic = {} #親がキーで配列内に属す頂点を入れる(これで分類)

for i in range(N):
    np = wantp(i)
    if np not in dic:
        dic[np] = []
    dic[np].append(i)


side = [-1] * N #0と1サイドで分ける

for i in dic: #整合性の確認

    side[i] = 0
    for j in dic[i]:

        for k in dic[i]:
            if j != k and (not link[j][k]) and side[k] != -1:

                if side[j] == -1:
                    side[j] = side[k] ^ 1
                elif side[j] == side[k]:
                    print (-1)
                    sys.exit()

for i in dic: #整合性の確認2

    side[i] = 0
    for j in dic[i]:

        for k in dic[i]:
            if j != k and (not link[j][k]) and side[k] != -1:

                if side[j] == -1 or side[j] == side[k]:
                    print (-1)
                    sys.exit()



dic2 = {} #キーは先頭[0の数-1の数を入れる]

for i in range(N):
    np = wantp(i)
    if np not in dic2:
        dic2[np] = 0

    if side[i] == 0:
        dic2[np] += 1
    else:
        dic2[np] -= 1

allnum = [False] * 1401
allnum[0] = [True]

for i in dic2:
    dic2[i] = abs(dic2[i])

#dpで中心に一番近い数を求める

for i in dic2:
    nn = dic2[i]
    nall = [False] * 1401
    
    for j in range(1401):
        j -= 701
        if allnum[j]:
            nall[j + nn] = True
            nall[j - nn] = True

    allnum = nall.copy()

ans = float("inf")
for i in range(N):
    if nall[i] or nall[-1 * i]:
        a = (N-i) // 2
        b = (N+i) // 2

        nans = 0
        if a >= 2:
            nans += nCr(a,2)
        if b >= 2:
            nans += nCr(b,2)
        
        ans = min(nans,ans)

if ans == float("inf"):
    print (-1)
else:
    print (ans)

Submission status: Wrong Answer

Solution code:

import math
import sys

def nCr(n,r):
    return math.factorial(n) // (math.factorial(n-r) * math.factorial(r))

def wantp(n):

    wpl = []
    while n != p[n]:
        wpl.append(n)
        n = p[n]

    for i in wpl:
        p[i] = n
    return n


N,M = map(int,input().split())

link = [[False] * N for i in range(N)]

for i in range(M):

    A,B = map(int,input().split())
    A -= 1
    B -= 1

    link[A][B] = True
    link[B][A] = True


#まず普通にUnion-Findする
p = [i for i in range(N)]
rank = [1] * N
for i in range(N):

    for j in range(N):

        if not link[i][j]:
            ip = wantp(i)
            jp = wantp(j)

            if ip != jp:

                if rank[ip] > rank[jp]:
                    p[jp] = ip
                elif rank[ip] < rank[jp]:
                    p[ip] = jp
                else:
                    p[ip] = jp
                    rank[jp] += 1
                    

dic = {} #親がキーで配列内に属す頂点を入れる(これで分類)

for i in range(N):
    np = wantp(i)
    if np not in dic:
        dic[np] = []
    dic[np].append(i)


side = [-1] * N #0と1サイドで分ける

for i in dic: #整合性の確認

    side[i] = 0
    for j in dic[i]:

        for k in dic[i]:
            if j != k and (not link[j][k]) and side[k] != -1:

                if side[j] == -1:
                    side[j] = side[k] ^ 1
                elif side[j] == side[k]:
                    print (-1)
                    sys.exit()


dic2 = {} #キーは先頭[0の数-1の数を入れる]

for i in range(N):
    np = wantp(i)
    if np not in dic2:
        dic2[np] = 0

    if side[i] == 0:
        dic2[np] += 1
    else:
        dic2[np] -= 1

allnum = [False] * 1401
allnum[0] = [True]

for i in dic2:
    dic2[i] = abs(dic2[i])

#dpで中心に一番近い数を求める

for i in dic2:
    nn = dic2[i]
    nall = [False] * 1401
    
    for j in range(1401):
        j -= 701
        if allnum[j]:
            nall[j + nn] = True
            nall[j - nn] = True

    allnum = nall.copy()

ans = float("inf")
for i in range(N):
    if nall[i] or nall[-1 * i]:
        a = (N-i) // 2
        b = (N+i) // 2

        nans = 0
        if a >= 2:
            nans += nCr(a,2)
        if b >= 2:
            nans += nCr(b,2)
        
        ans = min(nans,ans)

if ans == float("inf"):
    print (-1)
else:
    print (ans)

Submission status: Wrong Answer

Solution code:

"""

m,k 個に分けた場合 mC2+kC2が答え
分けられるもののうち、差が最小となるようにする。

aとbがつながってない
a = 0とすると b = 1
bとcがつながってない c = 0

ぐる0,ぐる1で分かれる
x,y個に分かれた時、abs(x-y)として差を最小にする。

dpで求めればいいか。


[親、所属(0or1)]でunion-find
"""

import math
import sys

def nCr(n,r):
    return math.factorial(n) // (math.factorial(n-r) * math.factorial(r))

def wantp(n):

    wpl = []
    while n != p[n]:
        wpl.append(n)
        n = p[n]

    for i in wpl:
        p[i] = n
    return n


N,M = map(int,input().split())

link = [[False] * N for i in range(N)]

for i in range(M):

    A,B = map(int,input().split())
    A -= 1
    B -= 1

    link[A][B] = True
    link[B][A] = True


#まず普通にUnion-Findする
p = [i for i in range(N)]
rank = [1] * N
for i in range(N):

    for j in range(N):

        if not link[i][j]:
            ip = wantp(i)
            jp = wantp(j)

            if ip != jp:

                if rank[ip] > rank[jp]:
                    p[jp] = ip
                elif rank[ip] < rank[jp]:
                    p[ip] = jp
                else:
                    p[ip] = jp
                    rank[jp] += 1
                    

dic = {} #親がキーで配列内に属す頂点を入れる(これで分類)

for i in range(N):
    np = wantp(i)
    if np not in dic:
        dic[np] = []
    dic[np].append(i)


side = [-1] * N #0と1サイドで分ける

for i in dic: #整合性の確認

    side[i] = 0
    for j in dic[i]:

        for k in dic[i]:
            if j != k and (not link[j][k]) and side[k] != -1:

                if side[j] == -1:
                    side[j] = side[k] ^ 1
                elif side[j] == side[k]:
                    print (-1)
                    sys.exit()


dic2 = {} #キーは先頭[0の数-1の数を入れる]

for i in range(N):
    np = wantp(i)
    if np not in dic2:
        dic2[np] = 0

    if side[i] == 0:
        dic2[np] += 1
    else:
        dic2[np] -= 1

allnum = [False] * 1401
allnum[0] = [True]

for i in dic2:
    dic2[i] = abs(dic2[i])

#dpで中心に一番近い数を求める

for i in dic2:
    nn = dic2[i]
    nall = [False] * 1401
    
    for j in range(1401):
        j -= 701
        if allnum[j]:
            nall[j + nn] = True
            nall[j - nn] = True

    allnum = nall.copy()

ret = 0
while True:
    if allnum[ret] or allnum[-1 * ret]:
        break
    ret += 1

a = N
b = 0

while abs(a-b) != ret:
    a -= 1
    b += 1

ans = 0
if a >= 2:
    ans += nCr(a,2)
if b >= 2:
    ans += nCr(b,2)
print (ans)

Submission status: Wrong Answer

Solution code:

"""

m,k 個に分けた場合 mC2+kC2が答え
分けられるもののうち、差が最小となるようにする。

aとbがつながってない
a = 0とすると b = 1
bとcがつながってない c = 0

ぐる0,ぐる1で分かれる
x,y個に分かれた時、abs(x-y)として差を最小にする。

dpで求めればいいか。


[親、所属(0or1)]でunion-find
"""

import math
import sys

def nCr(n,r):
    return math.factorial(n) // (math.factorial(n-r) * math.factorial(r))

def wantp(n):

    wpl = []
    while n != p[n]:
        wpl.append(n)
        n = p[n]

    for i in wpl:
        p[i] = n
    return n


N,M = map(int,input().split())

link = [[False] * N for i in range(N)]

for i in range(M):

    A,B = map(int,input().split())
    A -= 1
    B -= 1

    link[A][B] = True
    link[B][A] = True


#まず普通にUnion-Findする
p = [i for i in range(N)]
rank = [1] * N
for i in range(N):

    for j in range(N):

        if not link[i][j]:
            ip = wantp(i)
            jp = wantp(j)

            if ip != jp:

                if rank[ip] > rank[jp]:
                    p[jp] = ip
                elif rank[ip] < rank[jp]:
                    p[ip] = jp
                else:
                    p[ip] = jp
                    rank[jp] += 1
                    

dic = {} #親がキーで配列内に属す頂点を入れる(これで分類)

for i in range(N):
    np = wantp(i)
    if np not in dic:
        dic[np] = []
    dic[np].append(i)


side = [-1] * N #0と1サイドで分ける

for i in dic: #整合性の確認

    side[i] = 0
    for j in dic[i]:

        for k in dic[i]:
            if j != k and (not link[j][k]) and side[k] != -1:

                if side[j] == -1:
                    side[j] = side[k] ^ 1
                elif side[j] == side[k]:
                    print (-1)
                    sys.exit()


dic2 = {} #キーは先頭[0の数-1の数を入れる]

for i in range(N):
    np = wantp(i)
    if np not in dic2:
        dic2[np] = 0

    if side[i] == 0:
        dic2[np] += 1
    else:
        dic2[np] -= 1

allnum = [False] * 1401
allnum[0] = [True]

for i in dic2:
    dic2[i] = abs(dic2[i])

#dpで中心に一番近い数を求める

for i in dic2:
    nn = dic2[i]
    nall = [False] * 1401
    
    for j in range(1401):
        if allnum[j]:
            nall[(j + nn)%1401] = True
            nall[j - nn] = True

    allnum = nall.copy()

ret = 0
while True:
    if allnum[ret] or allnum[-1 * ret]:
        break
    ret += 1

a = N
b = 0

while abs(a-b) != ret:
    a -= 1
    b += 1

ans = 0
if a >= 2:
    ans += nCr(a,2)
if b >= 2:
    ans += nCr(b,2)
print (ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, M = map(int, input().split())
adj = [[1 for _ in range(N + 1)] for _ in range(N + 1)]
for _ in range(M):
    a, b = map(int, input().split())
    adj[a][b] = 0
    adj[b][a] = 0
adj_inv = [[] for _ in range(N + 1)]
for i in range(1, N+1):
    for j in range(i+1, N+1):
        if adj[i][j] == 1:
            adj_inv[i].append(j)
            adj_inv[j].append(i)

seen = [0] * (N+1)
num = []
for i in range(1, N+1):
    if seen[i] == 0:
        plus = 0
        minus = 0
        que = deque()
        que.append(i)
        seen[i] = 1
        plus += 1
        while que:
            v = que.pop()
            u_list = adj_inv[v]
            for u in u_list:
                if seen[u] == 0:
                    que.append(u)
                    seen[u] = -seen[v]
                    if seen[u] == 1:
                        plus += 1
                    else:
                        minus += 1
                else:
                    if seen[u] == seen[v]:
                        print(-1)
                        exit()
        num.append((min(plus, minus), max(plus, minus)))

min_sum = 0
add = []
for i in range(len(num)):
    min_sum += num[i][0]
    add.append(num[i][1] - num[i][0])

dp = [[0 for _ in range((N // 2) + 1)] for _ in range(len(add) + 1)]
dp[0][min_sum] = 1
for i in range(len(add)):
    for j in range(min_sum, (N // 2) + 1):
        if dp[i][j] == 1:
            if j + add[i] <= (N // 2):
                dp[i+1][j+add[i]] = 1
            dp[i+1][j] = 1

dp_last = dp[-1]
for i in range(len(dp_last)-1, -1, -1):
    if dp_last[i] == 1:
        N1 = min_sum + i - 1
        break

print((N1 * (N1 - 1)) // 2 + ((N - N1) * (N - N1 - 1)) // 2)

Submission status: Wrong Answer

Solution code:
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]

n,m=MI()
g=[[0]*n for _ in range(n)]
v=[0]*n
f=[0]*n
for i in range(m):
    a,b=MI()
    a,b=a-1,b-1
    g[a][b]=g[b][a]=1

a=set([0])
f[0]=1
for i in range(1,n):
    if all([g[i][j]==1 for j in a]):
        a.add(i)
        f[i]=1
if len(a)==n:
    k=n//2
    print(k*(k-1)//2+(n-k)*(n-k-1)//2)
else:
    
    b=set()
    for i in range(n):
        if f[i]==0:
            b.add(i)
            f[i]=1
            break
    
    for i in range(1,n):
        if all([g[i][j]==1 for j in b]):
            b.add(i)
            f[i]=1
    
    A,B=len(a),len(b)
    if sum(f)<n:
        print(-1)
    else:
        k=min(A,B,n//2)
        print(k*(k-1)//2+(n-k)*(n-k-1)//2)

Submission status: Wrong Answer

Solution code:
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]

n,m=MI()
g=[[0]*n for _ in range(n)]
v=[0]*n
f=[0]*n
for i in range(m):
    a,b=MI()
    a,b=a-1,b-1
    g[a][b]=g[b][a]=1

a=set([0])
f[0]=1
for i in range(1,n):
    if all([g[i][j]==1 for j in a]):
        a.add(i)
        f[i]=1
if len(a)==n:
    k=n//2
    print(k*(k-1)//2+(n-k)*(n-k-1)//2)
else:
    
    b=set()
    for i in range(n):
        if f[i]==0:
            b.add(i)
            break
    
    for i in range(1,n):
        if all([g[i][j]==1 for j in b]):
            b.add(i)
            f[i]=1
    
    A,B=len(a),len(b)
    if sum(f)<n:
        print(-1)
    else:
        k=min(A,B,n//2)
        print(k*(k-1)//2+(n-k)*(n-k-1)//2)

Submission status: Wrong Answer

Solution code:
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]

n,m=MI()
g=[[0]*n for _ in range(n)]
v=[0]*n
f=[0]*n
for i in range(m):
    a,b=MI()
    a,b=a-1,b-1
    g[a][b]=g[b][a]=1

a=set([0])
f[0]=1
for i in range(1,n):
    if all([g[i][j]==1 for j in a]):
        a.add(i)
        f[i]=1
if len(a)==n:
    k=n//2
    print(k*(k-1)//2+(n-k)*(n-k-1)//2)
else:
    
    b=set()
    for i in range(n):
        if f[i]==0:
            b.add(i)
            break
    
    for i in range(1,n):
        if all([g[i][j]==1 for j in b]):
            b.add(i)
            f[i]+=1
    
    A,B=len(a),len(b)
    if A+B<n:
        print(-1)
    else:
        k=min(A,B,n//2)
        print(k*(k-1)//2+(n-k)*(n-k-1)//2)

Submission status: Wrong Answer

Solution code:
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]

n,m=MI()
g=[[0]*n for _ in range(n)]
v=[0]*n
f=[0]*n
for i in range(m):
    a,b=MI()
    a,b=a-1,b-1
    g[a][b]=g[b][a]=1

a=set([0])
f[0]=1
for i in range(1,n):
    if all([g[i][j]==1 for j in a]):
        a.add(i)
        f[i]=1
if len(a)==n:
    k=n//2
    print(k*(k-1)//2+(n-k)*(n-k-1)//2)
else:
    
    b=set()
    for i in range(n):
        if f[i]==0:
            b.add(i)
            f[i]=2
            break
    
    for i in range(1,n):
        if f[i]==0:
            if all([g[i][j]==1 for j in b]):
                b.add(i)
                f[i]=2
    
    A,B=len(a),len(b)
    if A+B<n:
        print(-1)
    else:
        print(A*(A-1)//2+B*(B-1)//2)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3


def dfs(v, cmp_list, visited, even, odd):
    visited[v] = True
    even.add(v)
    for w in cmp_list[v]:
        if w in even:
            return False
        if not visited[w]:
            r = dfs(w, cmp_list, visited, odd, even)
            if not r:
                return False

    return True


def solve(n, m, d):

    f = False
    cmp_list = [[] for _ in range(n)]
    for v in range(n):
        for w in range(v + 1, n):
            if not (v, w) in d:
                cmp_list[v].append(w)
                cmp_list[w].append(v)
                f = True

    if not f:
        m = n // 2
        l = n - m
        return m * (m - 1) // 2 + l * (l - 1) // 2


    graph_sizes = []
    l = 0
    visited = [False] * n
    for i in range(n):
        if not visited[i]:
            if len(cmp_list[i]):
                even = set()
                odd = set()
                r = dfs(i, cmp_list, visited, even, odd)
                if not r:
                    return -1
                l0 = len(even)
                l1 = len(odd)
                if l1 < l0:
                    l0, l1 = l1, l0
                graph_sizes.append((l0, l1))
                l += l0 + l1


    nh = n - n // 2
    dp = [[0] * (nh + 1) for _ in range(len(graph_sizes) + 1)]
    for i in range(len(graph_sizes)):
        l0, l1 = graph_sizes[i]
        for j in range(nh + 1):
            if j < l0:
                dp[i + 1][j] = dp[i][j]
            elif j < l1:
                dp[i + 1][j] = dp[i][j - l0] + l0
            else:
                dp[i + 1][j] = max(dp[i][j - l0] + l0, dp[i][j - l1] + l1)

    m = dp[len(graph_sizes)][nh]
    l -= m
    r = n - m - l
    if nh < m + r:
        m = nh
        l = n - nh
    else:
        m += r

    return m * (m - 1) // 2 + l * (l - 1) // 2


def main():
    n, m = input().split()
    n = int(n)
    m = int(m)
    d = set()
    for _ in range(m):
        a, b = input().split()
        a = int(a) - 1
        b = int(b) - 1
        if b < a:
            a, b = b, a
        d.add((a, b))


    print(solve(n, m, d))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3


def dfs(v, cmp_list, visited, even, odd):
    visited[v] = True
    even.add(v)
    for w in cmp_list[v]:
        if w in even:
            return False
        if not visited[w]:
            r = dfs(w, cmp_list, visited, odd, even)
            if not r:
                return False

    return True


def solve(n, m, d):

    f = False
    cmp_list = [[] for _ in range(n)]
    for v in range(n):
        for w in range(v + 1, n):
            if not (v, w) in d:
                cmp_list[v].append(w)
                cmp_list[w].append(v)
                f = True

    if not f:
        return n * (n - 1) // 2

    graph_sizes = []
    l = 0
    visited = [False] * n
    for i in range(n):
        if not visited[i]:
            if len(cmp_list[i]):
                even = set()
                odd = set()
                r = dfs(i, cmp_list, visited, even, odd)
                if not r:
                    return -1
                l0 = len(even)
                l1 = len(odd)
                if l1 < l0:
                    l0, l1 = l1, l0
                graph_sizes.append((l0, l1))
                l += l0 + l1


    nh = n // 2
    dp = [[0] * (nh + 1) for _ in range(len(graph_sizes) + 1)]
    for i in range(len(graph_sizes)):
        l0, l1 = graph_sizes[i]
        for j in range(nh + 1):
            if j < l0:
                dp[i + 1][j] = dp[i][j]
            elif j < l1:
                dp[i + 1][j] = dp[i][j - l0] + l0
            else:
                dp[i + 1][j] = max(dp[i][j - l0] + l0, dp[i][j - l1] + l1)

    m = dp[len(graph_sizes)][nh]
    l -= m
    r = n - m - l
    if nh < m + r:
        m = nh
        l = n - nh
    else:
        m += r

    return m * (m - 1) // 2 + l * (l - 1) // 2


def main():
    n, m = input().split()
    n = int(n)
    m = int(m)
    d = set()
    for _ in range(m):
        a, b = input().split()
        a = int(a) - 1
        b = int(b) - 1
        if b < a:
            a, b = b, a
        d.add((a, b))


    print(solve(n, m, d))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3


def dfs(v, cmp_list, visited, even, odd):
    visited[v] = True
    even.add(v)
    for w in cmp_list[v]:
        if w in even:
            return False
        if not visited[w]:
            r = dfs(w, cmp_list, visited, odd, even)
            if not r:
                return False

    return True


def solve(n, m, d):

    cmp_list = [[] for _ in range(n)]
    for v in range(n):
        for w in range(v + 1, n):
            if not (v, w) in d:
                cmp_list[v].append(w)
                cmp_list[w].append(v)

    graph_sizes = []
    full = []
    visited = [False] * n
    for i in range(n):
        if not visited[i]:
            if len(cmp_list[i]):
                even = set()
                odd = set()
                r = dfs(i, cmp_list, visited, even, odd)
                if not r:
                    return -1
                graph_sizes.append((len(even), len(odd)))
            else:
                full.append(i)

    nh = n // 2
    if len(graph_sizes):
        dp = [[0] * (nh + 1) for _ in range(len(graph_sizes) + 1)]
        for i, p in enumerate(graph_sizes):
            w0, w1 = p
            if w1 < w0:
                w0, w1 = w1, w0
            for w in range(nh + 1):
                if w < w0:
                    dp[i + 1][w] = dp[i][w]
                elif w < w1:
                    dp[i + 1][w] = dp[i][w - w0] + w0
                else:
                    dp[i + 1][w] = max(dp[i][w - w0] + w0, dp[i][w - w1] + w1)
        l = min(nh, dp[len(graph_sizes)][nh] + len(full))
        m = n - l
    else:
        l = n // 2
        m = (n + 1) // 2

    return l * (l - 1) // 2 + m * (m - 1) // 2


def main():
    n, m = input().split()
    n = int(n)
    m = int(m)
    d = set()
    for _ in range(m):
        a, b = input().split()
        a = int(a) - 1
        b = int(b) - 1
        if b < a:
            a, b = b, a
        d.add((a, b))


    print(solve(n, m, d))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
n,m = map(int,input().split())
G = [[1]*n for i in range(n)]
for i in range(m):
  a,b = map(int,input().split())
  a -=1;b-=1
  G[a][b] = G[b][a] = 0
colors = [-1 for i in range(n)]
possible = True
ni = []
def dfs(x,y):
  colors[x] = y
  k[y] += 1
  for l in range(n):
    if l == x or G[x][l] == 0:
      continue
    if colors[l] == -1:
      dfs(l,1-y)
    elif colors[l] != 1-y:
      global possible
      possible = False
for i in range(n):
  k = [0,0]
  if colors[i] != -1:
    continue
  dfs(i,0)
  ni.append([k[0],k[1]])
if possible == False:
  print(-1)
  exit()
dp =[0]*701
dp[0] = 1
for i in ni:
  for j in range(700,-1,-1):
    if dp[j]:
      dp[j] = 0
      dp[j+i[0]] = 1
      dp[j+i[1]] = 1
ans = float("inf")
for i in range(2,701):
  if dp[i]:
    tmp = (n-i)*(n-i-1)//2 + i*(i-1)//2
    ans = min(tmp,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
n,m = map(int,input().split())
G = [[1]*n for i in range(n)]
for i in range(m):
  a,b = map(int,input().split())
  a -=1;b-=1
  G[a][b] = G[b][a] = 0
colors = [-1 for i in range(n)]
possible = True
ni = []
def dfs(x,y):
  colors[x] = y
  k[y] += 1
  for l in range(n):
    if l == x or G[x][l] == 0:
      continue
    if colors[l] == -1:
      dfs(l,1-y)
    elif colors[l] != 1-y:
      global possible
      possible = False
for i in range(n):
  k = [0,0]
  if colors[i] != -1:
    continue
  dfs(i,0)
  ni.append([k[0],k[1]])
if possible == False:
  print(-1)
  exit()
dp =[0]*701
dp[0] = 1
for i in ni:
  for j in range(700,-1,-1):
    if dp[j]:
      dp[j+i[0]] = 1
      dp[j+i[1]] = 1
ans = float("inf")
for i in range(1,701):
  if dp[i]:
    tmp = (n-i)*(n-i-1)//2 + i*(i-1)//2
    ans = min(tmp,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
n,m = map(int,input().split())
G = [[1]*n for i in range(n)]
for i in range(m):
  a,b = map(int,input().split())
  a -=1;b-=1
  G[a][b] = G[b][a] = 0
colors = [-1 for i in range(n)]
possible = True
ni = []
def dfs(x,y):
  colors[x] = y
  k[y] += 1
  for l in range(n):
    if l == x or G[x][l] == 0:
      continue
    if colors[l] == -1:
      dfs(l,1-y)
    elif colors[l] != 1-y:
      possible = False
for i in range(n):
  k = [0,0]
  if colors[i] != -1:
    continue
  dfs(i,0)
  ni.append([k[0],k[1]])
if possible == False:
  print(-1)
  exit()
dp =[0]*701
dp[0] = 1
for i in ni:
  for j in range(700,-1,-1):
    if dp[j]:
      dp[j+i[0]] = 1
      dp[j+i[1]] = 1
ans = float("inf")
for i in range(1,701):
  if dp[i]:
    tmp = (n-i)*(n-i-1)//2 + i*(i-1)//2
    ans = min(tmp,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
bridge = [[True for _ in range(N)] for _ in range(N)]
for i in range(M):
    a,b = map(int,input().split())
    bridge[a-1][b-1] = False
    bridge[b-1][a-1] = False
connect = [[] for _ in range(N)]
for i in range(N):
    for j in range(N):
        if bridge[i][j] and i!=j:
            connect[i].append(j)
flg = False
used = [False for i in range(N)]
seplist = []
for i in range(N):
    if not used[i]:
        consider = [i]
        g = [0 for i in range(N)]
        g[i] = 1
        while consider != []:
            j = consider.pop()
            used[j] = True
            for k in connect[j]:
                if g[k] == 0:
                    g[k] = -1 if g[j] == 1 else 1
                    consider.append(k)
                elif g[k]*g[j]==1:
                    flg = True
                    break
                
            if flg:
                break
        if flg:
            break
        else:
            seplist.append([g.count(1),g.count(-1)])
            

if flg:
    print(-1)
else:
    canmake = set()
    for s in seplist:
        if canmake == set():
            canmake.add(s[0])
            canmake.add(s[1])
        else:
            c = canmake.copy()
            for j in c:
                canmake.add(s[0]+j)
                canmake.add(s[1]+j)
    cc = list(canmake)
    cc.sort()
    for i in cc:
        if i<=N//2:
            left = i
    right = N-left
    print(left*(left-1)//2+right*(right-1)//2)

Submission status: Wrong Answer

Solution code:
import bisect

N,M = map(int,input().split())
bridge = [[True for _ in range(N)] for _ in range(N)]
for i in range(M):
    a,b = map(int,input().split())
    bridge[a-1][b-1] = False
    bridge[b-1][a-1] = False
connect = [[] for _ in range(N)]
for i in range(N):
    for j in range(N):
        if bridge[i][j] and i!=j:
            connect[i].append(j)
flg = False
used = [False for i in range(N)]
seplist = []
for i in range(N):
    if not used[i]:
        consider = [i]
        g = [0 for i in range(N)]
        g[i] = 1
        while consider != []:
            j = consider.pop()
            used[j] = True
            for k in connect[j]:
                if g[k] == 0:
                    g[k] = -1 if g[j] == 1 else 1
                    consider.append(k)
                elif g[k]*g[j]==1:
                    flg = True
                    break
                
            if flg:
                break
        if flg:
            break
        else:
            seplist.append([g.count(1),g.count(-1)])
            

if flg:
    print(-1)
else:
    canmake = set()
    for s in seplist:
        if canmake == set():
            canmake.add(s[0])
            canmake.add(s[1])
        else:
            c = canmake.copy()
            for j in c:
                canmake.add(s[0]+j)
                canmake.add(s[1]+j)
    cc = list(canmake)
    cc.sort()
    pos = bisect.bisect_right(cc,N//2)
    left = cc[pos-1]
    right = N-left
    print(left*(left-1)//2+right*(right-1)//2)

Submission status: Wrong Answer

Solution code:
import queue
N,M=map(int,input().split())
G=[[1 for i in range(N)] for i in range(N)]
H=[[] for i in range(N)]
for i in range(N):
    G[i][i]-=1
for i in range(M):
    a,b=map(int,input().split())
    G[a-1][b-1]-=1
    G[b-1][a-1]-=1
for i in range(N):
    for j in range(N):
        if G[i][j]==1:
            H[i].append(j)
A=[[[],[]]]
k=0
q=queue.Queue()
reached=[0 for i in range(N)]
dist=[-1 for i in range(N)]
for i in range(N):
    if reached[i]==1:
        continue
    q.put(i)
    reached[i]=1
    dist[i]=0
    while(not(q.empty())):
        r=q.get()
        A[k][dist[r]].append(r)
        for p in H[r]:
            if reached[p]==0:
                q.put(p)
                dist[p]=(dist[r]+1)%2
                reached[p]=1
    k+=1
    A.append([[],[]])
B=[]
for i in range(k):
    B.append((len(A[i][0]),len(A[i][1])))
dp=[[0 for i in range(N+1)] for i in range(k+1)]
dp[0][0]=1
for i in range(1,k+1):
    for j in range(N+1):
        if dp[i-1][j]==0:
            continue
        dp[i][j+B[i-1][0]]=1
        dp[i][j+B[i-1][1]]=1
delta=N
for i in range(N+1):
    if dp[k][i]==1:
        tmp=abs(N-2*i)
        if tmp<delta:
            delta=tmp
X=(N+delta)//2
Y=(N-delta)//2
print((X*(X-1)+Y*(Y-1))//2)

Submission status: Wrong Answer

Solution code:
import queue
N,M=map(int,input().split())
G=[[1 for i in range(N)] for i in range(N)]
for i in range(N):
    G[i][i]-=1
for i in range(M):
    a,b=map(int,input().split())
    G[a-1][b-1]-=1
    G[b-1][a-1]-=1
H=[[] for i in range(N)]
for i in range(N):
    for j in range(N):
        if i==j:
            continue
        if G[i][j]==1:
            H[i].append(j)
dist=[-1 for i in range(N)]
reached=[0 for i in range(N)]
#print(H)
q=queue.Queue()
q.put(0)
dist[0]=0
#for i in G:
#    print(i)
#print("#"*30)
while(not(q.empty())):
    r=q.get()
    reached[r]=1
    for p in H[r]:
        if reached[p]==1:
            continue
        dist[p]=dist[r]+1
        q.put(p)
L=[]
R=[]
for i in range(N):
    if dist[i]==-1:
        continue
    if dist[i]%2==0:
        L.append(i)
    else:
        R.append(i)
for i in range(N):
    if dist[i]!=-1:
        continue
    if len(L)<=len(R):
        L.append(i)
    else:
        R.append(i)
X=len(L)
Y=len(R)
for i in range(X):
    for j in range(X):
        if i==j:
            continue
        if G[L[i]][L[j]]==1:
            print(-1)
            exit()
for i in range(Y):
    for j in range(Y):
        if i==j:
            continue
        if G[R[i]][R[j]]==1:
            print(-1)
            exit()
print((X*(X-1)+Y*(Y-1))//2)