結果

問題 No.812 Change of Class
ユーザー uwiuwi
提出日時 2019-04-12 21:34:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 578 ms / 4,000 ms
コード長 4,182 bytes
コンパイル時間 5,015 ms
コンパイル使用メモリ 87,896 KB
実行使用メモリ 58,000 KB
最終ジャッジ日時 2024-06-12 07:06:25
合計ジャッジ時間 26,540 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 327 ms
49,984 KB
testcase_01 AC 216 ms
46,632 KB
testcase_02 AC 305 ms
48,684 KB
testcase_03 AC 353 ms
51,056 KB
testcase_04 AC 350 ms
49,952 KB
testcase_05 AC 519 ms
50,536 KB
testcase_06 AC 485 ms
50,824 KB
testcase_07 AC 114 ms
44,844 KB
testcase_08 AC 100 ms
40,796 KB
testcase_09 AC 560 ms
53,500 KB
testcase_10 AC 522 ms
51,944 KB
testcase_11 AC 152 ms
46,644 KB
testcase_12 AC 447 ms
51,196 KB
testcase_13 AC 92 ms
39,668 KB
testcase_14 AC 81 ms
38,380 KB
testcase_15 AC 422 ms
50,140 KB
testcase_16 AC 175 ms
44,676 KB
testcase_17 AC 467 ms
50,688 KB
testcase_18 AC 375 ms
49,304 KB
testcase_19 AC 397 ms
49,764 KB
testcase_20 AC 578 ms
51,424 KB
testcase_21 AC 405 ms
49,640 KB
testcase_22 AC 260 ms
47,688 KB
testcase_23 AC 164 ms
43,948 KB
testcase_24 AC 180 ms
45,996 KB
testcase_25 AC 244 ms
47,732 KB
testcase_26 AC 481 ms
50,916 KB
testcase_27 AC 428 ms
50,420 KB
testcase_28 AC 426 ms
49,748 KB
testcase_29 AC 229 ms
47,364 KB
testcase_30 AC 419 ms
50,128 KB
testcase_31 AC 389 ms
49,384 KB
testcase_32 AC 294 ms
48,104 KB
testcase_33 AC 430 ms
50,588 KB
testcase_34 AC 178 ms
45,600 KB
testcase_35 AC 234 ms
47,692 KB
testcase_36 AC 188 ms
46,956 KB
testcase_37 AC 286 ms
48,656 KB
testcase_38 AC 119 ms
45,016 KB
testcase_39 AC 328 ms
48,580 KB
testcase_40 AC 213 ms
46,896 KB
testcase_41 AC 116 ms
44,788 KB
testcase_42 AC 461 ms
49,608 KB
testcase_43 AC 220 ms
46,800 KB
testcase_44 AC 378 ms
49,028 KB
testcase_45 AC 197 ms
46,052 KB
testcase_46 AC 213 ms
46,372 KB
testcase_47 AC 402 ms
49,084 KB
testcase_48 AC 366 ms
49,488 KB
testcase_49 AC 239 ms
47,660 KB
testcase_50 AC 115 ms
39,624 KB
testcase_51 AC 232 ms
47,852 KB
testcase_52 AC 295 ms
48,772 KB
testcase_53 AC 212 ms
47,144 KB
testcase_54 AC 218 ms
46,800 KB
testcase_55 AC 363 ms
56,052 KB
testcase_56 AC 411 ms
56,300 KB
testcase_57 AC 420 ms
57,020 KB
testcase_58 AC 305 ms
50,124 KB
testcase_59 AC 468 ms
58,000 KB
testcase_60 AC 82 ms
38,204 KB
testcase_61 AC 93 ms
38,636 KB
testcase_62 AC 85 ms
38,144 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