Submission #469071

#TimeUsernameProblemLanguageResultExecution timeMemory
469071hoanghq2004Connecting Supertrees (IOI20_supertrees)C++14
56 / 100
283 ms24196 KiB
#include <bits/stdc++.h>
#include "supertrees.h"

using namespace std;

const int Nmax = 1e3 + 10;

int n;
int b[Nmax][Nmax];
int p[Nmax][Nmax];
int root[3][Nmax], sz[3][Nmax];
vector <int> rt[Nmax];

int Find(int _, int u) {
    return (u == root[_][u] ? u : root[_][u] = Find(_, root[_][u]));
}

void Union(int _, int u, int v) {
    if ((u = Find(_, u)) == (v = Find(_, v))) return;
    if (sz[_][u] < sz[_][v]) swap(u, v);
    sz[_][u] += sz[_][v];
    root[_][v] = u;
}

int construct(vector <vector <int> > p) {
    int n = p.size();
    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] != p[j][i]) return 0;
    for (int i = 0; i < n; ++i) if (p[i][i] != 1) return 0;
    for (int i = 0; i < n; ++i) for (int j = 0; j < 3; ++j) root[j][i] = i, sz[j][i] = 1;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            if (p[i][j] == 1) Union(0, i, j);
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            if (Find(0, i) == Find(0, j) && p[i][j] != 1) return 0;

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

    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            if (p[i][j] == 3 && Find(1, Find(0, i)) == Find(1, Find(0, j))) return 0;
    for (int i = 0; i < n; ++i)
        if (i != Find(0, i)) {
//            cout << i << " " << Find(0, i) << "\n";
            b[i][Find(0, i)] = b[Find(0, i)][i] = 1;
        }
    for (int i = 0; i < n; ++i) {
        if (i == Find(0, i)) rt[Find(1, i)].push_back(i);
    }
    for (int i = 0; i < n; ++i) {
        if (rt[i].size() <= 1) continue;
        for (int j = 1; j < rt[i].size(); ++j) b[rt[i][j]][rt[i][j - 1]] = b[rt[i][j - 1]][rt[i][j]] = 1;
        b[rt[i].front()][rt[i].back()] = b[rt[i].back()][rt[i].front()] = 1;
    }
    for (int i = 0; i < n; ++i) rt[i].clear();
    for (int i = 0; i < n; ++i) {
        if (i == Find(0, i)) rt[Find(2, i)].push_back(i);
    }
    for (int i = 0; i < n; ++i) {
        if (rt[i].size() <= 1) continue;
        if (rt[i].size() == 2) return 0;
        for (int j = 1; j < rt[i].size(); ++j) b[rt[i][j]][rt[i][j - 1]] = b[rt[i][j - 1]][rt[i][j]] = 1;
        b[rt[i].front()][rt[i].back()] = b[rt[i].back()][rt[i].front()] = 1;
        b[rt[i][0]][rt[i][2]] = b[rt[i][2]][rt[i][0]] = 1;
    }
    build(b);
    return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:59:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         for (int j = 1; j < rt[i].size(); ++j) b[rt[i][j]][rt[i][j - 1]] = b[rt[i][j - 1]][rt[i][j]] = 1;
      |                         ~~^~~~~~~~~~~~~~
supertrees.cpp:69:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |         for (int j = 1; j < rt[i].size(); ++j) b[rt[i][j]][rt[i][j - 1]] = b[rt[i][j - 1]][rt[i][j]] = 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...