Problem:


Score : 500 points

Problem Statement

There are N cards placed face down in a row. On each card, an integer 1 or 2 is written.

Let A_i be the integer written on the i-th card.

Your objective is to guess A_1, A_2, ..., A_N correctly.

You know the following facts:

For each i = 1, 2, ..., M, the value A_{X_i} + A_{Y_i} + Z_i is an even number.

You are a magician and can use the following magic any number of times:

Magic: Choose one card and know the integer A_i written on it. The cost of using this magic is 1.

What is the minimum cost required to determine all of A_1, A_2, ..., A_N?

It is guaranteed that there is no contradiction in given input.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq X_i < Y_i \leq N

1 \leq Z_i \leq 100

The pairs (X_i, Y_i) are distinct.

There is no contradiction in input. (That is, there exist integers A_1, A_2, ..., A_N that satisfy the conditions.)

Input

Input is given from Standard Input in the following format:

N M
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_M Y_M Z_M

Output

Print the minimum total cost required to determine all of A_1, A_2, ..., A_N.

Sample Input 1

3 1
1 2 1

Sample Output 1

2

You can determine all of A_1, A_2, A_3 by using the magic for the first and third cards.

Sample Input 2

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

Sample Output 2

2

Sample Input 3

100000 1
1 100000 100

Sample Output 3

99999



Sample input:
3 1
1 2 1


Sample output:
2

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
data = [list(map(int, input().split())) for _ in range(m)]

indices = {}
for i in range(n):
    indices[i + 1] = -1

idx = 0
red = 0
links = set()
for x, y, z in data:
    if indices[x] == -1 and indices[y] == -1:
        indices[x] = idx
        indices[y] = idx
        idx += 1
        red += 1
    elif indices[x] == -1:
        indices[x] = indices[y]
        red += 1
    elif indices[y] == -1:
        indices[y] = indices[x]
        red += 1
    elif indices[x] != indices[y]:
        if indices[x] < indices[y]:
            links.add((indices[x], indices[y]))
        else:
            links.add((indices[y], indices[x]))
print(n - red - len(links))

Submission status: Wrong Answer

Solution code:
#!venv/bin/python
class UnionFind2():
    def __init__(self, n):
        self.parent = [ i for i in range(n)]
        self.rank = [0 for i in range(n)]
        self.size = [1 for i in range(n)]
    
    def find(self, x):
        if(self.parent[x] == x):
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]
    
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y: return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
            self.size[x] = self.size[x] + self.size[y]
            self.size[y] = 0
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.size[y] = self.size[x] + self.size[y]
            self.size[x] = 0
    
    def gsize(self, x):
        return self.size[self.find(x)]
    
    def same(self, x, y):
        return self.find(x) == self.find(y)


N, M = [ int(x) for x in input().split() ]
XYZ = []
for _i in range(M):
    a = [ int(x) for x in input().split() ]
    a[0] -= 1
    a[1] -=1
    XYZ.append(a)

uf = UnionFind2(N)
diff = []
for xyz in XYZ:
    if xyz[2] % 2 == 0:
        uf.unite(xyz[0], xyz[1])
    else:
        diff.append(xyz)
diff_uniq = set()

for d in diff:
    g1, g2 = sorted( [uf.find(d[0]), uf.find(d[1])] )
    key = "p1:" + str(g1) + "," + "p2:" + str(g2)
    diff_uniq.add(key)

groups = set()
for i in range(N):
    groups.add(uf.find(i))


print(len(groups) - len(diff_uniq))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
X=[]
Y=[]
Z=[]
for i in range(m):
  x,y,z=map(int,input().split())
  X.append(x)
  Y.append(y)
  Z.append(z)
a=0
num=[]
num=X+Y
a=n-len(set(num))
exist=[]
b=0
for i in range(m):
  if X[i] not in exist:
    exist.append(X[i])
    if Y[i] not in exist:
      b+=1
      exist.append(Y[i])
print(a+b)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n, m = map(int, input().split())

class UnionFind:
    def __init__(self, n):
        self.par = list(range(n+1))
        self.rank = [0] * (n+1)

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def same_check(self, x, y):
        return self.find(x) == self.find(y)

    def union(self, x, y):
        rx = self.find(x)
        ry = self.find(y)

        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
        else:
            self.par[ry] = rx
        if self.rank[rx] == self.rank[ry]:
            self.rank[rx] += 1

