結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
kuuso1
|
| 提出日時 | 2016-08-27 00:34:30 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,436 bytes |
| コンパイル時間 | 1,026 ms |
| コンパイル使用メモリ | 108,800 KB |
| 実行使用メモリ | 38,144 KB |
| 最終ジャッジ日時 | 2024-11-08 17:38:02 |
| 合計ジャッジ時間 | 12,066 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | TLE * 1 -- * 20 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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<Pair> H = new HashSet<Pair>();
for(int i=0;i<Q;i++){
H.Add(new Pair(C[i],D[i]));
}
for(int i=0;i<M;i++){
if(H.Contains(new Pair(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>(1024);
}
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 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;
}
}
}
kuuso1