Submission #469075

# Submission time Handle Problem Language Result Execution time Memory
469075 2021-08-30T15:56:28 Z hoanghq2004 Connecting Supertrees (IOI20_supertrees) C++14
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
#include "supertrees.h"

using namespace std;

const int Nmax = 1e3 + 10;

static int n;
static std::vector<std::vector<int>> p;
static std::vector<std::vector<int>> b;
static bool called = false;

static void check(bool cond, std::string message) {
    if (!cond) {
        printf("%s\n", message.c_str());
        exit(0);
    }
}

void build(std::vector<std::vector<int>> _b) {
    check(!called, "build is called more than once");
    called = true;
    check((int)_b.size() == n, "Invalid number of rows in b");
    for (int i = 0; i < n; i++) {
        check((int)_b[i].size() == n, "Invalid number of columns in b");
    }
    b = _b;
}

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 vis[Nmax];

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;
        if (rt[i].size() == 2) return 0;
//        for (auto u: rt[i]) vis[u] = 1;
        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 || rt[i].size() == 3) return 0;
//        for (auto u: rt[i]) vis[u] = 1;
        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;
    }
//    for (int i = 0; i < n; ++i) if (i == Find(0, i) && !vis[i]) return 0;
    build(b);
    return 1;
}

//
//int main() {
//    assert(scanf("%d", &n) == 1);
//
//    p.resize(n);
//    for (int i = 0; i < n; i++) {
//        p[i].resize(n);
//    }
//
//    for (int i = 0; i < n; i++) {
//        for (int j = 0; j < n; j++) {
//            assert(scanf("%d", &p[i][j]) == 1);
//        }
//    }
//
//    int possible = construct(p);
//
//    check(possible == 0 || possible == 1, "Invalid return value of construct");
//    if (possible == 1) {
//        check(called, "construct returned 1 without calling build");
//    } else {
//        check(!called, "construct called build but returned 0");
//    }
//
//    printf("%d\n", possible);
//    if (possible == 1) {
//        for (int i = 0; i < n; i++) {
//            for (int j = 0; j < n; j++) {
//                if (j) {
//                    printf(" ");
//                }
//                printf("%d", b[i][j]);
//            }
//            printf("\n");
//        }
//    }
//}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:82:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         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:93:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         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;
      |                         ~~^~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccyTD35a.o: in function `build(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)':
grader.cpp:(.text+0x320): multiple definition of `build(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)'; /tmp/ccBIZhKa.o:supertrees.cpp:(.text+0x3e0): first defined here
collect2: error: ld returned 1 exit status