u = UnionFind(n)
for _ in range(m):
    x, y, z = map(int, input().split())
    u.union(x,y)
c = Counter(u.par[1:])
print(len(c))

Submission status: Wrong Answer

Solution code:
import collections
import itertools
import operator


class UnionFind:
    def __init__(self, elems=None):
        class KeyDict(dict):
            def __missing__(self, key):
                self[key] = key
                return key

        self.parent = KeyDict()
        self.rank = collections.defaultdict(int)

        if elems is not None:
            for elem in elems:
                _, _ = self.parent[elem], self.rank[elem]

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1

    def are_same(self, x, y):
        return self.find(x) == self.find(y)

    def grouper(self):
        roots = [(x, self.find(x_par)) for x, x_par in self.parent.items()]
        root = operator.itemgetter(1)
        for _, group in itertools.groupby(sorted(roots, key=root), root):
            yield [x for x, _ in group]

uf=UnionFind()

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

for i in range(M):
    x,y,z=map(int,input().split())
    uf.unite(x,y)

s=set()
used=set()
for i in uf.parent.items():
    s.add(i[1])
    used.add(i[0])

ans=len(s)+N-len(used)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
import sys
s = sys.stdin.readlines()
N, M = map(int, s[0].split())
look_set = set([])
counter = 0
for e in s[1:]:
    X, Y, Z = map(int, e.split())
    if X not in look_set:
        if Y not in look_set:
            counter += 1
    look_set.add(X)
    look_set.add(Y)

O = N - len(look_set) + counter
print(O)

Submission status: Wrong Answer

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

import os
import sys


def main():
    n, m = [int(e) for e in input().split()]
    input_list = list()
    ans_list = [None] * n
    for _ in range(m):
        x, y, z = [int(e) for e in input().split()]
        input_list.append((x, y, z % 2))
    input_list.sort(key=lambda x: x[0])
    for xi, yi, odd in input_list:
        if odd == 1:
            if ans_list[xi - 1] is not None:
                ans_list[yi - 1] = 'B' if ans_list[xi - 1] == 'A' else 'A'
            elif ans_list[yi - 1] is not None:
                ans_list[xi - 1] = 'B' if ans_list[yi - 1] == 'A' else 'A'
            else:
                ans_list[xi - 1] = 'A'
                ans_list[yi - 1] = 'B'
        else:
            if ans_list[xi - 1] is not None:
                ans_list[yi - 1] = ans_list[xi - 1]
            elif ans_list[yi - 1] is not None:
                ans_list[xi - 1] = ans_list[yi - 1]
    print(ans_list.count(None) + 1)




if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
###############################################################################

from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if (self.parent[i] == i):
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if i < j:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)


###############################################################################


def main():
    n, m = intin()
    xyzlist = intinl(m)

    xyzlist.sort()

    count = 0
    prev_x = -1
    longest_y = -1
    in_new_x = False

    for x, y, z in xyzlist:
        if prev_x != x:
            in_new_x = True


        if in_new_x:
            if y > longest_y:
                count += 1
                in_new_x = False
                longest_y = max(y, longest_y)
        prev_x = x

    print(n - count)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
in_list=[]
cards=[]
z_list=[]
count=0
for i in range(m):
  in_list.append(list(map(int,input().split())))

in_list=sorted(in_list)

for j in range(m):
  if in_list[j][0] not in cards and in_list[j][1] not in cards:
    count+=1
  cards.append(in_list[j][0])
  cards.append(in_list[j][1])

cards=set(cards)

count+=n-len(cards)

print(count)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

N,M = map(int,input().split())
X,Y,Z = [0 for i in range(M)],[0 for i in range(M)],[0 for i in range(M)]
info = [[] for i in range(M)]
for i in range(M):
    X[i],Y[i],Z[i] = map(int,input().split())
    info[i] = [X[i],Y[i],Z[i]]

info = list(map(list,set(map(tuple,info))))

info = sorted(info,key=lambda x:x[2])

