Submission #63219453


Source Code Expand

// 期待外れでいたいだなんて
// いつから願ってしまった?
// 名も知れぬほうがいいなんて
// いつからか願ってしまった

// ココロもネジ巻出して
// 意味を失ってしまった
// 何ひとつも動かせない今日と
// 押しつぶすように広がる

// 澄んだ機械仕掛けの空

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// Pragmas
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

// Namespaces
using namespace std;
using namespace __gnu_pbds;

// Data types
using si  = short int;
using ll  = long long;
using ull = unsigned long long;
using lll = __int128;
using ld  = long double;

// Pairs
using pii  = pair<int, int>;
using psi  = pair<si, si>;
using pll  = pair<ll, ll>;
using plll = pair<lll, lll>;
using pld  = pair<ld, ld>;
#define fi first
#define se second

// PBDS
template<typename Z>
using ordered_set = tree<Z, null_type, less<Z>, rb_tree_tag, tree_order_statistics_node_update>;

// Various outputs and debug
template<typename Z, typename = void> struct is_iterable : false_type {};
template<typename Z> struct is_iterable<Z, void_t<decltype(begin(declval<Z>())),decltype(end(declval<Z>()))>> : true_type {};
template<typename Z> typename enable_if<is_iterable<Z>::value&&!is_same<Z, string>::value,ostream&>::type operator<<(ostream &os, const Z &v);

template<typename Y, typename Z> ostream& operator<<(ostream &os, const pair<Y, Z> &p) {
    return os << "(" << p.fi << ", " << p.se << ")";
}
template<class TupType, size_t... I> void printTuple(ostream& os, const TupType& _tup, index_sequence<I...>) {
    os << "(";
    (..., (os << (I == 0? "" : ", ") << get<I>(_tup)));
    os << ")";
}
template<class... Z> ostream& operator<<(ostream& os, const tuple<Z...>& _tup) {
    printTuple(os, _tup, make_index_sequence<sizeof...(Z)>());
    return os;
}
template<typename Z> typename enable_if<is_iterable<Z>::value&&!is_same<Z, string>::value,ostream&>::type operator<<(ostream &os, const Z &v) {
    os << "["; 
    for (auto it = v.begin(); it != v.end();) { os << *it; if (++it != v.end()) os << ", "; }
    return os << "]";
}

#define debug(...)    logger(cout, #__VA_ARGS__, __VA_ARGS__)
#define debugV(v, x)  vlogger(cout, #v, x, v[x]);
#define rrebug(...)   logger(cerr, #__VA_ARGS__, __VA_ARGS__)
#define rrebugV(v, x) vlogger(cerr, #v, x, v[x]);
template <typename... Args>
void logger(ostream& os, string vars, Args &&...values) {
    os << vars << " = "; string delim = "";
    (..., (os << delim << values, delim = ", "));
    os << "\n";
}
template<class Y, class Z>
void vlogger(ostream& os, string var, Y idx, Z val) {
    os << var << "[" << idx << "] = " << val << "\n";
}

// Various macros
#define All(x)            x.begin(), x.end()
#define Sort(x)           sort(All(x))
#define Reverse(x)        reverse(All(x))
#define Uniqueify(x)      Sort(x); x.erase(unique(All(x)), x.end())
#define RandomSeed        chrono::steady_clock::now().time_since_epoch().count()
#define MultipleTestcases int _tc; cin >> _tc; for (int _cur_tc = 1; _cur_tc <= _tc; _cur_tc++)

// Chmin & chmax
template<typename Z> bool chmin(Z &a, Z b) { return (b < a) ? a = b, true : false; }
template<typename Z> bool chmax(Z &a, Z b) { return (b > a) ? a = b, true : false; }
 
// Modular arithmetic
template<int MOD>
class ModInt {
  public:
    int v;
    ModInt() : v(0) {}
    ModInt(long long _v) {
        v = int((-MOD < _v && _v < MOD) ? (_v) : (_v % MOD));
        if (v < 0) v += MOD;
    }
 
