結果

問題 No.1612 I hate Construct a Palindrome
ユーザー nebukuro09nebukuro09
提出日時 2021-07-21 23:18:14
言語 D
(dmd 2.107.1)
結果
WA  
実行時間 -
コード長 3,015 bytes
コンパイル時間 1,106 ms
コンパイル使用メモリ 140,804 KB
実行使用メモリ 42,284 KB
最終ジャッジ日時 2023-09-04 13:24:09
合計ジャッジ時間 12,840 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 1 ms
4,376 KB
testcase_28 WA -
testcase_29 AC 1 ms
4,380 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 2 ms
4,380 KB
testcase_35 WA -
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.datetime;


void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new Tuple!(int, int, int)[][](N);
    int[] Cs;
    foreach (i; 0..M) {
        auto t = readln.split;
        auto u = t[0].to!int - 1;
        auto v = t[1].to!int - 1;
        auto c = (t[2].to!char - 'a').to!int;
        G[u] ~= tuple(v, c, i);
        G[v] ~= tuple(u, c, i);
        Cs ~= c;
    }

    if (Cs.sort().uniq.array.length == 1) {
        writeln(-1);
        return;
    }

    int mid;

    foreach (i; 0..N) {
        if (G[i].map!(t => t[1]).array.sort().uniq.array.length >= 2) {
            mid = i;
            break;
        }
    }

    auto H = new Edge!int[][](N);
    foreach (i; 0..N) foreach (j; G[i]) H[i] ~= Edge!int(j[0], 1);

    auto path1 = dijkstra!(int, 1<<29)(H, 0, mid);
    auto path2 = dijkstra!(int, 1<<29)(H, mid, N-1);

    int[] S;
    int[] ans1;
    int[] ans2;
    foreach (i; 0..path1.length.to!int-1) foreach (j; G[path1[i]]) if (j[0] == path1[i+1]) {
        S ~= j[1];
        ans1 ~= j[2];
    }
    foreach (i; 0..path2.length.to!int-1) foreach (j; G[path2[i]]) if (j[0] == path2[i+1]) {
        S ~= j[1];
        ans2 ~= j[2];
    }

    if (S.dup.sort().uniq.array.length == 1) {
        foreach (j; G[mid]) if (j[1] != S.front) {
            ans1 ~= j[2];
            ans1 ~= j[2];
        }
    }
    int[] ans = ans1 ~ ans2;

    if (is_kaibun(S)) {
        if (path1.length > 1) {
            ans = [ans.front, ans.front] ~ ans;
        } else if (path2.length > 1) {
            ans = ans ~ [ans.back, ans.back];
        }
    }

    writeln(ans.length);
    ans.each!(a => (a+1).writeln);

}

bool is_kaibun(int[] S) {
    foreach (i; 0..S.length.to!int/2) {
        if (S[i] != S[S.length.to!int-i-1]) return false;
    }
    return true;
}

struct Edge(T) {
    int to;
    T cost;
}

int[] dijkstra(T, T inf)(const Edge!(T)[][] graph, int start, int goal) {
    import std.typecons : Tuple, tuple;
    import std.conv : to;
    import std.container : BinaryHeap;

    int n = graph.length.to!int;
    auto dist = new T[](n);
    dist[] = inf;
    dist[start] = 0;

    int[] prev = new int[](n);
    prev[] = -1;

    auto pq = new BinaryHeap!(Array!(Edge!(T)), "a.cost > b.cost");
    pq.insert(Edge!(T)(start, 0));

    while (!pq.empty) {
        auto u = pq.front.to;
        auto cost = pq.front.cost;
        pq.removeFront;
        foreach (const e; graph[u]) {
            auto v = e.to;
            auto next_cost = cost + e.cost;
            if (dist[v] <= next_cost) continue;
            dist[v] = next_cost;
            prev[v] = u;
            pq.insert(Edge!(T)(v, next_cost));
        }
    }

    int[] ans;
    for (int cur = goal; cur != start; cur = prev[cur]) {
        ans ~= cur;
    }
    ans ~= start;

    return ans;
}
0