Submission #1297729

#TimeUsernameProblemLanguageResultExecution timeMemory
1297729khoavn2008Connecting Supertrees (IOI20_supertrees)C++17
21 / 100
98 ms22032 KiB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define FOR(i,l,r) for(int i = (l), _r = (r); i <= _r; i++)
#define FORNG(i,r,l) for(int i = (r), _l = (l); i >= _l; i--)
#define REP(i,r) for(int i = 0, _r = (r); i < _r; i++)
#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define size(v) ((ll)(v).size())
#define all(v) (v).begin(),(v).end()
#define MASK(x) (1LL << (x))
#define BIT(x,i) (((x) >> (i)) & 1)
const ll N = 1e5 + 10, LOG = 18;
const ll INF = 1e18 + 36;
#ifdef KHOA
void build();
#endif


struct DSU{
    int n;
    vector<int> par,sz;
    vector<vector<int>> node;
    DSU(int n = 0):n(n){
        par.assign(n, -1);
        sz.assign(n, 1);
        node.resize(n);
        REP(i,n)node[i].pb(i);
    }
    int find(int u){return par[u] == -1 ? u : par[u] = find(par[u]);}
    bool merge(int u,int v){
        u = find(u), v = find(v);
        if(u == v)return 0;
        if(sz[u] < sz[v])swap(u, v);
        par[v] = u;
        sz[u] += sz[v];
        for(int x : node[v])node[u].pb(x);
        return 0;
    }
};
int construct(vector<vector<int>> p) {
	int n = size(p);
	DSU dsu(n);
	REP(i,n)REP(j,n)if(p[i][j])dsu.merge(i, j);

	REP(i,n){
        for(int j : dsu.node[dsu.find(i)]){
            if(!p[i][j] || !p[j][i])return 0;
        }
	}

    vector<vector<int>> ans(n, vector<int>(n, 0));
    vector<bool> mark(n, 0);
    REP(i,n)if(!mark[dsu.find(i)]){
        mark[dsu.find(i)] = 1;
        REP(j,size(dsu.node[dsu.find(i)]) - 1){
            ans[dsu.node[dsu.find(i)][j]][dsu.node[dsu.find(i)][j + 1]] = 1;
            ans[dsu.node[dsu.find(i)][j + 1]][dsu.node[dsu.find(i)][j]] = 1;
        }
    }
    build(ans);
	return 1;
}



#ifdef KHOA
#undef size
#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>

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);
}

#endif
#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...