結果

問題 No.812 Change of Class
ユーザー uwi
提出日時 2019-04-12 21:34:54
言語 Java8
(openjdk 1.8.0.191)
結果
AC  
実行時間 383 ms
コード長 4,182 Byte
コンパイル時間 2,713 ms
使用メモリ 45,712 KB
最終ジャッジ日時 2019-07-03 11:47:52

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
02_hitohude_01 AC 242 ms
38,284 KB
02_hitohude_02 AC 135 ms
34,996 KB
02_hitohude_03 AC 191 ms
36,208 KB
02_hitohude_04 AC 273 ms
39,864 KB
02_hitohude_05 AC 247 ms
39,148 KB
02_M_max_01 AC 383 ms
39,876 KB
02_M_max_02 AC 333 ms
38,252 KB
02_M_min_01 AC 106 ms
34,928 KB
02_M_min_02 AC 88 ms
32,524 KB
02_M_N_max_01 AC 341 ms
42,144 KB
02_M_N_max_02 AC 360 ms
41,580 KB
02_N_max_01 AC 128 ms
35,680 KB
02_N_max_02 AC 261 ms
38,676 KB
02_N_min_01 AC 77 ms
32,196 KB
02_N_min_02 AC 77 ms
30,620 KB
02_random_01 AC 274 ms
37,204 KB
02_random_02 AC 120 ms
34,696 KB
02_random_03 AC 254 ms
36,216 KB
02_random_04 AC 220 ms
37,148 KB
02_random_05 AC 243 ms
36,176 KB
02_random_06 AC 362 ms
41,120 KB
02_random_07 AC 221 ms
36,208 KB
02_random_08 AC 162 ms
37,560 KB
02_random_09 AC 117 ms
34,092 KB
02_random_10 AC 126 ms
35,108 KB
02_random_11 AC 153 ms
37,076 KB
02_random_12 AC 316 ms
39,836 KB
02_random_13 AC 286 ms
40,112 KB
02_random_14 AC 240 ms
36,180 KB
02_random_15 AC 135 ms
33,980 KB
02_random_16 AC 260 ms
38,516 KB
02_random_17 AC 260 ms
37,788 KB
02_random_18 AC 175 ms
37,740 KB
02_random_19 AC 273 ms
38,520 KB
02_random_20 AC 122 ms
33,656 KB
02_random_21 AC 145 ms
37,624 KB
02_random_22 AC 126 ms
32,748 KB
02_random_23 AC 208 ms
35,232 KB
02_random_24 AC 117 ms
37,192 KB
02_random_25 AC 207 ms
36,724 KB
02_random_26 AC 130 ms
33,680 KB
02_random_27 AC 110 ms
33,572 KB
02_random_28 AC 312 ms
37,372 KB
02_random_29 AC 142 ms
36,192 KB
02_random_30 AC 259 ms
35,988 KB
02_random_31 AC 127 ms
33,832 KB
02_random_32 AC 136 ms
37,160 KB
02_random_33 AC 243 ms
37,384 KB
02_random_34 AC 257 ms
37,064 KB
02_random_35 AC 155 ms
36,580 KB
02_random_36 AC 106 ms
32,380 KB
02_random_37 AC 148 ms
36,104 KB
02_random_38 AC 182 ms
37,548 KB
02_random_39 AC 135 ms
34,924 KB
02_random_40 AC 129 ms
35,232 KB
02_star_01 AC 229 ms
40,768 KB
02_star_02 AC 302 ms
45,580 KB
02_star_03 AC 306 ms
45,712 KB
02_star_04 AC 194 ms
40,200 KB
02_star_05 AC 316 ms
44,584 KB
sample_1 AC 75 ms
30,924 KB
sample_2 AC 77 ms
30,424 KB
sample_3 AC 72 ms
30,620 KB
テストケース一括ダウンロード

ソースコード

diff #
package contest190412;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Queue;

public class D {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni();
		int m = ni();
		int[] from = new int[m];
		int[] to = new int[m];
		for (int i = 0; i < m; i++) {
			from[i] = ni() - 1;
			to[i] = ni() - 1;
		}
		int[][] g = packU(n, from, to);
		
		for(int Q = ni();Q > 0;Q--) {
			int x = ni()-1;
			Queue<Integer> q = new ArrayDeque<>();
			q.add(x);
			int ct = 0;
			int[] ds = new int[n];
			Arrays.fill(ds, 99999999);
			ds[x] = 0;
			int maxd = 0;
			while(!q.isEmpty()) {
				int cur = q.poll();
				maxd = Math.max(maxd, ds[cur]);
				ct++;
				for(int e : g[cur]) {
					if(ds[e] > ds[cur] + 1) {
						ds[e] = ds[cur] + 1;
						q.add(e);
					}
				}
			}
			int day = maxd <= 1 ? 0 : Integer.numberOfTrailingZeros(Integer.highestOneBit(maxd-1)) + 1;
			out.println((ct-1) + " " + day);
		}
	}
	
	static int[][] packU(int n, int[] from, int[] to) {
		int[][] g = new int[n][];
		int[] p = new int[n];
		for (int f : from)
			p[f]++;
		for (int t : to)
			p[t]++;
		for (int i = 0; i < n; i++)
			g[i] = new int[p[i]];
		for (int i = 0; i < from.length; i++) {
			g[from[i]][--p[from[i]]] = to[i];
			g[to[i]][--p[to[i]]] = from[i];
		}
		return g;
	}
	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new D().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0