| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 301412 | cuom1999 | Connecting Supertrees (IOI20_supertrees) | C++14 | 0 ms | 0 KiB | 
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
struct Solver {
    int n;
    vector<bool> vs;
    vector<int> lineIndex;
    vector<vector<int>> p, line, cycle;
    Solver(vector<vector<int>> p): p(p), n(p.size()) {
        vs.resize(n);
        lineIndex.resize(n);
        line.resize(n);
        cycle.resize(n);
    }
    void dfs1(int a, int c) {
        if (vs[a]) return;
        vs[a] = 1;
        lineIndex[a] = c;
        line[c].push_back(a);
        for (int i = 0; i < n; i++) {
            if (p[a][i] == 1) dfs1(i, c);
        }
    }
    void dfs2(int a, int c) {
        if (vs[a]) return;
        vs[a] = 1;
        cycle[c].push_back(a);
        for (int i = 0; i < n; i++) {
            if (p[a][i] == 2) dfs2(i, c);
        }
    }
    int solve() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (p[i][j] == 3) return 0;
            }
        }
        for (int i = 0; i < n; i++) {
            if (!vs[i]) dfs1(i, i);
        }
        for (int i = 0; i < n; i++) {
            if (lineIndex[i] == i) vs[i] = 0;
        }
        for (int i = 0; i < n; i++) {
            if (!vs[i]) dfs2(i, i);
        }
        // construct res
        vector<vector<int>> res(n, vector<int>(n));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j + 1 < line[i].size(); j++) {
                int x = line[i][j], y = line[i][j + 1];
                res[x][y] = res[y][x] = 1;
            }
            if (cycle[i].size()) cycle[i].push_back(cycle[i][0]);
            for (int j = 0; j + 1 < cycle[i].size(); j++) {
                int x = cycle[i][j], y = cycle[i][j + 1];
                res[x][y] = res[y][x] = 1;
            }
        }
        build(res);
        // for (int i = 0; i < n; i++) {
        //     cout << lineIndex[i] << " ";
        // }
        // cout << endl;
        // for (int i = 0; i < n; i++) {
        //     if (cycle[i].size()) {
        //         for (auto j: cycle[i]) cout << j << " ";
        //         cout << endl;
        //     }
        // }
        return 1;
    }
};