    friend bool operator==(const ModInt &a, const ModInt &b) { return a.v == b.v; }
    friend bool operator!=(const ModInt &a, const ModInt &b) { return a.v != b.v; }
    friend bool operator< (const ModInt &a, const ModInt &b) { return a.v <  b.v; }
    friend bool operator<=(const ModInt &a, const ModInt &b) { return a.v <= b.v; }
    friend bool operator> (const ModInt &a, const ModInt &b) { return a.v >  b.v; }
    friend bool operator>=(const ModInt &a, const ModInt &b) { return a.v >= b.v; }
 
    ModInt &operator+=(const ModInt &a) { v = (long long)(v + a.v) % MOD; return *this; }
    ModInt &operator-=(const ModInt &a) { v = (long long)(v - a.v + MOD) % MOD; return *this; }
    ModInt &operator*=(const ModInt &a) { v = 1ll * v * a.v % MOD; return *this; }
    ModInt &operator/=(const ModInt &a) { return (*this) *= inverse(a); }
 
    friend ModInt pow(ModInt a, long long x) {
        ModInt res = 1;
        for (; x; x /= 2, a *= a) if (x & 1) res *= a;
        return res;
    }
    friend ModInt inverse(ModInt a) { return pow(a, MOD - 2); }
 
    ModInt operator+ () const { return ModInt( v); }
    ModInt operator- () const { return ModInt(-v); }
    ModInt operator++() const { return *this += 1; }
    ModInt operator--() const { return *this -= 1; }
 
    friend ModInt operator+(ModInt a, const ModInt &b) { return a += b; }
    friend ModInt operator-(ModInt a, const ModInt &b) { return a -= b; }
    friend ModInt operator*(ModInt a, const ModInt &b) { return a *= b; }
    friend ModInt operator/(ModInt a, const ModInt &b) { return a /= b; }
 
    friend istream &operator>>(istream &is, ModInt &v) { return is >> v.v; }
    friend ostream &operator<<(ostream &os, const ModInt &v) { return os << v.v; }
};
const int ModA = 998244353;
const int ModC = 1e9 + 7;
using MintA    = ModInt<ModA>;
using MintC    = ModInt<ModC>;

// Other constants
const ll INF  = 1e18;
const ll iINF = 1e9;
const ld EPS  = 1e-9;
const ld iEPS = 1e-6;

const int maxN = 123;

int n;
char adj[maxN][maxN];
int dist[maxN][maxN];
bool vis[maxN][maxN];

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> adj[i][j];
            dist[i][j] = iINF;
        }
    }

    queue<pii> pyqe;
    for (int i = 0; i < n; i++) {
        dist[i][i] = 0;
        pyqe.push({i, i});
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) if (j != i) {
            if (adj[i][j] != '-') {
                dist[i][j] = 1;
                pyqe.push({i, j});
            }
        }
    }
    while (!pyqe.empty()) {
        auto [from, to] = pyqe.front();
        pyqe.pop();
        if (vis[from][to]) continue;

        vis[from][to] = true;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (adj[i][from] != '-' && adj[to][j] != '-' && adj[i][from] == adj[to][j]) {
                    if (dist[i][j] > dist[from][to] + 2) {
                        dist[i][j] = dist[from][to] + 2;
                        pyqe.push({i, j});
                    }
                }
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (dist[i][j] >= iINF) dist[i][j] = -1;
            cout << dist[i][j] << " \n"[j == n-1];
        }
    }
}

// dibisakan

Submission Info

Submission Time
Task E - Palindromic Shortest Path
User Zanite
Language C++ 20 (gcc 12.2)
Score 450
Code Size 7255 Byte
Status AC
Exec Time 109 ms
Memory 3780 KiB

Judge Result

