結果

問題 No.416 旅行会社
ユーザー uafr_csuafr_cs
提出日時 2016-08-26 23:05:00
言語 Java19
(openjdk 21)
結果
AC  
実行時間 2,634 ms / 4,000 ms
コード長 4,315 bytes
コンパイル時間 2,327 ms
コンパイル使用メモリ 76,892 KB
実行使用メモリ 201,412 KB
最終ジャッジ日時 2023-08-21 09:41:42
合計ジャッジ時間 31,666 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,525 ms
158,184 KB
testcase_01 AC 46 ms
49,388 KB
testcase_02 AC 46 ms
49,508 KB
testcase_03 AC 47 ms
49,476 KB
testcase_04 AC 48 ms
49,408 KB
testcase_05 AC 50 ms
49,536 KB
testcase_06 AC 57 ms
49,692 KB
testcase_07 AC 118 ms
51,384 KB
testcase_08 AC 204 ms
55,424 KB
testcase_09 AC 387 ms
64,856 KB
testcase_10 AC 1,507 ms
158,020 KB
testcase_11 AC 1,612 ms
162,972 KB
testcase_12 AC 1,572 ms
162,272 KB
testcase_13 AC 1,437 ms
166,608 KB
testcase_14 AC 2,634 ms
200,164 KB
testcase_15 AC 2,368 ms
198,632 KB
testcase_16 AC 2,491 ms
201,412 KB
testcase_17 AC 2,364 ms
198,044 KB
testcase_18 AC 2,401 ms
199,064 KB
testcase_19 AC 2,436 ms
163,116 KB
testcase_20 AC 2,418 ms
167,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;

public class Main {

	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 void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);

		final int N = sc.nextInt();
		final int M = sc.nextInt();
		final int Q = sc.nextInt();
		
		ArrayList<HashSet<Integer>> first_adj = new ArrayList<HashSet<Integer>>();
		for(int i = 0; i < N; i++){
			first_adj.add(new HashSet<Integer>());
		}
		ArrayList<HashSet<Integer>> last_adj = new ArrayList<HashSet<Integer>>();
		for(int i = 0; i < N; i++){
			last_adj.add(new HashSet<Integer>());
		}
		
		ArrayList<HashSet<Integer>> query_adj = new ArrayList<HashSet<Integer>>();
		for(int i = 0; i < N; i++){
			query_adj.add(new HashSet<Integer>());
		}
		
		for(int i = 0; i < M; i++){
			final int a = sc.nextInt() - 1;
			final int b = sc.nextInt() - 1;
			first_adj.get(a).add(b);
			first_adj.get(b).add(a);
		}
		
		int[] cs = new int[Q];
		int[] ds = new int[Q];
		for(int i = 0; i < Q; i++){
			cs[i] = sc.nextInt() - 1;
			ds[i] = sc.nextInt() - 1;
			query_adj.get(cs[i]).add(ds[i]);
			query_adj.get(ds[i]).add(cs[i]);
		}
		
		UnionFind uf = new UnionFind(N);
		for(int from = 0; from < N; from++){
			for(final int to : first_adj.get(from)){
				final boolean breaked = query_adj.get(from).contains(to);
				
				if(!breaked){
					uf.union(from, to);
					
					last_adj.get(from).add(to);
					last_adj.get(to).add(from);
				}
			}
		}
		
		
		int[] answer = new int[N];
		boolean[] already = new boolean[N];
		for(int i = 1; i < N; i++){
			if(uf.same(0, i)){
				answer[i] = -1;
				already[i] = true;
			}else{
				answer[i] = 0;
				already[i] = false;
			}
		}
		
		//System.out.println(last_adj);
		
		LinkedList<Integer> queue = new LinkedList<Integer>();
		for(int q = Q - 1; q >= 0; q--){
			final int c = cs[q];
			final int d = ds[q];
			
			//System.out.println(Arrays.toString(uf.par));
			uf.union(c, d);
			
			//System.out.println(c + " " + d);
			
			queue.clear();
			queue.add(c);
			queue.add(d);
			while(!queue.isEmpty()){
				final int node = queue.poll();
				
				if(uf.same(0, node) && !already[node]){
					answer[node] = q + 1;
					already[node] = true;
					
					for(final int next : last_adj.get(node)){
						if(!already[next]){
							queue.add(next);
						}
					}
				}
			}
			
			last_adj.get(c).add(d);
			last_adj.get(d).add(c);
		}
		
		for(int i = 1; i < N; i++){
			System.out.println(answer[i]);
		}
	}
	
	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;
 
		public Scanner(InputStream is) throws IOException {
			br = new BufferedReader(new InputStreamReader(is));
		}	
 
		private void getLine() throws IOException {
			while (!hasNext()) {
				tok = new StringTokenizer(br.readLine());
			}
		}
 
		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}
 
		public String next() throws IOException {
			getLine();
			return tok.nextToken();
		}
 
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
 
		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}
 
		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}
 
		public void close() throws IOException {
			br.close();
		}
	}
}
0