Submission #401041

#TimeUsernameProblemLanguageResultExecution timeMemory
401041dxz05Connecting Supertrees (IOI20_supertrees)C++14
0 / 100
1 ms204 KiB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1111;

int par[MAXN][4];

int find_set(int x, int flag){
    if (x == par[x][flag]) return x;
    return par[x][flag] = find_set(par[x][flag], flag);
}

bool union_sets(int x, int y, int flag){
    x = find_set(x, flag);
    y = find_set(y, flag);
    if (x == y) return false;
    if (rand() % 2) swap(x, y);
    par[x][flag] = y;
    return true;
}

vector<int> children[MAXN];

bool check(vector<vector<int>> &answer, vector<vector<int>> &p){
    int n = answer.size();

    if (n == 2 && p[1][0] > 1) return false;

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            if (p[i][j] == 1){
                if (find_set(i, 1) != find_set(j, 1)) return false;
            }

            if (p[i][j] == 2){
                int x = find_set(i, 1), y = find_set(j, 1);
                if (find_set(x, 2) != find_set(y, 2)) return false;
            }

            if (answer[i][j] == 1) union_sets(i, j, 3);
        }
    }

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            bool a = (find_set(i, 3) == find_set(j, 3));
            if (a && p[i][j] != 0 || !a && p[i][j]) return false;
        }
    }

    return true;
}

int construct(vector<vector<int>> p) {
    int n = p.size();
    vector<vector<int>> answer(n, vector<int>(n, 0));

    for (int i = 0; i < n; i++) par[i][1] = par[i][2] = par[i][3] = i;

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            if (p[i][j] == 1) union_sets(i, j, 1);
            if (p[i][j] == 3) return 0;
        }
    }

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            if (p[i][j] == 2){
                int x = find_set(i, 1), y = find_set(j, 1);
                union_sets(x, y, 2);
            }
        }
    }

    for (int i = 0; i < n; i++){
        children[find_set(i, 2)].push_back(i);
    }

    for (int i = 0; i < n; i++){
        if (children[i].size() < 2) continue;
        children[i].push_back(children[i].front());
        for (int j = 0; j < children[i].size() - 1; j++){
            int x = children[i][j], y = children[i][j + 1];
            answer[x][y] = answer[y][x] = 1;
        }
    }

    for (int i = 0; i < n; i++){
        int x = find_set(i, 1);
        if (x != i) answer[i][x] = answer[x][i] = 1;
    }

    if (!check(answer, p)) return 0;

    build(answer);
    return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'bool check(std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:49:19: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   49 |             if (a && p[i][j] != 0 || !a && p[i][j]) return false;
      |                 ~~^~~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:85:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for (int j = 0; j < children[i].size() - 1; j++){
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...