Set Name Sample All After Contest
Score / Max Score 0 / 0 450 / 450 0 / 0
Status
AC × 2
AC × 58
AC × 4
Set Name Test Cases
Sample sample00.txt, sample01.txt
All sample00.txt, sample01.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt, testcase42.txt, testcase43.txt, testcase44.txt, testcase45.txt, testcase46.txt, testcase47.txt, testcase48.txt, testcase49.txt, testcase50.txt, testcase51.txt, testcase52.txt, testcase53.txt, testcase54.txt, testcase55.txt
After Contest after_contest00.txt, after_contest01.txt, after_contest02.txt, after_contest03.txt
Case Name Status Exec Time Memory
after_contest00.txt AC 109 ms 3744 KiB
after_contest01.txt AC 97 ms 3596 KiB
after_contest02.txt AC 25 ms 3512 KiB
after_contest03.txt AC 104 ms 3760 KiB
sample00.txt AC 1 ms 3584 KiB
sample01.txt AC 1 ms 3416 KiB
testcase00.txt AC 1 ms 3452 KiB
testcase01.txt AC 1 ms 3516 KiB
testcase02.txt AC 3 ms 3480 KiB
testcase03.txt AC 93 ms 3732 KiB
testcase04.txt AC 1 ms 3548 KiB
testcase05.txt AC 2 ms 3644 KiB
testcase06.txt AC 2 ms 3660 KiB
testcase07.txt AC 3 ms 3512 KiB
testcase08.txt AC 3 ms 3700 KiB
testcase09.txt AC 4 ms 3596 KiB
testcase10.txt AC 2 ms 3544 KiB
testcase11.txt AC 4 ms 3520 KiB
testcase12.txt AC 3 ms 3520 KiB
testcase13.txt AC 5 ms 3568 KiB
testcase14.txt AC 2 ms 3568 KiB
testcase15.txt AC 5 ms 3596 KiB
testcase16.txt AC 1 ms 3484 KiB
testcase17.txt AC 2 ms 3584 KiB
testcase18.txt AC 2 ms 3476 KiB
testcase19.txt AC 3 ms 3512 KiB
testcase20.txt AC 4 ms 3552 KiB
testcase21.txt AC 38 ms 3720 KiB
testcase22.txt AC 14 ms 3624 KiB
testcase23.txt AC 43 ms 3680 KiB
testcase24.txt AC 5 ms 3572 KiB
testcase25.txt AC 50 ms 3592 KiB
testcase26.txt AC 6 ms 3476 KiB
testcase27.txt AC 38 ms 3584 KiB
testcase28.txt AC 15 ms 3532 KiB
testcase29.txt AC 46 ms 3680 KiB
testcase30.txt AC 9 ms 3568 KiB
testcase31.txt AC 50 ms 3676 KiB
testcase32.txt AC 10 ms 3572 KiB
testcase33.txt AC 38 ms 3644 KiB
testcase34.txt AC 7 ms 3592 KiB
testcase35.txt AC 46 ms 3724 KiB
testcase36.txt AC 12 ms 3500 KiB
testcase37.txt AC 50 ms 3604 KiB
testcase38.txt AC 24 ms 3604 KiB
testcase39.txt AC 41 ms 3780 KiB
testcase40.txt AC 41 ms 3668 KiB
testcase41.txt AC 45 ms 3596 KiB
testcase42.txt AC 12 ms 3524 KiB
testcase43.txt AC 52 ms 3736 KiB
testcase44.txt AC 5 ms 3600 KiB
testcase45.txt AC 11 ms 3616 KiB
testcase46.txt AC 5 ms 3648 KiB
testcase47.txt AC 36 ms 3520 KiB
testcase48.txt AC 27 ms 3544 KiB
testcase49.txt AC 41 ms 3768 KiB
testcase50.txt AC 23 ms 3520 KiB
testcase51.txt AC 44 ms 3596 KiB
testcase52.txt AC 5 ms 3552 KiB
testcase53.txt AC 48 ms 3632 KiB
testcase54.txt AC 35 ms 3556 KiB
testcase55.txt AC 50 ms 3596 KiB