Submission #73541004


Source Code Expand

import sys
import string
import math
import bisect
import os
import heapq
import operator
from io import BytesIO, IOBase
from heapq import heappop,heappush
from functools import lru_cache,cache
from copy import copy,deepcopy
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,accumulate
from array import array


INF = float('inf')
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = sys.stdin.buffer.readline

ask = lambda *x:print('?',*x,flush=True)
reply = lambda *x:print('!',*x,flush=True)

RI = lambda: int(sys.stdin.readline())
RF = lambda: float(sys.stdin.readline())
RS = lambda: sys.stdin.readline().strip()
RFF = lambda: map(float, sys.stdin.readline().split())
RII = lambda: map(int, sys.stdin.readline().split())
RSS = lambda: map(str, sys.stdin.readline().strip().split())
RIL = lambda: list(RII())
RFL = lambda: list(RFF())
RSL = lambda: list(RSS())

from types import GeneratorType
def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break 
                    to = stack[-1].send(to)
            return to

    return wrappedfunc

    
def main():
    test = RI()
    
    for _ in range(test):
        n = RI()

        a = []
        for i in range(n):
            x,y,z = RII()
            a.append((x,y,z,i))
        
        a.sort(key=lambda x:x[0])
        
        g = [[]for i in range(n)]
        for i in range(n-1):
            x,y = a[i][0],a[i+1][0]
            u,v = a[i][3],a[i+1][3]
            g[u].append(v)
            if x==y:
                g[v].append(u)
                
        a.sort(key=lambda x:x[1])
        for i in range(n-1):
            x,y = a[i][1],a[i+1][1]
            u,v = a[i][3],a[i+1][3]
            g[u].append(v)
            if x==y:
                g[v].append(u)

        a.sort(key=lambda x:x[2])
        for i in range(n-1):
            x,y = a[i][2],a[i+1][2]
            u,v = a[i][3],a[i+1][3]
            g[u].append(v)
            if x==y:
                g[v].append(u) 
        
        st = a[-1][3]
        vis = [False]*n
        q = deque()
        vis[st] = True
        q.append(st)
        while q:
            u = q.popleft()
            for i in g[u]:
                if vis[i]==False:
                    vis[i] = True
                    q.append(i)
        
        ans = vis.count(True)
        print(ans)

if __name__ == '__main__':
    main()

                

Submission Info

Submission Time
Task C - Strong Surname
User x3x3
Language Python (PyPy 3.11-v7.3.20)
Score 600
Code Size 4506 Byte
Status AC
Exec Time 880 ms
Memory 173208 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 1
AC × 23
Set Name Test Cases
Sample 00_sample_01.txt
All 00_sample_01.txt, hand-12.txt, hand-13.txt, hand-14.txt, hand-15.txt, hand-16.txt, hand-17.txt, hand-18.txt, hand-19.txt, hand-20.txt, hand-21.txt, hand-22.txt, random-01.txt, random-02.txt, random-03.txt, random-04.txt, random-05.txt, random-06.txt, random-07.txt, random-08.txt, random-09.txt, random-10.txt, random-11.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 126 ms 110284 KiB
hand-12.txt AC 312 ms 117212 KiB
hand-13.txt AC 520 ms 134148 KiB
hand-14.txt AC 834 ms 172084 KiB
hand-15.txt AC 789 ms 171628 KiB
hand-16.txt AC 834 ms 171776 KiB
hand-17.txt AC 550 ms 135576 KiB
hand-18.txt AC 529 ms 135528 KiB
hand-19.txt AC 390 ms 151908 KiB
hand-20.txt AC 528 ms 132240 KiB
hand-21.txt AC 526 ms 136204 KiB
hand-22.txt AC 222 ms 135112 KiB
random-01.txt AC 283 ms 116072 KiB
random-02.txt AC 279 ms 116144 KiB
random-03.txt AC 373 ms 119032 KiB
random-04.txt AC 542 ms 132788 KiB
random-05.txt AC 629 ms 147536 KiB
random-06.txt AC 731 ms 164136 KiB
random-07.txt AC 861 ms 173048 KiB
random-08.txt AC 855 ms 173208 KiB
random-09.txt AC 880 ms 172644 KiB
random-10.txt AC 856 ms 172268 KiB
random-11.txt AC 841 ms 172532 KiB