Submission #480932

#TimeUsernameProblemLanguageResultExecution timeMemory
480932PiejanVDC슈퍼트리 잇기 (IOI20_supertrees)C++17
11 / 100
198 ms24000 KiB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

void build(std::vector<std::vector<int>> _b);

vector<int>parent;

int UF(int u) {
    if(parent[u] == u)
        return u;
    return parent[u] = UF(parent[u]);
}

vector<int>cycle;

int Cyc(int u) {
    if(u == cycle[u])
        return u;
    return cycle[u] = Cyc(cycle[u]);
}

int construct(vector<vector<int>> p) {
    const int n = (int)p.size();
    parent.resize(n);
    cycle.resize(n);
    vector<vector<int>>ans(n,vector<int>(n,0));
    for(int i = 0 ; i < n ; i++)
        parent[i] = i, cycle[i] = i;
    for(int i = 0 ; i < n ; i++) {
        for(int j = i+1 ; j < n ; j++) {
            if(p[i][j] == 3) {
                return 0; // invalid
            }
            assert(p[i][j] == p[j][i]);
            if(p[i][j] == 1) {
                int u = UF(i), v = UF(j);
                if(u != v) {
                    ans[i][j] = ans[j][i] = 1;
                    parent[u] = v;
                }
            }
        }
    }
    for(int i = 0 ; i < n ; i++) { // additional test
        for(int j = i+1 ; j < n ; j++) {
            if(p[i][j] == 2) {
                if(UF(i) == UF(j)) {
                    return 0;
                }
            }
        }
    }
    vector<pair<int,int>>d(n);
    vector<bool>closed(n,false);
    for(int i = 0 ; i < n ; i++) {
        d[i] = {i,i};
    }
    for(int i = 0 ; i < n ; i++) {
        for(int j = i+1 ; j < n ; j++) {
            if(p[i][j] == 2) {
                if(closed[i] || closed[j]) {
                    if(Cyc(i) != Cyc(j))
                        return 0;
                }
                int left = d[i].first, right = d[i].second;
                int lleft = d[j].first, rright = d[j].second;
                if(i == left && j == rright) {
                    ans[left][rright] = 1;
                    ans[rright][left] = 1;
                    for(int x = 0 ; x < n ; x++) {
                        if((d[x].first && d[x].second == right) || (d[x].first == lleft && d[x].second == rright))
                            closed[x]=true;
                    }
                    continue;
                } else if(i == lleft && j == right) {
                    ans[lleft][right] = 1;
                    ans[right][lleft] = 1;
                    for(int x = 0 ; x < n ; x++) {
                        if((d[x].first && d[x].second == right) || (d[x].first == lleft && d[x].second == rright))
                            closed[x]=true;
                    }
                    continue;
                }
                if(left == lleft && right == rright)
                    continue;
                ans[right][lleft] = 1;
                ans[lleft][right] = 1;
                for(int k = 0 ; k < n ; k++) {
                    if((d[k].first == left && d[k].second == right) || (d[k].first == lleft && d[k].second == rright)) {
                        d[k].first = left, d[k].second = rright;
                    }
                }
            }
        }
    }
    build(ans);
    return 1;
}

/*

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());
        fclose(stdout);
        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 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");
        }
    }
}
*/
#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...