結果

問題 No.812 Change of Class
ユーザー uwi
提出日時 2019-04-12 21:34:54
言語 Java8
(openjdk 1.8.0.222)
結果
AC  
実行時間 479 ms
コード長 4,182 Byte
コンパイル時間 2,773 ms
使用メモリ 36,140 KB
最終ジャッジ日時 2019-09-20 21:59:48

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
02_hitohude_01 AC 241 ms
28,644 KB
02_hitohude_02 AC 136 ms
24,576 KB
02_hitohude_03 AC 193 ms
25,548 KB
02_hitohude_04 AC 273 ms
29,612 KB
02_hitohude_05 AC 257 ms
27,116 KB
02_M_max_01 AC 479 ms
30,116 KB
02_M_max_02 AC 385 ms
29,504 KB
02_M_min_01 AC 112 ms
25,472 KB
02_M_min_02 AC 90 ms
22,956 KB
02_M_N_max_01 AC 445 ms
32,476 KB
02_M_N_max_02 AC 472 ms
32,120 KB
02_N_max_01 AC 136 ms
27,548 KB
02_N_max_02 AC 330 ms
28,388 KB
02_N_min_01 AC 81 ms
21,004 KB
02_N_min_02 AC 80 ms
21,008 KB
02_random_01 AC 336 ms
28,360 KB
02_random_02 AC 123 ms
24,228 KB
02_random_03 AC 294 ms
27,280 KB
02_random_04 AC 245 ms
28,012 KB
02_random_05 AC 262 ms
28,368 KB
02_random_06 AC 448 ms
31,172 KB
02_random_07 AC 244 ms
27,504 KB
02_random_08 AC 170 ms
25,060 KB
02_random_09 AC 120 ms
23,672 KB
02_random_10 AC 129 ms
25,640 KB
02_random_11 AC 158 ms
26,516 KB
02_random_12 AC 409 ms
29,904 KB
02_random_13 AC 342 ms
27,164 KB
02_random_14 AC 266 ms
28,324 KB
02_random_15 AC 140 ms
25,772 KB
02_random_16 AC 337 ms
28,916 KB
02_random_17 AC 299 ms
26,504 KB
02_random_18 AC 181 ms
26,732 KB
02_random_19 AC 337 ms
29,156 KB
02_random_20 AC 128 ms
24,348 KB
02_random_21 AC 155 ms
26,476 KB
02_random_22 AC 131 ms
25,604 KB
02_random_23 AC 210 ms
26,960 KB
02_random_24 AC 121 ms
25,724 KB
02_random_25 AC 220 ms
26,984 KB
02_random_26 AC 132 ms
23,956 KB
02_random_27 AC 117 ms
23,956 KB
02_random_28 AC 365 ms
26,684 KB
02_random_29 AC 157 ms
27,372 KB
02_random_30 AC 283 ms
27,452 KB
02_random_31 AC 133 ms
25,600 KB
02_random_32 AC 147 ms
27,276 KB
02_random_33 AC 270 ms
27,440 KB
02_random_34 AC 300 ms
26,500 KB
02_random_35 AC 161 ms
26,408 KB
02_random_36 AC 109 ms
22,104 KB
02_random_37 AC 153 ms
26,496 KB
02_random_38 AC 203 ms
26,040 KB
02_random_39 AC 140 ms
25,692 KB
02_random_40 AC 139 ms
25,616 KB
02_star_01 AC 243 ms
31,040 KB
02_star_02 AC 315 ms
34,348 KB
02_star_03 AC 316 ms
36,140 KB
02_star_04 AC 198 ms
28,808 KB
02_star_05 AC 325 ms
35,832 KB
sample_1 AC 79 ms
20,996 KB
sample_2 AC 79 ms
21,000 KB
sample_3 AC 79 ms
19,272 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