結果

問題 No.497 入れ子の箱
ユーザー 🍮かんプリン
提出日時 2020-06-05 00:14:17
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 40 ms / 5,000 ms
コード長 2,468 bytes
コンパイル時間 1,977 ms
コンパイル使用メモリ 176,288 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-11-30 18:24:04
合計ジャッジ時間 3,917 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

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