結果

問題 No.1660 Matrix Exponentiation
ユーザー ruthenruthen
提出日時 2021-08-28 02:04:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 4,797 bytes
コンパイル時間 1,823 ms
コンパイル使用メモリ 179,288 KB
実行使用メモリ 11,208 KB
最終ジャッジ日時 2024-05-01 07:07:34
合計ジャッジ時間 3,312 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 6 ms
7,704 KB
testcase_10 AC 5 ms
7,516 KB
testcase_11 AC 5 ms
7,040 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 59 ms
8,132 KB
testcase_20 AC 38 ms
7,764 KB
testcase_21 AC 61 ms
6,940 KB
testcase_22 AC 5 ms
6,940 KB
testcase_23 AC 26 ms
6,940 KB
testcase_24 AC 82 ms
11,204 KB
testcase_25 AC 50 ms
8,884 KB
testcase_26 AC 84 ms
11,208 KB
testcase_27 AC 64 ms
9,856 KB
testcase_28 AC 75 ms
9,472 KB
testcase_29 AC 72 ms
9,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// clang-format off
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using vii = vector<int>;
using vll = vector<ll>;
using vdd = vector<ld>;
using vvii = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vvdd = vector<vector<ld>>;
using vvvii = vector<vector<vector<int>>>;
using vvvll = vector<vector<vector<ll>>>;
using vvvdd = vector<vector<vector<ld>>>;
template<class T, class U> using P = pair<T, U>;
template<class T> using V1 = vector<T>;
template<class T> using V2 = vector<vector<T>>;
template<class T> using V3 = vector<vector<vector<T>>>;
template<class T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define _overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = 0; i < (n); i++)
#define rep3(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(...) _overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = (n) - 1; i >= 0; i--)
#define rrep2(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rrep3(i, a, b) for (ll i = (b) - 1; i >= (a); i--)
#define rrep(...) _overload3(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define popcnt(x) __builtin_popcountll(x)
#define uniq(x) (x).erase(unique((x).begin(), (x).end()), (x).end());
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const ld DINF = numeric_limits<ld>::infinity();
const ll mod = 1000000007;
//const ll mod = 998244353;
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932;
const ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<typename T> T min(const vector<T> &x) { return *min_element(all(x)); }
template<typename T> T max(const vector<T> &x) { return *max_element(all(x)); }
template<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.fi << ", " << p.se << ")"; }
template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "[ "; for (auto &z : v) os << z << " "; os << "]"; return os; }
#ifdef _DEBUG
#define show(x) cout << #x << " = " << x << endl;
#else
#define show(x)
#endif
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll rem(ll a, ll b) { return (a % b + b) % b; }
// clang-format on

// using mint = modint998244353;

ll modpow(ll A, ll N, ll mod) {
    ll RES = 1;
    // 例えば3=101(2)なので、下位bitから順に1ならa倍する
    while (N) {
        if (N & 1) RES = RES * A % mod;
        A = A * A % mod;
        N >>= 1;
    }
    return RES;
}

vector<int> tsort(vector<vector<int>> &G, int V) {
    vector<int> DEG(V, 0);
    // 入次数をメモ
    for (int i = 0; i < V; i++) {
        for (int T : G[i]) DEG[T]++;
    }
    // 入次数0の点の集合を作成
    queue<int> QUE;
    for (int i = 0; i < V; i++) {
        if (DEG[i] == 0) QUE.push(i);
    }
    vector<int> RES;
    while (QUE.size()) {
        int T = QUE.front();
        QUE.pop();
        RES.push_back(T);
        for (int NX : G[T]) {
            // 入次数を減らす
            DEG[NX]--;
            // 新たな入次数0の点を追加
            if (DEG[NX] == 0) QUE.push(NX);
        }
    }
    return RES;
}

int main() {
    // IOS;
    ll n, k;
    cin >> n >> k;
    vii r(k), c(k), deg(n, 0);
    vvii g(n);
    rep(k) {
        cin >> r[i] >> c[i];
        r[i]--, c[i]--;
        g[r[i]].pb(c[i]);
        deg[c[i]]++;
    }
    queue<int> q;
    rep(n) {
        if (deg[i] == 0) q.push(i);
    }
    show(deg);
    vii res;
    while (!q.empty()) {
        int nex = q.front();
        q.pop();
        res.pb(nex);
        for (int x : g[nex]) {
            deg[x]--;
            if (deg[x] == 0) q.push(x);
        }
    }
    if (sz(res) != n) {
        // 自己ループ
        cout << -1 << endl;
        return 0;
    }
    vll dp(n, 1);
    for (int v : res) {
        for (int nex : g[v]) chmax(dp[nex], dp[v] + 1);
    }
    ll ans = 0;
    rep(n) chmax(ans, dp[i]);
    show(dp);
    cout << ans << endl;
    return 0;
}

/*
メモ
C=AB(行列の掛け算)に対し
c_{i,j}=\sum_{k} a_{i,k}b_{k,j}
より、A^2はi->どこか->jの経路数になる
そこから再帰的に解説に書いてることが言える(と思う…)
*/
0