結果

問題 No.416 旅行会社
ユーザー kuuso1kuuso1
提出日時 2016-08-27 00:51:39
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 876 ms / 4,000 ms
コード長 3,462 bytes
コンパイル時間 2,556 ms
コンパイル使用メモリ 108,084 KB
実行使用メモリ 43,884 KB
最終ジャッジ日時 2023-08-21 09:47:19
合計ジャッジ時間 14,446 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 648 ms
39,236 KB
testcase_01 AC 65 ms
16,416 KB
testcase_02 AC 65 ms
16,556 KB
testcase_03 AC 64 ms
16,608 KB
testcase_04 AC 65 ms
16,496 KB
testcase_05 AC 65 ms
16,704 KB
testcase_06 AC 65 ms
16,872 KB
testcase_07 AC 68 ms
16,816 KB
testcase_08 AC 77 ms
17,860 KB
testcase_09 AC 127 ms
21,452 KB
testcase_10 AC 668 ms
39,144 KB
testcase_11 AC 651 ms
40,848 KB
testcase_12 AC 661 ms
40,760 KB
testcase_13 AC 635 ms
39,240 KB
testcase_14 AC 876 ms
43,672 KB
testcase_15 AC 863 ms
43,612 KB
testcase_16 AC 859 ms
43,640 KB
testcase_17 AC 873 ms
43,884 KB
testcase_18 AC 867 ms
43,740 KB
testcase_19 AC 695 ms
35,840 KB
testcase_20 AC 708 ms
35,952 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		var UF = new UnionFind(N);
		
		HashSet<long> H = new HashSet<long>();
		for(int i=0;i<Q;i++){
			H.Add(enc(C[i],D[i]));
		}
		
		for(int i=0;i<M;i++){
			if(H.Contains(enc(A[i],B[i])))continue;
			UF.Unite(A[i],B[i]);
		}
		
		List<int>[] L = new List<int>[N];
		for(int i=0;i<N;i++){
			L[i] = new List<int>();
		}
		
		for(int i=0;i<N;i++){
			L[UF.Parent(i)].Add(i);
		}
		
		
		int[] history = new int[N];
		foreach(var n in L[UF.Parent(0)])history[n] = -1;
		
		
		for(int j=Q-1;j>=0;j--){
			if(UF.IsUnited(C[j],D[j]))continue;
			
			if(UF.IsUnited(0,C[j])){
				foreach(var n in L[UF.Parent(D[j])])history[n] = j+1;
			}
			if(UF.IsUnited(0,D[j])){
				foreach(var n in L[UF.Parent(C[j])])history[n] = j+1;
			}
			
			if(L[UF.Parent(D[j])].Count > L[UF.Parent(C[j])].Count){
				//L[UF.Parent(D[j])].AddRange(L[UF.Parent(C[j])]);
				foreach(var n in L[UF.Parent(C[j])])L[UF.Parent(D[j])].Add(n);
				UF.Unite(C[j],D[j]);
			}else{
				//L[UF.Parent(C[j])].AddRange(L[UF.Parent(D[j])]);
				foreach(var n in L[UF.Parent(D[j])])L[UF.Parent(C[j])].Add(n);
				UF.Unite(D[j],C[j]);
			}
		}
		
		for(int i=1;i<N;i++){
			Console.WriteLine(history[i]);
		}
		
		
		
	}
	int N,M,Q;
	int[] A,B;
	int[] C,D;
	public Sol(){
		var d = ria();
		N = d[0];
		M = d[1];
		Q = d[2];
		A = new int[M];
		B = new int[M];
		for(int i=0;i<M;i++){
			d = ria();
			A[i] = d[0] - 1;
			B[i] = d[1] - 1;
		}
		C = new int[Q];
		D = new int[Q];
		for(int i=0;i<Q;i++){
			d = ria();
			C[i] = d[0] - 1;
			D[i] = d[1] - 1;
		}
	}
	struct Pair{
		public int A,B;
		public Pair(int a,int b){
			A = a;
			B = b;
		}
	}
	static long enc(long a,long b){return (a<<24) + b ;}
	
	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}
class UnionFind{
	
	int[] parent;
	int[] mem;
	int compo;
	int N;
	public UnionFind(int n_){
		Initialize(n_);
	}
	
	public void Initialize(int n_){
		N=n_;
		parent=new int[N];
		mem=new int[N];
		for(int i=0;i<N;i++){
			parent[i]=i;
			mem[i]=1;
		}
		compo=N;
	}
	
	public int Parent(int a){
		if(parent[a]==a)return a;
		return parent[a]=Parent(parent[a]);
	}
	
	public bool IsUnited(int a,int b){
		return Parent(a)==Parent(b);
	}
	
	public void Unite(int a,int b){
		a=Parent(a);b=Parent(b);//Parent()を呼ぶことでa以上のノードは全てparentがrootになる
		if(a==b)return;
		parent[a]=b;
		mem[b]+=mem[a];
		compo-=1;
	}
	
	public bool IsRoot(int x){
		return x==parent[x];
	}
	public int MemCnt(int x){
		return mem[Parent(x)];
	}
	
	public void Dump(){
		for(int i=0;i<parent.Length;i++){
			Console.Write(i==0?"{0}":" {0}",parent[i]);
		}
		Console.WriteLine("");
	}
	
	public int Compo{
		get{
			return compo;
		}
	}
	
}
0