used_z = []
ans = N
for i in range(len(info)):
    x,y,z = info[i]
    # print(used_z)
    if x in used_z and y in used_z and z in used_z:
        pass
    else:
        # print(x,y,z)
        ans -= 1
    x,y,z = info[i]
    if x <= 100:
        used_z += [x]
    if y <= 100:
        used_z += [y]
    used_z.append(z)
    used_z = list(set(used_z))

print(ans)

Submission status: Wrong Answer

Solution code:
n,m=[int(i) for i in input().split()]
ls=[]
lss=[]
st=set()
for i in range(m):
    l=[i for i in input().split()]
    l.pop(2)
    lss.append(l)
    if not(st.__contains__(l[0]) or st.__contains__(l[1])):
        st.add(l[0])
        st.add(l[1])
        ls.append(l)
from itertools import chain
count=len(set(range(n+1)))-len(set(chain.from_iterable(lss)))-1
print(len(ls) + count)

Submission status: Wrong Answer

Solution code:
N,M = (int(i) for i in input().split())
e = [[int(i) for i in input().split()] for i in range(M)]
hoge = set()

zenbu = {int(i + 1) for i in range(N)}
result = 0

for i in e:
    onon = {i[0],i[1]}
    if onon.issubset(hoge) == False:
        result += 1
    hoge.add(i[0])
    hoge.add(i[1])

hoge_difference = zenbu - hoge
result += len(hoge_difference)

print(result)

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n + 1)]
        self.rank = [0 for _ in range(n + 1)]
        self.size = [1] * (n + 1)
        self.group = [[i] for i in range(n + 1)]

    def find(self, x):
        # If x is root
        if self.parent[x] == x:
            return x
        # If x is not root, search again by using x's parent
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        # Make an edge from the root of lower tree to the root of higher tree
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
            self.size[y] += self.size[x]
        else:
            self.parent[y] = x
            self.size[x] += self.size[y]
            # If the height of tree the tree is the same, increase one of the heights by 1
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def check_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        return self.size[self.find(x)]

    def merge(self, x, y):
        """
        "データ構造をマージする一般的なテク"
        """
        x = self.find(x)
        y = self.find(y)
        if len(self.group[x]) < len(self.group[y]):
            x, y = y, x
        self.group[x].extend(self.group[y])
        self.group[y] = []
        self.parent[y] = x



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

items = []
visible = set()
uf = UnionFind(N)
for i in range(M):
    x, y, z = map(int, input().split())
    uf.union(x, y)
    items.append((x, y, z))

se = set()
ans = 0
for i in range(1, N+1):
    tmp = uf.find(i)
    if tmp in se:
        continue
    else:
        ans += 1
        se.add(i)

print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
data = [0] * (N+1)
data[0] = 2
n = 0
for i in range(M):
    x, y, z = map(int, input().split())
    if x == y:
        data[z] = 2
    elif y == z:
        data[x] = 2
    elif x == z:
        data[y] = 2
    else:
        n += 1

print(data.count(0)-n)

Submission status: Wrong Answer

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

g = [list(map(int, input().split())) for _ in range(m)]

added = [1] * n
sol = 0

for i, j, _ in g:
    if added[i - 1] == added[j - 1] == 1:
        sol += 1
    added[i - 1] = 0
    added[j - 1] = 0

print(sol+sum(added))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
v = [0 for _ in range(n)]
setnum = 0
for _ in range(m):
    x, y ,z = map(int, input().split())
    if v[x-1] == 0 and v[y-1] == 0:
        setnum += 1
        v[x-1] = setnum
        v[y-1] = setnum
    elif v[x-1] != 0 and v[y-1] != 0:
        v = [min(v[x-1], v[y-1]) if x == max(v[x-1], v[y-1]) else x for x in v]
    elif v[x-1] != 0 or v[y-1] != 0:
        v[x-1] = max(v[x-1], v[y-1])
        v[y-1] = max(v[x-1], v[y-1])

ans = sum([1 for x in v if x == 0])
ans += len(set([x for x in v if x > 0]))

print(ans)

Submission status: Wrong Answer

Solution code:
import copy

N, M = map(int, input().split())
X_Y = []
label = [0] * N
for i in range(M):
    X, Y, Z = map(int, input().split())
    X_Y.append([X, Y])
