結果

問題 No.497 入れ子の箱
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-06-05 00:14:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 44 ms / 5,000 ms
コード長 2,468 bytes
コンパイル時間 2,070 ms
コンパイル使用メモリ 162,120 KB
実行使用メモリ 6,632 KB
最終ジャッジ日時 2023-08-20 15:24:27
合計ジャッジ時間 4,748 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 36 ms
6,208 KB
testcase_04 AC 37 ms
6,120 KB
testcase_05 AC 36 ms
6,508 KB
testcase_06 AC 36 ms
5,892 KB
testcase_07 AC 37 ms
6,632 KB
testcase_08 AC 36 ms
6,312 KB
testcase_09 AC 36 ms
6,420 KB
testcase_10 AC 36 ms
6,184 KB
testcase_11 AC 37 ms
6,404 KB
testcase_12 AC 43 ms
5,112 KB
testcase_13 AC 43 ms
4,972 KB
testcase_14 AC 43 ms
5,280 KB
testcase_15 AC 43 ms
5,028 KB
testcase_16 AC 44 ms
5,160 KB
testcase_17 AC 44 ms
5,024 KB
testcase_18 AC 33 ms
6,144 KB
testcase_19 AC 33 ms
6,224 KB
testcase_20 AC 33 ms
6,172 KB
testcase_21 AC 32 ms
6,140 KB
testcase_22 AC 33 ms
6,108 KB
testcase_23 AC 29 ms
4,376 KB
testcase_24 AC 30 ms
4,380 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 39 ms
5,340 KB
testcase_28 AC 39 ms
5,412 KB
testcase_29 AC 39 ms
5,380 KB
testcase_30 AC 36 ms
4,380 KB
testcase_31 AC 37 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.06.05 00:14:07
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

int ans;
struct DAG {
private:
    struct Edge {
        int to;
    };
    std::vector<std::vector<Edge>> graph;
    bool is_dag = false;
    std::vector<int> sorted; 
    int V; 
public:
    DAG(int v) {
        assert(v > 0);
        V = v;
        graph.resize(v);
    }
    
    void add_edge(int from, int to) {
        graph[from].push_back({to});
    }
    
    
    std::vector<int> topological_sort() {
        std::stack<int> sta;
        std::vector<int> dist(V, 0);
        std::vector<int> in(V, 0);
        int used_cnt = 0;
        for (int i = 0; i < V; i++) {
            for (Edge e : graph[i]) {
                in[e.to]++;
            }
        }
        for (int i = 0; i < V; i++) if (in[i] == 0) {
            sta.push(i);
            used_cnt++;
        }
        while (!sta.empty()) {
            int p = sta.top(); sta.pop();
            sorted.push_back(p);
            for (Edge e : graph[p]) {
                int v = e.to;
                in[v]--;
                dist[v] = std::max(dist[v], dist[p] + 1);
                ans = max(ans,dist[v]);
                if (in[v] == 0) {
                    sta.push(v);
                    used_cnt++;
                }
            }
        }
        if (used_cnt == V) {
            return sorted;
        }
        else {
            return std::vector<int>(0);
        }
    }
    vector<Edge>& operator[](int x) {
        return graph[x];
    }
};



int f(vector<int> a, vector<int> b) {
    do {
        if (a[0] > b[0] && a[1] > b[1] && a[2] > b[2]) {
            return 2;
        }
        else if (a[0] < b[0] && a[1] < b[1] && a[2] < b[2]) {
            return 1;
        }
    } while(next_permutation(a.begin(), a.end()));
    return 0;
}
int main() {
    int n;cin >> n;
    vector<vector<int>> a(n,vector<int>(3));
    for (int i = 0; i < n; i++) {
        cin >> a[i][0] >> a[i][1] >> a[i][2];
        sort(a[i].begin(), a[i].end());
    }
    DAG g(n);
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            int t = f(a[i],a[j]);
            
            if (t == 1) {
                g.add_edge(i,j);
            } 
            else if (t == 2) {
                g.add_edge(j,i);
            }
        }
    }
    g.topological_sort();
    cout << ans + 1 << endl;
    return 0;
}
0