OI XXIV - tur (Jiangly)

// https://szkopul.edu.pl/problemset/problem/kqBM3UKWL-qlFiXIOxPXL35m/site/?key=statement
#include <algorithm>
#include <iostream>
#include <vector>
int n;
std::vector<std::vector<bool>> g;
int cntScc, dfsClock;
std::vector<int> dfn, low, belong;
std::vector<int> stk;
std::vector<std::vector<int>> ans;
std::vector<std::vector<int>> h;
void tarjan(int u) {
    dfn[u] = low[u] = dfsClock++;
    stk.push_back(u);
    for (int v = 0; v < n; ++v) {
        if (g[u][v]) {
            if (dfn[v] == -1) {
                tarjan(v);
                low[u] = std::min(low[u], low[v]);
            } else if (belong[v] == -1) {
                low[u] = std::min(low[u], dfn[v]);
            }
        }
    }
    if (dfn[u] == low[u]) {
        int v;
        do {
            v = stk.back();
            belong[v] = cntScc;
            stk.pop_back();
        } while (v != u);
        ++cntScc;
    }
}
auto solve(std::vector<int> v) {
    if (int(v.size()) == 1) return v;
    std::vector<int> h;
    for (auto u : v)
        h.insert(std::find_if(h.begin(), h.end(), [&](int v) { return g[u][v]; }), u);
    auto it = std::find_if(h.begin(), h.end(), [&](int v) { return g[v][h[0]]; }) + 1;
    std::vector<int> c(h.begin(), it);
    while (it != h.end()) {
        auto r = it;
        while (std::find_if(c.begin(), c.end(), [&](int v) { return g[*r][v]; }) == c.end())
            ++r;
        auto p = c.begin();
        while (p + 1 != c.end() && !(g[*p][*it] && g[*r][*(p + 1)]))
            ++p;
        c.insert(p == c.end() ? c.begin() : p + 1, it, r + 1);
        it = r + 1;
    }
    return c;
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin >> n;
    g.assign(n, std::vector<bool>(n));
    dfn.assign(n, -1);
    low.resize(n);
    belong.assign(n, -1);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < i; ++j) {
            int x;
            std::cin >> x;
            g[i][j] = x ^ 1;
            g[j][i] = x;
        }
    }
    for (int i = 0; i < n; ++i)
        if (dfn[i] == -1) tarjan(i);
    h.resize(cntScc);
    ans.resize(n);
    for (int i = 0; i < n; ++i)
        h[belong[i]].push_back(i);
    for (int i = 0; i < cntScc; ++i) {
        h[i] = solve(h[i]);
        for (auto it = h[i].begin(); it != h[i].end(); ++it) {
            int u = *it;
            ans[u].insert(ans[u].end(), it, h[i].end());
            ans[u].insert(ans[u].end(), h[i].begin(), it);
            for (int j = i - 1; j >= 0; --j)
                ans[u].insert(ans[u].end(), h[j].begin(), h[j].end());
        }
    }
    for (int i = 0; i < n; ++i) {
        std::cout << ans[i].size();
        for (int u : ans[i])
            std::cout << " " << u + 1;
        std::cout << "\n";
    }
    return 0;
}