# print(X_Y)
new_X_Y = X_Y
cost = 0
while new_X_Y:
    counter = [0] * N
    for a in new_X_Y:
        for b in a:
            counter[b-1] += 1
    index = counter.index(max(counter))
    rem = index+1
    # print("rem:{}".format(rem))
    new_X_Y = []
    for e in copy.copy(X_Y):
        if rem not in e:
            new_X_Y.append(e)
        else:
            label[e[0]-1] = 1
            label[e[1]-1] = 1
    X_Y = new_X_Y
    cost += 1
    # print(new_X_Y)
# print(label)
count = 0
flag = 0
for i in label:
    if i == 0:
        count += 1
        flag = 1
if flag == 0:
    cost -= 1
print(cost+count)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
appeared = set()
sets = []
for i in range(m):
    x, y, z = map(int, input().split())
    appeared.add(x)
    appeared.add(y)

    inset = []
    for i in range(len(sets)):
        if x in sets[i] or y in sets[i]:
            inset.append(i)
    if inset:
        sets[inset[0]].add(x)
        sets[inset[0]].add(y)

        for i in range(1, len(inset)):
            sets[inset[0]] |= sets[i]
        for i in reversed(inset[1:]):
            del sets[i]
    else:
        sets.append({x, y})

print(n - len(appeared) + len(sets))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = []; Y = []
numSet = {0}
for i in range(M):
    x, y, z = map(int, input().split())
    X.append(x); Y.append(y)

cnt = 0
for i in range(M):
    if X[i] in numSet or Y[i] in numSet:
        cnt += 1
    numSet.add(X[i]); numSet.add(Y[i])

ans = cnt + N - len(numSet)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m=list(map(int,input().split()))
#チェックしたか
checked=[False]*n
#辺があるか（左が小さい）
matrix=[[False]*n for _ in range(n)]
for i in range(m):
  u,v,a=list(map(int,input().split()))
  matrix[u-1][v-1] = True

ans=0

#閉路があればFalse,なければTrue
#既にチェック済のところを踏んだら閉路
def check(c,pre):
  flg=True
  checked[c]=True
  for i in range(n):
    if pre != i and matrix[min(c,i)][max(c,i)]:
      if checked[i]:
        flg=False
      else:
        if flg and check(i,c)==False:
          flg=False
  return flg

for i in range(n):
  if checked[i]==False:
    if check(i,-1):
      ans+=1
print(ans)

Submission status: Wrong Answer

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

N_list = [0] * N
M_list = []
counter = 0

for i in range(M):
    M_list.append(input().split())


for s in range(M):
    X = int(M_list[s][0]) - 1
    Y = int(M_list[s][1]) - 1
    Z = int(M_list[s][2])

    if N_list[X] != 0 and N_list[Y] != 0:
        continue
    
    elif N_list[X] == 0 and N_list[Y] == 0:
        N_list[X] = 1
        counter += 1

        if Z % 2 == 0:
            N_list[Y] = 1
        
        else:
            N_list[Y] = 2
    
    elif N_list[X] == 0:
        if Z % 2 == 0:
            N_list[X] = N_list[Y]
        
        else:
            if N_list[Y] == 1:
                N_list[X] = 2
            
            else:
                N_list[X] = 1
    
    elif N_list[Y] == 0:
        if Z % 2 == 0:
            N_list[Y] = N_list[X]
        
        else:
            if N_list[X] == 1:
                N_list[Y] = 2
            
            else:
                N_list[Y] = 1
    

counter2 = N_list.count(0)

print(counter + counter2)

Submission status: Wrong Answer

Solution code:
import collections
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0] * (n)

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
                
    def ans(self):
        return len(set(self.par))
n,m=map(int,input().split())
U=UnionFind(n)

for i in range(m):
  x,y,z=map(int,input().split())
  U.union(x-1,y-1)
C=collections.Counter(U.par)
ans=0
for j,i in C.most_common():
  if i <=3:
    ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = [int(i) for i in input().split()]

x = []
y = []
z = []

