Submission #7606585


Source Code Expand

import java.io.*;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import java.util.Map.Entry;

public class Main implements Runnable {
    static final int MOD = (int) 1e9 + 7;
    static final int MI = (int) 1e9;
    static final long ML = (long) 1e18;
    static final Reader in = new Reader();
    static final PrintWriter out = new PrintWriter(System.out);
    StringBuilder answer = new StringBuilder();
    Random random = new Random(751454315315L + System.currentTimeMillis());

    public static void main(String[] args) {
        new Thread(null, new Main(), "persefone", 1 << 28).start();
    }

    @Override
    public void run() {
        solve();
        printf();
        flush();
    }

    void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int[] cnt = IntStream.generate(() -> -1).limit(m + 1).toArray();
        DSU dsu = new DSU(n);
        for (int i = 0; i < n; i++) {
            int k = in.nextInt();
            for (int j = 0; j < k; j++) {
                int l = in.nextInt();
                if (cnt[l] == -1) {
                    cnt[l] = i;
                } else {
                    dsu.merge(i, cnt[l]);
                }
            }
        }

        printf(dsu.connectedComponents == 1 ? "YES" : "NO") ;

    }

    static class DSU {
        int vertices, connectedComponents;
        int[] ancestor, rank;

        public DSU(int vertices) {
            this.vertices = vertices;
            this.connectedComponents = vertices;
            this.ancestor = IntStream.range(0, vertices).toArray();
            this.rank = IntStream.generate(() -> 1).limit(vertices).toArray();
        }

        public int find(int vertex) {
            while (vertex != ancestor[vertex]) {
                vertex = ancestor[vertex];
            }
            return vertex;
        }

        public void merge(int from, int to) {
            from = find(from);
            to = find(to);
            if (from == to) {
                return;
            }
            connectedComponents--;
            if (rank[from] > rank[to]) {
                ancestor[to] = from;
                rank[from] += rank[to];
            } else {
                ancestor[from] = to;
                rank[to] += rank[from];
            }
        }
    }

    void printf() {
        out.print(answer);
    }

    void close() {
        out.close();
    }

    void flush() {
        out.flush();
    }

    void printf(Stream<?> str) {
        str.forEach(o -> add(o, " "));
        add("\n");
    }


    void printf(Object... obj) {
        printf(false, obj);
    }

    void printfWithDescription(Object... obj) {
        printf(true, obj);
    }


    private void printf(boolean b, Object... obj) {

        if (obj.length > 1) {
            for (int i = 0; i < obj.length; i++) {
                if (b) add(obj[i].getClass().getSimpleName(), " - ");
                if (obj[i] instanceof Collection<?>) {
                    printf((Collection<?>) obj[i]);
                } else if (obj[i] instanceof int[][]) {
                    printf((int[][]) obj[i]);
                } else if (obj[i] instanceof long[][]) {
                    printf((long[][]) obj[i]);
                } else if (obj[i] instanceof double[][]) {
                    printf((double[][]) obj[i]);
                } else printf(obj[i]);
            }
            return;
        }

        if (b) add(obj[0].getClass().getSimpleName(), " - ");
        printf(obj[0]);
    }

    void printf(Object o) {
        if (o instanceof int[])
            printf(Arrays.stream((int[]) o).boxed());
        else if (o instanceof char[])
            printf(new String((char[]) o));
        else if (o instanceof long[])
            printf(Arrays.stream((long[]) o).boxed());
        else if (o instanceof double[])
            printf(Arrays.stream((double[]) o).boxed());
        else if (o instanceof boolean[]) {
            for (boolean b : (boolean[]) o) add(b, " ");
            add("\n");
        } else
            add(o, "\n");
    }

    void printf(int[]... obj) {
        for (int i = 0; i < obj.length; i++) printf(obj[i]);
    }

    void printf(long[]... obj) {
        for (int i = 0; i < obj.length; i++) printf(obj[i]);
    }

    void printf(double[]... obj) {
        for (int i = 0; i < obj.length; i++) printf(obj[i]);
    }

    void printf(boolean[]... obj) {
        for (int i = 0; i < obj.length; i++) printf(obj[i]);
    }

    void printf(Collection<?> col) {
        printf(col.stream());
    }

    <T, K> void add(T t, K k) {
        if (t instanceof Collection<?>) {
            ((Collection<?>) t).forEach(i -> add(i, " "));
        } else if (t instanceof Object[]) {
            Arrays.stream((Object[]) t).forEach(i -> add(i, " "));
        } else
            add(t);
        add(k);
    }


    <T> void add(T t) {
        answer.append(t);
    }

    static class Reader {
        private BufferedReader br;
        private StringTokenizer st;

        Reader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        Reader(String fileName) throws FileNotFoundException {
            br = new BufferedReader(new FileReader(fileName));
        }

        boolean isReady() throws IOException {
            return br.ready();
        }

        String next() {
            try {
                while (st == null || !st.hasMoreTokens()) {
                    st = new StringTokenizer(br.readLine());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++)
                arr[i] = nextInt();
            return arr;
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String s = "";
            try {
                s = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return s;
        }
    }
}

Submission Info

Submission Time
Task C - Interpretation
User hongquanntct
Language Java8 (OpenJDK 1.8.0)
Score 400
Code Size 6795 Byte
Status AC
Exec Time 310 ms
Memory 46584 KB

Judge Result

Set Name sample dataset1 dataset2
Score / Max Score 0 / 0 200 / 200 200 / 200
Status
AC × 2
AC × 12
AC × 27
Set Name Test Cases
sample sample-01.txt, sample-02.txt
dataset1 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt
dataset2 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, sample-01.txt, sample-02.txt
Case Name Status Exec Time Memory
01-01.txt AC 203 ms 26580 KB
01-02.txt AC 170 ms 25812 KB
01-03.txt AC 173 ms 25428 KB
01-04.txt AC 162 ms 25812 KB
01-05.txt AC 181 ms 25684 KB
01-06.txt AC 172 ms 27728 KB
01-07.txt AC 174 ms 25940 KB
01-08.txt AC 172 ms 26324 KB
01-09.txt AC 163 ms 28372 KB
01-10.txt AC 179 ms 26320 KB
02-01.txt AC 272 ms 34752 KB
02-02.txt AC 293 ms 44308 KB
02-03.txt AC 265 ms 38380 KB
02-04.txt AC 301 ms 46584 KB
02-05.txt AC 302 ms 45608 KB
02-06.txt AC 306 ms 43692 KB
02-07.txt AC 302 ms 45428 KB
02-08.txt AC 284 ms 45200 KB
02-09.txt AC 310 ms 43564 KB
02-10.txt AC 274 ms 41824 KB
02-11.txt AC 284 ms 43096 KB
02-12.txt AC 284 ms 46516 KB
02-13.txt AC 283 ms 41384 KB
sample-01.txt AC 153 ms 27860 KB
sample-02.txt AC 150 ms 25940 KB