Problem:


Score : 300 points

Problem Statement

There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M.
Road i connects City A_i and City B_i.

Snuke can perform the following operation zero or more times:

Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities.

After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times).

What is the minimum number of roads he must build to achieve the goal?

Constraints

2 \leq N \leq 100,000

1 \leq M \leq 100,000

1 \leq A_i < B_i \leq N

No two roads connect the same pair of cities.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

Print the answer.

Sample Input 1

3 1
1 2

Sample Output 1

1

Initially, there are three cities, and there is a road between City 1 and City 2.

Snuke can achieve the goal by building one new road, for example, between City 1 and City 3.
After that,

We can travel between 1 and 2 directly.

We can travel between 1 and 3 directly.

We can travel between 2 and 3 by following both roads (2 - 1 - 3).



Sample input:
3 1
1 2


Sample output:
1

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
c=[]
d=0

for i in range(M):
    c.extend(map(int,input().split()))

for j in range(1,N):
    if c.count(j) < 2:
        d += 1

if d<2:
    print(0)
else:
    print(d-2)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
g=[[],]*n
for i in range(m):
	u,v=map(int,input().split())
	u-=1
	v-=1
	g[u].append(v)
	g[v].append(u)

vis=[False]*n
stk=[]

cnt=0
for i in range(n):
	if not vis[i]:
		cnt+=1
		stk.append(i)
	while len(stk)!=0:
		u=stk[-1]
		stk.pop()
		if vis[u]:
			continue
		vis[u]=True
		for v in g[u]:
			if not vis[v]:
				stk.append(v)
print(cnt-1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
g=[[],]*n
for i in range(m):
	u,v=map(int,input().split())
	u-=1
	v-=1
	g[u].append(v)
	g[v].append(u)

vis=[False]*n
stk=[]

cnt=0
for i in range(n):
	if not vis[i]:
		cnt+=1
		vis[i]=True
		stk.append(i)
	while len(stk)!=0:
		u=stk[-1]
		stk.pop()
		if vis[u]:
			continue
		for v in g[u]:
			if not vis[v]:
				vis[v]=True
				stk.append(v)
print(cnt-1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
g=[[],]*n
for i in range(m):
	u,v=map(int,input().split())
	u-=1
	v-=1
	g[u].append(v)
	g[v].append(u)

vis=[False]*n
stk=[]

cnt=0
for i in range(n):
	if not vis[i]:
		cnt+=1
		vis[i]=True
		stk.append(i)
	while len(stk)!=0:
		u=stk[-1]
		stk.pop()
		for v in g[u]:
			if not vis[v]:
				vis[v]=True
				stk.append(v)
print(cnt-1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
c=[]
d=0

for i in range(M):
    c.extend(map(int,(input().split())))

for j in range(1,N):
    if c.count(j) < 2:
        d += 1

if d<2:
    print(0)
else:
    print(d-2)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
c=[]
d=0

for i in range(M):
    c.extend(map(int,(input().split())))

for j in range(N):
    if c.count(j) < 2:
        d += 1

if d<2:
    print(0)
else:
    print(d-2)

Submission status: Wrong Answer

Solution code:
cn,rn=map(int,input().split())

pairs = cn - rn
left_cities = cn - rn * pairs
result = pairs - 1 + left_cities - 1
print(result)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())

print(N-1-M)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
ab_list = [list(map(int,input().split())) for nesya in range(m)]
ab_list = sorted(ab_list)
hoge = [1 for motsu in range(n) ]
hoge[0] = 0
for ab in ab_list:
  hoge[ab[1]-1] = 0
print(sum(hoge))

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int,input().split())
c = [0]*(n+1)
ans = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    tmp.sort()
    ans.append(tmp)
ans.sort()
c[ans[0][0]]=ans[0][0]
for i in ans:
    if i[0]==ans[0][0]:
        c[i[1]]=i[0]
    else:
        if c[i[1]]==0 and c[i[0]]==0:
            c[i[0]]=i[0]
            c[i[1]]=i[0]
        elif c[i[0]]==0 or c[i[1]]==0:
            if c[i[0]]<c[i[1]]:
                c[i[0]]=c[i[1]]
            else:
                c[i[1]]=c[i[0]]
        else:
            if c[i[0]]<c[i[1]]:
                c[i[1]]=c[i[0]]
            else:
                c[i[0]]=c[i[1]]
           
cnt = Counter(c)
a=len(cnt)+c.count(0)-3
print(a)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int,input().split())
c = [0]*(n+1)
ans = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    tmp.sort()
    ans.append(tmp)
ans.sort()
c[ans[0][0]]=ans[0][0]
for i in ans:
    if i[0]==ans[0][0]:
        c[i[1]]=i[0]
    else:
        if c[i[1]]==0 and c[i[0]]==0:
            c[i[0]]=i[0]
            c[i[1]]=i[1]
        elif c[i[0]]==0 or c[i[1]]==0:
            if c[i[0]]<c[i[1]]:
                c[i[0]]=c[i[1]]
            else:
                c[i[1]]=c[i[0]]
        else:
            if c[i[0]]<c[i[1]]:
                c[i[1]]=c[i[0]]
            else:
                c[i[0]]=c[i[1]]
           
