結果

問題 No.416 旅行会社
ユーザー mbanmban
提出日時 2017-03-21 00:50:20
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 5,034 bytes
コンパイル時間 4,324 ms
コンパイル使用メモリ 107,956 KB
実行使用メモリ 39,540 KB
最終ジャッジ日時 2023-09-18 13:37:30
合計ジャッジ時間 15,338 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
using System.ComponentModel;

class Program
{
    static void Main(string[] args)
    {
        var alice = new Magatro();
        alice.Scan();
        alice.Solve();
    }
}

public class Scanner
{
    private StreamReader Sr;
    private string[] S;
    private int Index;
    private const char Separator = ' ';

    public Scanner() : this(Console.OpenStandardInput())
    {
        Index = 0;
        S = new string[0];
    }
    public Scanner(Stream source)
    {
        Sr = new StreamReader(source);
    }

    private string[] Line()
    {
        return Sr.ReadLine().Split(Separator);
    }

    public string Next()
    {
        string result;
        if (Index >= S.Length)
        {
            S = Line();
            Index = 0;
        }
        result = S[Index];
        Index++;
        return result;
    }
    public T Next<T>()
    {
        var converter = TypeDescriptor.GetConverter(typeof(T));
        return (T)converter.ConvertFromString(Next());

    }
    public int NextInt()
    {
        return int.Parse(Next());
    }
    public double NextDouble()
    {
        return double.Parse(Next());
    }
    public long NextLong()
    {
        return long.Parse(Next());
    }
    public decimal NextDecimal()
    {
        return decimal.Parse(Next());
    }
    public string[] StringArray(int index = 0)
    {
        Next();
        Index = S.Length;
        return S.Skip(index).ToArray();
    }
    public int[] IntArray(int index = 0)
    {
        return StringArray(index).Select(int.Parse).ToArray();
    }
    public long[] LongArray(int index = 0)
    {
        return StringArray(index).Select(long.Parse).ToArray();
    }
    public bool EndOfStream
    {
        get { return Sr.EndOfStream; }
    }
}

public class Magatro
{
    private int N, M, Q;
    private Bridge[] AB, CD;
    private int[] Parent;
    private List<int> List;
    private int[] Anser;
    private int[] Rank;
    public void Scan()
    {
        Scanner cin = new Scanner();
        N = cin.NextInt();
        M = cin.NextInt();
        Q = cin.NextInt();
        AB = new Bridge[M];
        for (int i = 0; i < M; i++)
        {
            int a = cin.NextInt() - 1;
            int b = cin.NextInt() - 1;
            AB[i] = new Bridge(a, b);
        }
        CD = new Bridge[Q];
        for (int i = 0; i < Q; i++)
        {
            int c = cin.NextInt() - 1;
            int d = cin.NextInt() - 1;
            CD[i] = new Bridge(c, d);
        }
    }

    private bool Link(int x, int y)
    {
        return Root(x) == Root(y);
    }

    private void Union(int x, int y)
    {
        int rootx = Root(x);
        int rooty = Root(y);
        if (rootx == rooty)
        {
            return;
        }
        if (Rank[rootx] < Rank[rooty])
        {
            Parent[rootx] = rooty;
        }
        else
        {
            Parent[rooty] = rootx;
            if(Rank[rootx] == rooty)
            {
                Rank[rootx]++;
            }
        }
    }
    //かっこつけたかった
    private void Swap(ref int a, ref int b)
    {
        a ^= b;
        b ^= a;
        a ^= b;
    }

    private int Root(int x)
    {
        if (Parent[x] == x)
        {
            return x;
        }
        return Root(Parent[x]);
    }

    public void Solve()
    {
        Parent = new int[N];
        Rank = new int[N];
        for (int i = 0; i < N; i++)
        {
            Parent[i] = i;
        }

        List = new List<int>();
        for (int i = 1; i < N; i++)
        {
            List.Add(i);
        }

        for (int i = 0; i < M; i++)
        {
            Bridge b = AB[i];
            if (CD.Contains(b))
            {
                continue;
            }

            Union(b.A, b.B);
        }
        Anser = (new int[N]).Select(i => 0).ToArray();
        List<int> RemoveList = new List<int>();
        foreach (int i in List)
        {
            if (Link(0, i))
            {
                Anser[i] = -1;
                RemoveList.Add(i);
            }
        }
        foreach (int i in RemoveList)
        {
            List.Remove(i);
        }

        for (int i = Q - 1; i >= 0; i--)
        {
            Bridge b = CD[i];
            Union(b.A, b.B);
            RemoveList = new List<int>();
            foreach (int j in List)
            {
                if (Link(0, j))
                {
                    Anser[j] = i + 1;
                    RemoveList.Add(j);
                }
            }
            foreach (int j in RemoveList)
            {
                List.Remove(j);
            }
        }

        for (int i = 1; i < N; i++)
        {
            Console.WriteLine(Anser[i]);
        }
    }

}

struct Bridge
{
    public int A, B;
    public Bridge(int a, int b)
    {
        A = a;
        B = b;
    }
}
0