for i in range(m):
    x1,y1, z1 = [int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
    z.append(z1)
    
print(2)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(M)]
class UnionFind:
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
    def find(self, x):
        if self.par[x] == x:
            return x
        self.par[x] = self.find(self.par[x])
        return self.par[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            self.par[y] = x
    def is_same(self, x, y):
        return self.find(x) == self.find(y)
uf = UnionFind(N)
for x, y, z in L:
    uf.union(x - 1, y - 1)
print(len(set(uf.par)))
num = len(set(uf.par))

Submission status: Wrong Answer

Solution code:
import collections
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0] * (n)

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
                
    def ans(self):
        return len(set(self.par))
n,m=map(int,input().split())
U=UnionFind(n)

for i in range(m):
  x,y,z=map(int,input().split())
  U.union(x-1,y-1)
C=collections.Counter(U.par)
ans=0
for j,i in C.most_common():
  if i <=3 or i % 2==1:
    ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

N,M = map(int,input().split())
X,Y,Z = [0 for i in range(M)],[0 for i in range(M)],[0 for i in range(M)]
info = [[] for i in range(M)]
for i in range(M):
    X[i],Y[i],Z[i] = map(int,input().split())
    info[i] = [X[i],Y[i],Z[i]]

info = list(map(list,set(map(tuple,info))))

info = sorted(info,key=lambda x:x[2])

used_z = []
ans = N
for i in range(len(info)):
    x,y,z = info[i]
    if x in used_z and y in used_z and z in used_z:
        pass
    else:
        ans -= 1
    x,y,z = info[i]
    if x <= 100:
        used_z += [x]
    elif y <= 100:
        used_z += [y]
    used_z.append(z)
    used_z = list(set(used_z))

print(ans)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
XYZ = [list(map(int,input().split())) for _ in range(M)]
XYZ.sort(key = lambda x:x[0])

nlist = [0] * N
for i in range(N):
    nlist[i] = i
    
for i in range(M):
    y = XYZ[i][1]-1
    x = XYZ[i][0]-1
    if nlist[y] == y:
        nlist[y] = nlist[x]
    else :
        nlist[x] = nlist[y]
cnt = 0
for i in range(N):
    if nlist[i] == i: cnt += 1
    
print(cnt)

Submission status: Wrong Answer

Solution code:
print(2)

Submission status: Wrong Answer

Solution code:
from sys import stdin
input = stdin.readline
def find(i):
    if parent[i] == i:return i
    else:
        parent[i] = find(parent[i])
        return parent[i]
def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:return
    if rank[x] > rank[y]:
        parent[y] = x
    else:
        parent[x] = y
        if rank[x] == rank[y]:
            rank[y] += 1
def ecount(X):
  ans=1
  for i in range(len(X)-1):
    if X[i]!=X[i+1]:ans+=1
  return ans
n,m=map(int,input().split())
b=[]
parent = [i for i in range(n)]
rank = [0] * n
for _ in range(m):
  x,y,z=map(int,input().split())
  unite(x-1,y-1)
parent.sort()
print(ecount(parent))

Submission status: Wrong Answer

Solution code:
n,m = (int(x) for x in input().split())
x = []
y = []
z = []

l = []
for i in range(n):
    l.append(i)

for i in range(m):
    X,Y,Z = (int(x) for x in input().split())
    #x.append(U)
    #y.append(V)
    #z.append(W)
    X = X-1
    Y = Y-1

    l[X] = min(l[X],l[Y])
    l[Y] = min(l[X],l[Y])

    
print(len(set(l)))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = []; Y = []
numSet = {0}
for i in range(M):
    x, y, z = map(int, input().split())
    X.append(x); Y.append(y)

cnt = 0
for i in range(M):
    if X[i] in numSet or Y[i] in numSet:
        cnt += 1
    numSet.add(X[i]); numSet.add(Y[i])

ans = cnt + N - len(numSet) + 1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
import sys
s = sys.stdin.readlines()
N, M = map(int, s[0].split())
look_set = set([])
counter = 0
for e in s[1:]:
    X, Y, Z = map(int, e.split())
    if X not in look_set:
        if Y not in look_set:
            counter += 1
    look_set.add(X)
    look_set.add(Y)
        
print(counter)

Submission status: Wrong Answer

Solution code:
#!python3
import sys

