結果

問題 No.470 Inverse S+T Problem
ユーザー eitahoeitaho
提出日時 2016-12-20 01:01:03
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 5,830 bytes
コンパイル時間 876 ms
コンパイル使用メモリ 120,392 KB
実行使用メモリ 29,400 KB
最終ジャッジ日時 2024-06-01 22:28:45
合計ジャッジ時間 2,693 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
25,188 KB
testcase_01 AC 27 ms
25,448 KB
testcase_02 AC 26 ms
25,076 KB
testcase_03 AC 27 ms
26,988 KB
testcase_04 AC 26 ms
25,320 KB
testcase_05 AC 26 ms
27,104 KB
testcase_06 AC 22 ms
24,808 KB
testcase_07 AC 21 ms
25,004 KB
testcase_08 AC 23 ms
24,880 KB
testcase_09 AC 25 ms
25,064 KB
testcase_10 AC 26 ms
25,320 KB
testcase_11 AC 27 ms
25,448 KB
testcase_12 AC 26 ms
25,004 KB
testcase_13 AC 26 ms
25,064 KB
testcase_14 AC 27 ms
29,400 KB
testcase_15 AC 27 ms
25,320 KB
testcase_16 AC 26 ms
25,068 KB
testcase_17 AC 25 ms
23,220 KB
testcase_18 AC 26 ms
25,440 KB
testcase_19 AC 26 ms
25,132 KB
testcase_20 AC 25 ms
22,964 KB
testcase_21 AC 26 ms
25,068 KB
testcase_22 AC 27 ms
25,064 KB
testcase_23 AC 27 ms
24,928 KB
testcase_24 AC 27 ms
27,240 KB
testcase_25 AC 27 ms
25,320 KB
testcase_26 AC 29 ms
27,244 KB
testcase_27 AC 28 ms
23,096 KB
testcase_28 AC 22 ms
26,848 KB
testcase_29 AC 22 ms
24,812 KB
testcase_30 AC 21 ms
24,784 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.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    static readonly string NG = "Impossible";
    static readonly int Atmost = 52 + 52 * 52;

    public void Solve()
    {
        int N = Reader.Int();
        if (N > Atmost / 2) { Console.WriteLine(NG); return; }
        var S = Reader.StringArray(N);
        var scc = new StronglyConnectedComponent(N * 2);

        for (int a = 0; a < N; a++)
            for (int b = a + 1; b < N; b++)
                for (int alen = 1; alen <= 2; alen++)
                    for (int blen = 1; blen <= 2; blen++)
                    {
                        string a1 = S[a].Substring(0, alen), a2 = S[a].Substring(alen);
                        string b1 = S[b].Substring(0, blen), b2 = S[b].Substring(blen);
                        int aid = a + (alen - 1) * N;
                        int bid = b + (2 - blen) * N;
                        if (a1 == b1 || a1 == b2 || a2 == b1 || a2 == b2)
                        {
                            scc.AddEdge(a + (alen - 1) * N, b + (2 - blen) * N);
                            scc.AddEdge(b + (blen - 1) * N, a + (2 - alen) * N);
                        }
                    }
        scc.Decompose();
        if (Enu.Range(0, N).Any(i => scc.Component(i) == scc.Component(N + i)))
        {
            Console.WriteLine(NG);
            return;
        }
        for (int i = 0; i < N; i++)
            if (scc.Component(i) > scc.Component(N + i))
                Console.WriteLine(S[i].Insert(1, " "));
            else
                Console.WriteLine(S[i].Insert(2, " "));
    }
}


class Component
{
    public int[] Vertexes;
    public int[] Edges;
}

class StronglyConnectedComponent
{
    public int NumComponents { get; private set; }
    public int[] Vertexes(int at) { return vertexes[component[at]].ToArray(); }
    public int Component(int v) { return component[v]; }
    private readonly int[] component;
    private readonly List<int>[] E, RevE;
    private readonly List<List<int>> vertexes = new List<List<int>>();

    public StronglyConnectedComponent(int N)
    {
        component = new int[N];
        E = new List<int>[N];
        RevE = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            component[i] = -1;
            E[i] = new List<int>();
            RevE[i] = new List<int>();
        }
    }
    public void AddEdge(int from, int to)
    {
        E[from].Add(to);
        RevE[to].Add(from);
    }
    public void Decompose()
    {
        var vs = new List<int>();
        var seen = new bool[E.Length];
        for (int v = 0; v < E.Length; v++)
            if (!seen[v]) DFS(v, seen, vs);
        vs.Reverse();
        foreach (int v in vs)
            if (component[v] == -1)
            {
                vertexes.Add(new List<int>());
                DFSRev(v, NumComponents++);
            }
    }
    public Component[] GetComponents()
    {
        var components = new Component[vertexes.Count];
        for (int i = 0; i < components.Length; i++)
        {
            components[i] = new Component();
            components[i].Vertexes = vertexes[i].ToArray();
            var es = new HashSet<int>();
            foreach (int v in components[i].Vertexes)
                foreach (int next in E[v])
                    if (component[next] != i)
                        es.Add(component[next]);
            components[i].Edges = es.ToArray();
        }
        return components;
    }

    private void DFS(int v, bool[] seen, List<int> vs)
    {
        seen[v] = true;
        foreach (int next in E[v])
            if (!seen[next]) DFS(next, seen, vs);
        vs.Add(v);
    }
    private void DFSRev(int v, int num)
    {
        component[v] = num;
        vertexes[num].Add(v);
        foreach (int next in RevE[v])
            if (component[next] == -1) DFSRev(next, num);
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0