結果

問題 No.497 入れ子の箱
ユーザー まいん-まいん-
提出日時 2017-03-29 21:42:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 4,559 bytes
コンパイル時間 984 ms
コンパイル使用メモリ 109,444 KB
実行使用メモリ 17,120 KB
最終ジャッジ日時 2023-09-20 20:20:06
合計ジャッジ時間 3,558 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 44 ms
16,988 KB
testcase_04 AC 60 ms
17,116 KB
testcase_05 AC 60 ms
17,060 KB
testcase_06 AC 60 ms
17,060 KB
testcase_07 AC 59 ms
16,928 KB
testcase_08 AC 59 ms
16,836 KB
testcase_09 AC 59 ms
17,024 KB
testcase_10 AC 59 ms
17,004 KB
testcase_11 AC 59 ms
17,044 KB
testcase_12 AC 54 ms
10,724 KB
testcase_13 AC 55 ms
10,424 KB
testcase_14 AC 55 ms
10,380 KB
testcase_15 AC 55 ms
10,516 KB
testcase_16 AC 55 ms
10,556 KB
testcase_17 AC 54 ms
10,648 KB
testcase_18 AC 57 ms
17,060 KB
testcase_19 AC 57 ms
17,108 KB
testcase_20 AC 58 ms
17,080 KB
testcase_21 AC 58 ms
16,864 KB
testcase_22 AC 58 ms
17,120 KB
testcase_23 AC 25 ms
4,376 KB
testcase_24 AC 26 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 56 ms
13,160 KB
testcase_28 AC 57 ms
13,236 KB
testcase_29 AC 56 ms
13,020 KB
testcase_30 AC 34 ms
4,380 KB
testcase_31 AC 34 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <functional>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <vector>
#include <array>
#include <tuple>
#include <utility>
#include <numeric>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <assert.h>
#include <cstdlib>
#include <list>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;

template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {
    return s << "(" << p.first << ", " << p.second << ")";
}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
    s << "[";
    for (int i = 0; i < v.size(); i++) s << (i == 0 ? "" : ", ") << v[i];
    s << "]";
    return s;
}

#define ALL(a) (a).begin(), (a).end()

using Weight = int;

// 重み付きの辺
struct Edge {
    int from, to;
    Weight weight;

    Edge() {}
    Edge(int to) : from(-1), to(to), weight(-1) {}
    Edge(int from, int to) : from(from), to(to), weight(-1) {}
    Edge(int from, int to, Weight weight) : from(from), to(to), weight(weight) {}

    bool operator<(const Edge& e) const {
        return (weight != e.weight) ? (weight < e.weight) : (from < e.from);
    }
    bool operator>(const Edge& e) const {
        return (weight != e.weight) ? (weight > e.weight) : (from > e.from);
    }
};

using Arc = Edge;
using Graph = vector<vector<Edge>>;
using DGraph = vector<vector<Arc>>;

using Tree = Graph;

// 最初に初期化することを忘れないように!
using GArray = vector<Weight>;
using GMatrix = vector<GArray>;

// この4つの関数は頂点数を固定したバージョンでも動く
void add_edge(Graph& g, int u, int v, Weight w = -1) {
    g[u].push_back(Edge(u, v, w));
    g[v].push_back(Edge(v, u, w));
}

void add_arc(DGraph& dg, int u, int v, Weight w = -1) {
    dg[u].push_back(Arc(u, v, w));
}

const int GM_INF = (1LL << 25);

void init_gmatrix(GMatrix& gm) {
    for (int u = 0; u < gm.size(); u++) {
        for (int v = 0; v < gm[u].size(); v++) {
            gm[u][v] = GM_INF;
        }
    }
}

void add_edge(GMatrix& gm, int u, int v, Weight w = -1) {
    gm[u][v] = w;
    gm[v][u] = w;
}

void add_arc(GMatrix& gm, int u, int v, Weight w = -1) {
    gm[u][v] = w;
}

namespace TSort {
    const int MAX_V = 1010;

    DGraph dgraph;
    vector<int> nodes;
    int nodes_i;

    array<bool, MAX_V> used;
    array<int, MAX_V> indeg;

    void bfs(int s) {
        queue<int> que;
        que.push(s);
        used[s] = true;

        while (!que.empty()) {
            int v = que.front(); que.pop();
            nodes[nodes_i++] = v;

            for (Arc a : dgraph[v]) {
                indeg[a.to]--;
                if (indeg[a.to] == 0 && !used[a.to]) {
                    used[a.to] = true;
                    que.push(a.to);
                }
            }
        }
    }

    vector<int>& tsort(DGraph& dg) {
        dgraph = dg;
        nodes = vector<int>(dgraph.size(), -1);
        used.fill(false);
        indeg.fill(0);

        nodes_i = 0;

        for (int v = 0; v < dgraph.size(); v++) {
            for (Arc a : dgraph[v]) {
                indeg[a.to]++;
            }
        }

        for (int v = 0; v < dgraph.size(); v++) {
            if (indeg[v] == 0 && !used[v]) {
                bfs(v);
            }
        }
        return nodes;
    }
};

const int MAX_N = 1010;

vector<int> x, y, z;
int table[MAX_N];

bool can_contain(int u, int v) {
    int us[3] = {x[u], y[u], z[u]},
        vs[3] = {x[v], y[v], z[v]};
    sort(us, us + 3);
    sort(vs, vs + 3);
    for (int i = 0; i < 3; i++) {
        if (us[i] <= vs[i]) return false;
    }
    return true;
}

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

    int n;
    cin >> n;

    x.resize(n);
    y.resize(n);
    z.resize(n);

    DGraph dgraph(n);

    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i] >> z[i];
    }

    for (int u = 0; u < n; u++) {
        for (int v = 0; v < n; v++) {
            if (can_contain(u, v)) {
                add_arc(dgraph, u, v);
            }
        }
    }

    auto nodes = TSort::tsort(dgraph);
    
    for (int i = 0; i < n; i++) {
        int v = nodes[i];
        for (Arc a : dgraph[v]) {
            table[a.to] = max(table[a.to], table[v] + 1);
        }
    }
    cout << *max_element(table, table + n) + 1 << endl;

    return 0;
}
0