def main():
    n, m = map(int, input().split())
    xyz = [list(map(int, input().split())) for i in range(m)]

    sets = []

    for ls in xyz:
        x, y, z = ls

        if len(sets) == 0:
            sets.append({x, y})

        else:
            added = False
            for s in sets:
                if x in s:
                    s.add(y)
                    added = True
                    break
                elif y in s:
                    s.add(x)
                    added = True
                    break

            if not added:
                sets.append({x, y})

    print(len(sets) + (n - sum(map(len, sets))))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections

class Solver:
    def __init__(self, _n, _m):
        self.n = _n
        self.m = _m

        self.solve()
    
    def solve(self):
        card = [i for i in range(0, self.n+1)]

        for i in range(0, self.m):
            x, y, z = (int(j) for j in input().split())
            card[y] = card[x]

        col = collections.Counter(card)
        print(len(col) - 1)

def main():
    n, m = (int(i) for i in input().split())
    Solver(n, m)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# 2019/05/19
# AtCoder Beginner Contest 126 - E

# Input
n, m = map(int,input().split())

# Table Init
wl = list()
for i in range(n):
    subl = list()
    subl.append(i)
    subl.append(-1)
    subl.append(list())
    wl.append(subl)

wl.sort()
for j in range(m):
    x, y, z = map(int,input().split())
    if wl[x-1][1] == -1:
        wl[x-1][2].append(y)
    else:
        wl[wl[y-1][1]][2].append(y)
        wl[wl[y-1][1]][2] += wl[y-1][2]
        wl[y-1][2].clear()
    wl[y-1][1] = x-1

cnt = 0
for i in range(n):
    if wl[i][1] == -1:
        cnt += 1

# Output
print(cnt)

Submission status: Wrong Answer

Solution code:
N,M=(int(x) for x in input().split())
X=[]
Y=[]
Z=[]
for n in range(M):
    a,b,c=(int(x) for x in input().split())
    X.append(a)
    Y.append(b)
    Z.append(c)
listX=[]
listY=[]
count=0
for n in range(M):
    if listX.count(X[n])==0:
        listX.append(X[n])
        listY.append(Y[n])
    else:
        if listY.count(Y[n])==0:
            listY.append(Y[n])
        else:
            count=count+1
print(N-M+count)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
li = []
for i in range(m):
    li.append(list(map(int, input().split())))

mark = [0]*n

count = 0
for i in range(m):
    if not (mark[li[i][0]-1] == 1 or mark[li[i][1]-1] == 1):
        count += 1
    mark[li[i][0]-1] = 1
    mark[li[i][1]-1] = 1

count += mark.count(0)
print (count)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
x=[]
y=[]
z=[]
for _ in range(M):
    X,Y,Z = (int(i) for i in input().split()) 
    x.append(X)
    y.append(Y)
    z.append(Z)
    
a = [-1] * N
num=0
finished=0
#nにしたい

for i in range(len(x)):
  if a[x[i]-1]<0:
    a[x[i]-1]=1
    finished=finished+1
  a[y[i]-1]=1
  
    
finished=finished+a.count(-1)
print(finished)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)
        self.size = [1] * (n + 1)

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def same_check(self, x, y):
        return self.find(x) == self.find(y)

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            if self.same_check(x, y) != True:
                self.size[y] += self.size[x]
                self.size[x] = 0
            self.par[x] = y
        else:
            if self.same_check(x, y) != True:
               self.size[x] += self.size[y]
               self.size[y] = 0
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
               self.rank[x] += 1

    def siz(self, x):
        x = self.find(x)
        return self.size[x]

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

N, M = getlist()
U = UnionFind(N)
for i in range(M):
    X, Y, Z = getlist()
    U.union(X, Y)

D = defaultdict(int)
for i in range(1, N + 1):
    D[U.par[i]] += 1

print(len(D))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = []; Y = []
numSet = {0}
for i in range(M):
    x, y, z = map(int, input().split())
    X.append(x); Y.append(y)

cnt = 0
for i in range(M):
    if X[i] in numSet or Y[i] in numSet:
        cnt += 1
    numSet.add(X[i]); numSet.add(Y[i])

ans = cnt + N - len(numSet) + 1

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np

a = list(map(int, input().split()))
N = a[0]
M = a[1]

s = np.array([list(map(int,input().split())) for i in range(M)])