Submission #300471

#TimeUsernameProblemLanguageResultExecution timeMemory
300471imeimi2000슈퍼트리 잇기 (IOI20_supertrees)C++17
100 / 100
278 ms26232 KiB
#ifdef imeimi

#include <vector>

int construct(std::vector<std::vector<int>> p);
void build(std::vector<std::vector<int>> b);

#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>
#define n N

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);
        }
    }
    fclose(stdin);

    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");
        }
    }
    fclose(stdout);
}

#undef n
#else

#include "supertrees.h"

#endif

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

static vector<int> lst;
static int n, edge[1000][1000];
static bool vis[1000];
static int idx[1000];
static void dfs(int x, int c) {
    vis[x] = 1;
    lst.push_back(x);
    for (int i = 0; i < n; ++i) {
        if (!edge[x][i] || edge[x][i] > c || vis[i]) continue;
        dfs(i, c);
    }
}

int construct(vector<vector<int>> p) {
	n = p.size();
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
        if (p[i][j] == 3) return 0;
        edge[i][j] = p[i][j];
    }
	vector<vector<int>> ans(n, vector<int>(n, 0));
    for (int i = 0; i < n; ++i) {
        if (vis[i]) continue;
        dfs(i, 1);
        for (int j : lst) for (int k : lst) if (edge[j][k] != 1) return 0;
        for (int j : lst) idx[j] = lst[0];
        for (int j = 1; j < int(lst.size()); ++j) {
            int x = lst[j - 1], y = lst[j];
            ans[x][y] = ans[y][x] = 1;
        }
        lst.clear();
    }
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < n; ++i) {
        if (vis[i]) continue;
        dfs(i, 2);
        for (int j : lst) for (int k : lst) if (!edge[j][k]) return 0;
        vector<int> nxt;
        for (int j : lst) if (idx[j] == j) nxt.push_back(j);
        lst.clear();
        if (int(nxt.size()) == 2) return 0;
        if (int(nxt.size()) <= 1) continue;
        for (int j = 0; j < int(nxt.size()); ++j) {
            int x = nxt[j], y = nxt[(j + 1) % int(nxt.size())];
            ans[x][y] = ans[y][x] = 1;
        }
    }
    build(ans);
    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...