Submission #992474
Source Code Expand
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.HashSet; import java.util.Map.Entry; import java.util.StringTokenizer; public class Main { public static void main(String[] args){ try(Scanner sc = new Scanner(System.in)){ final int N = sc.nextInt(); final int M = sc.nextInt(); HashMap<Integer, HashSet<Integer>> speaks = new HashMap<Integer, HashSet<Integer>>(); for(int i = 0; i < N; i++){ final int K = sc.nextInt(); for(int j = 0; j < K; j++){ final int L = sc.nextInt(); if(!speaks.containsKey(L)){ speaks.put(L, new HashSet<Integer>()); } speaks.get(L).add(i); } } UnionFind uf = new UnionFind(N); for(final Entry<Integer, HashSet<Integer>> entry : speaks.entrySet()){ int prev_person = -1; for(final int person : entry.getValue()){ if(prev_person < 0){ prev_person = person; }else{ uf.union(prev_person, person); } } } System.out.println(uf.size(0) == N ? "YES" : "NO"); } } public static class UnionFind{ int[] par; // public UnionFind(int n){ par = new int[n]; for(int i = 0; i < n; i++){ par[i] = -1; } } public int find(int x){ if(par[x] < 0){ return x; }else{ return par[x] = find(par[x]); } } public boolean union(int x, int y){ x = find(x); y = find(y); if(x != y){ if(par[y] < par[x]) { // 多い方が根になるようにスワップする. int tmp = x; x = y; y = tmp; } par[x] += par[y]; par[y] = x; return true; }else{ return false; } } public boolean same(int x, int y){ return find(x) == find(y); } public int size(int x){ return -par[find(x)]; } } public static class Scanner implements AutoCloseable { private BufferedReader br; private StringTokenizer tok; public Scanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } private void getLine() { try { while (!hasNext()) {tok = new StringTokenizer(br.readLine());} } catch(IOException e){ /* ignore */ } } private boolean hasNext() { return tok != null && tok.hasMoreTokens(); } public String next() { getLine(); return tok.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } // 他のnextXXXもXXX.parseXXX()メソッドを使って作れるので省略 public void close() { try{ br.close(); } catch (IOException e){ /*ignore*/ } } } }
Submission Info
Submission Time | |
---|---|
Task | C - Interpretation |
User | tshirakawa |
Language | Java8 (OpenJDK 1.8.0) |
Score | 400 |
Code Size | 2717 Byte |
Status | AC |
Exec Time | 367 ms |
Memory | 47256 KB |
Judge Result
Set Name | sample | dataset1 | dataset2 | ||||||
---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 200 / 200 | 200 / 200 | ||||||
Status |
|
|
|
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 |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01-01.txt | AC | 90 ms | 8400 KB |
01-02.txt | AC | 116 ms | 9040 KB |
01-03.txt | AC | 110 ms | 9164 KB |
01-04.txt | AC | 116 ms | 9040 KB |
01-05.txt | AC | 118 ms | 8908 KB |
01-06.txt | AC | 122 ms | 9040 KB |
01-07.txt | AC | 106 ms | 9040 KB |
01-08.txt | AC | 118 ms | 9040 KB |
01-09.txt | AC | 117 ms | 9040 KB |
01-10.txt | AC | 117 ms | 9040 KB |
02-01.txt | AC | 367 ms | 47256 KB |
02-02.txt | AC | 313 ms | 32180 KB |
02-03.txt | AC | 317 ms | 35464 KB |
02-04.txt | AC | 358 ms | 46640 KB |
02-05.txt | AC | 301 ms | 31800 KB |
02-06.txt | AC | 345 ms | 43692 KB |
02-07.txt | AC | 310 ms | 33056 KB |
02-08.txt | AC | 285 ms | 30636 KB |
02-09.txt | AC | 304 ms | 36268 KB |
02-10.txt | AC | 341 ms | 44252 KB |
02-11.txt | AC | 341 ms | 45256 KB |
02-12.txt | AC | 335 ms | 43948 KB |
02-13.txt | AC | 343 ms | 44824 KB |
sample-01.txt | AC | 90 ms | 8400 KB |
sample-02.txt | AC | 99 ms | 8404 KB |