結果

問題 No.812 Change of Class
ユーザー uwiuwi
提出日時 2019-04-12 21:34:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 496 ms / 4,000 ms
コード長 4,182 bytes
コンパイル時間 4,309 ms
コンパイル使用メモリ 83,852 KB
実行使用メモリ 68,624 KB
最終ジャッジ日時 2023-09-03 01:35:09
合計ジャッジ時間 25,722 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 312 ms
60,980 KB
testcase_01 AC 199 ms
58,324 KB
testcase_02 AC 267 ms
58,628 KB
testcase_03 AC 368 ms
62,016 KB
testcase_04 AC 298 ms
60,728 KB
testcase_05 AC 443 ms
62,444 KB
testcase_06 AC 466 ms
61,340 KB
testcase_07 AC 97 ms
55,916 KB
testcase_08 AC 85 ms
54,796 KB
testcase_09 AC 488 ms
63,572 KB
testcase_10 AC 452 ms
63,368 KB
testcase_11 AC 132 ms
58,792 KB
testcase_12 AC 323 ms
63,068 KB
testcase_13 AC 86 ms
52,284 KB
testcase_14 AC 76 ms
52,868 KB
testcase_15 AC 378 ms
61,072 KB
testcase_16 AC 160 ms
57,884 KB
testcase_17 AC 384 ms
63,004 KB
testcase_18 AC 342 ms
58,740 KB
testcase_19 AC 394 ms
61,284 KB
testcase_20 AC 496 ms
62,648 KB
testcase_21 AC 368 ms
59,432 KB
testcase_22 AC 238 ms
58,316 KB
testcase_23 AC 151 ms
56,920 KB
testcase_24 AC 170 ms
58,208 KB
testcase_25 AC 232 ms
58,968 KB
testcase_26 AC 469 ms
61,036 KB
testcase_27 AC 450 ms
61,276 KB
testcase_28 AC 391 ms
61,588 KB
testcase_29 AC 214 ms
58,700 KB
testcase_30 AC 374 ms
60,984 KB
testcase_31 AC 398 ms
61,380 KB
testcase_32 AC 267 ms
58,684 KB
testcase_33 AC 440 ms
61,216 KB
testcase_34 AC 167 ms
57,948 KB
testcase_35 AC 217 ms
58,572 KB
testcase_36 AC 183 ms
56,996 KB
testcase_37 AC 312 ms
59,472 KB
testcase_38 AC 105 ms
56,540 KB
testcase_39 AC 263 ms
58,676 KB
testcase_40 AC 190 ms
57,920 KB
testcase_41 AC 101 ms
56,140 KB
testcase_42 AC 392 ms
60,408 KB
testcase_43 AC 199 ms
56,656 KB
testcase_44 AC 333 ms
58,780 KB
testcase_45 AC 180 ms
58,860 KB
testcase_46 AC 181 ms
58,504 KB
testcase_47 AC 331 ms
58,688 KB
testcase_48 AC 381 ms
61,020 KB
testcase_49 AC 222 ms
58,792 KB
testcase_50 AC 107 ms
53,716 KB
testcase_51 AC 206 ms
58,676 KB
testcase_52 AC 261 ms
60,476 KB
testcase_53 AC 205 ms
58,400 KB
testcase_54 AC 196 ms
58,308 KB
testcase_55 AC 342 ms
67,880 KB
testcase_56 AC 372 ms
66,744 KB
testcase_57 AC 409 ms
65,720 KB
testcase_58 AC 279 ms
61,044 KB
testcase_59 AC 464 ms
68,624 KB
testcase_60 AC 90 ms
52,596 KB
testcase_61 AC 78 ms
52,600 KB
testcase_62 AC 77 ms
52,232 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