結果

問題 No.979 Longest Divisor Sequence
ユーザー merom686merom686
提出日時 2020-01-31 23:41:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 525 ms / 2,000 ms
コード長 1,587 bytes
コンパイル時間 820 ms
コンパイル使用メモリ 82,720 KB
実行使用メモリ 23,544 KB
最終ジャッジ日時 2023-10-17 13:05:22
合計ジャッジ時間 2,306 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,520 KB
testcase_01 AC 3 ms
4,520 KB
testcase_02 AC 3 ms
4,520 KB
testcase_03 AC 2 ms
4,520 KB
testcase_04 AC 3 ms
4,520 KB
testcase_05 AC 2 ms
4,520 KB
testcase_06 AC 3 ms
4,520 KB
testcase_07 AC 3 ms
4,520 KB
testcase_08 AC 2 ms
4,520 KB
testcase_09 AC 3 ms
4,520 KB
testcase_10 AC 7 ms
4,520 KB
testcase_11 AC 8 ms
4,520 KB
testcase_12 AC 7 ms
4,520 KB
testcase_13 AC 28 ms
16,196 KB
testcase_14 AC 525 ms
23,544 KB
testcase_15 AC 162 ms
9,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct Graph {
    struct Vertex { int k, d; };
    struct Edge { int i; };
    Graph(int n) : v(n, { 0, 0 }), e(n), n(n) {}
    void add_edge(int i, int j) {
        e[i].push_back({ j });
        v[j].k++;
    }
    void tsort() {
        vector<int> q(n);
        int i0 = 0, i1 = 0;
        for (int i = 0; i < n; i++) {
            if (v[i].k == 0) q[i1++] = i, v[i].d = 0;
        }
        int r = 0;
        while (i0 < i1) {
            int i = q[i0++];
            for (auto &o : e[i]) {
                if (--v[o.i].k == 0) q[i1++] = o.i, r = max(r, v[o.i].d = v[i].d + 1);
            }
        }
        cout << r + 1 << endl;
    }
    vector<Vertex> v;
    vector<vector<Edge>> e;
    int n;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    vector<int> a(n), b((int)3e+5 + 1, -1);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    Graph g(n);
    for (int i = 0; i < n; i++) {
        int t = a[i];
        if (t > 1) {
            if (b[1] >= 0) g.add_edge(b[1], i);
            for (int k = 2; k * k <= t; k++) {
                if (t % k == 0) {
                    int l = t / k;
                    if (b[k] >= 0) g.add_edge(b[k], i);
                    if (k == l) break;
                    if (b[l] >= 0) g.add_edge(b[l], i);
                }
            }
        }
        b[t] = i;
    }

    g.tsort();

    return 0;
}
0