cnt = Counter(c)
a=len(cnt)+c.count(0)-3
print(a)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int,input().split())
c = [0]*(n+1)
ans = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    tmp.sort()
    ans.append(tmp)
ans.sort()
c[ans[0][0]]=ans[0][0]
for i in ans:
    if i[0]==ans[0][0]:
        c[i[1]]=i[0]
    else:
        if c[i[1]]==0 and c[i[0]]==0:
            c[i[0]]=i[0]
            c[i[1]]=i[1]
        elif c[i[0]]==0 or c[i[1]]==0:
            if c[i[0]]<c[i[1]]:
                c[i[0]]=c[i[1]]
            else:
                c[i[1]]=c[i[0]]
        else:
            if c[i[0]]<c[i[1]]:
                c[i[1]]=c[i[0]]
            else:
                c[i[0]]=c[i[1]]
           
cnt = Counter(c)
a=len(cnt)+c.count(0)-1
print(a)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int,input().split())
c = [0]*(n+1)
ans = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    tmp.sort()
    ans.append(tmp)
ans.sort()
c[ans[0][0]]=ans[0][0]
for i in ans:
    if i[0]==ans[0][0]:
        c[i[1]]=i[0]
    else:
        if c[i[1]]==0 and c[i[0]]==0:
            c[i[0]]=i[0]
            c[i[1]]=i[1]
        elif c[i[0]]==0 or c[i[1]]==0:
            if c[i[0]]<c[i[1]]:
                c[i[0]]=c[i[1]]
            else:
                c[i[1]]=c[i[0]]
        else:
            if c[i[0]]<c[i[1]]:
                c[i[1]]=c[i[0]]
            else:
                c[i[0]]=c[i[1]]
           
cnt = Counter(c)
a=len(cnt)-1+c.count(0)-1
print(a)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int,input().split())
c = [0]*(n+1)
ans = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    tmp.sort()
    ans.append(tmp)
ans.sort()
c[ans[0][0]]=ans[0][0]
for i in ans:
    print(i)
    if i[0]==ans[0][0]:
        c[i[1]]=i[0]
    else:
        if c[i[1]]<=c[i[0]] and c[i[1]]!=0:
            c[i[0]]=c[i[1]]
        elif c[i[0]]<=c[i[1]] and c[i[0]]!=0:
            c[i[1]]=c[i[0]]
        else:
            c[i[0]]=i[0]
            c[i[1]]=i[0]
    print(c)
cnt = Counter(c)
a=len(cnt)-1+c.count(0)-1
print(a)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int,input().split())
c = [0]*(n+1)
ans = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    tmp.sort()
    ans.append(tmp)
ans.sort()
c[ans[0][0]]=ans[0][0]
for i in ans:
    print(i)
    if i[0]==ans[0][0]:
        c[i[1]]=i[0]
    else:
        if c[i[1]]<=c[i[0]] and c[i[1]]!=0:
            c[i[0]]=c[i[1]]
        elif c[i[0]]<=c[i[1]] and c[i[0]]!=0:
            c[i[1]]=c[i[0]]
        else:
            c[i[0]]=i[0]
            c[i[1]]=i[0]
    print(c)
cnt = Counter(c)
cnt = cnt.most_common()
a=len(cnt)-1+c.count(0)-1
print(a)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int,input().split())
c = [0]*(n+1)
ans = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    tmp.sort()
    ans.append(tmp)
ans.sort()
c[ans[0][0]]=ans[0][0]
for i in ans:
    if i[0]==ans[0][0]:
        c[i[1]]=i[0]
    else:
        if c[i[1]]!=0:
            c[i[0]]=c[i[1]]
        else:
            c[i[0]]=i[0]
            c[i[1]]=i[0]
cnt = Counter(c)
cnt = cnt.most_common()
if cnt[0][0]==0:
    print(n-cnt[1][1])
else:
    print(n-cnt[0][1])

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int,input().split())
c = [0]*(n+1)
ans = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    tmp.sort()
    ans.append(tmp)
ans.sort()
c[ans[0][0]]=ans[0][0]
for i in ans:
    if i[0]==ans[0][0]:
        c[i[1]]=i[0]
    else:
        if c[i[1]]!=0:
            c[i[0]]=c[i[1]]
        else:
            c[i[0]]=i[0]
            c[i[1]]=i[0]
cnt = Counter(c)
cnt = cnt.most_common()
if cnt[0][0]==0:
    print(n-1-cnt[1][1])
else:
    print(n-1-cnt[0][1])

Submission status: Wrong Answer

Solution code:
def solve():
	n,m=map(int,input().split())
	a,b=map(int,input().split())
	return n-b


print(solve())

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(' '))

par = [i for i in range(n+1)]

def union_find(x):
    if par[x] == x:
        return x
    else:
        return union_find(par[x])

for i in range(m):
    ai, bi = map(int, input().split(' '))
    root = union_find(ai)
    par[bi] = root
cnt = 0
for i in range(2, n+1):
    root_i = union_find(i)
    if root_i != 1:
        cnt += 1
        par[root_i] = 1

print(cnt)