Submission #301461

#TimeUsernameProblemLanguageResultExecution timeMemory
301461mth1908Connecting Supertrees (IOI20_supertrees)C++17
100 / 100
263 ms22392 KiB
#include <bits/stdc++.h>
#include "supertrees.h"

using namespace std;

#define ll long long
#define ull unsigned long long
#define ar array
#define pii pair<int, int>
#define sz(s) (int) s.size()
#define all(s) s.begin(), s.end()

int construct(vector<vector<int>> p) {
    int n = sz(p);
    vector<vector<int>> comp1(n), comp2(n);
    vector<vector<int>> b(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 3) return 0;
        }
    }
    vector<int> par1(n), par2(n);
    for (int i = 0; i < n; i++) {
        par1[i] = par2[i] = i;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 1) {
                par1[i] = j;
                comp1[j].push_back(i);
                break;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        if (par1[i] != i) continue;
        for (int j = 0; j < n; j++) {
            if (par1[j] != j) continue;
            if (p[i][j] == 2 || i == j) {
                par2[i] = j;
                comp2[j].push_back(i);
                break;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        vector<int> comp = comp1[i];
        for (int j = 1; j < sz(comp); j++) {
            if (p[comp[j]] != p[comp[0]]) return 0;
            b[comp[j]][comp[0]] = 1;
            b[comp[0]][comp[j]] = 1;
        }
        comp = comp2[i];
        if (sz(comp) == 2) return 0;
        if (sz(comp) < 2) continue;
        for (int j = 0; j < sz(comp); j++) {
            for (int k = 0; k < j; k++) {
                if (p[comp[j]][comp[k]] != 2) return 0;
            }
        }
        for (int j = 1; j < sz(comp); j++) {
            b[comp[j]][comp[j - 1]] = 1;
            b[comp[j - 1]][comp[j]] = 1;
        }
        b[comp[0]][comp[sz(comp) - 1]] = 1;
        b[comp[sz(comp) - 1]][comp[0]] = 1;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 1 && par1[i] != par1[j]) return 0;
            if (p[i][j] == 2 && par2[par1[i]] != par2[par1[j]]) return 0;
        }
    }
    build(b);
    return 1;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...