Submission #800102

#TimeUsernameProblemLanguageResultExecution timeMemory
800102becaidoConnecting Supertrees (IOI20_supertrees)C++17
100 / 100
166 ms24132 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "supertrees.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

#ifdef WAIMAI
void build(vector<vector<int>> b);
#endif

const int SIZE = 1005;

int n, mx;
int g[SIZE];
bool vs[SIZE];
vector<int> all[SIZE];
vector<vector<int>> ans;

int construct(vector<vector<int>> p) {
	n = p.size();
	mx = 0;
	ans.clear();
	FOR (i, 0, n - 1) {
        g[i] = -1;
        vs[i] = 0;
        all[i].clear();
        ans.pb(vector<int>(n));
        mx = max(mx, *max_element(p[i].begin(), p[i].end()));
	}
	if (mx == 3) return 0;
	FOR (i, 0, n - 1) if (!vs[i]) {
        vector<int> v;
        queue<int> q;
        vs[i] = 1, q.push(i);
        while (q.size()) {
            int pos = q.front();
            q.pop();
            v.pb(pos);
            FOR (np, 0, n - 1) if (p[pos][np] == 1 && !vs[np]) {
                q.push(np);
                vs[np] = 1;
            }
        }
        for (int x : v) for (int y : v) if (p[x][y] != 1) return 0;
        FOR (j, 1, v.size() - 1) ans[v[j]][v[j - 1]] = ans[v[j - 1]][v[j]] = 1;
        for (int x : v) {
            all[v[0]].pb(x);
            g[x] = v[0];
        }
	}
	fill(vs, vs + n, 0);
	FOR (i, 0, n - 1) if (!vs[g[i]]) {
        vector<int> v;
        queue<int> q;
        vs[g[i]] = 1, q.push(g[i]);
        while (q.size()) {
            int pos = q.front();
            q.pop();
            v.pb(pos);
            for (int x : all[pos]) {
                FOR (y, 0, n - 1) if (p[x][y] == 2 && !vs[g[y]]) {
                    int np = g[y];
                    vs[np] = 1;
                    q.push(np);
                }
            }
        }
        if (v.size() == 1) continue;
        if (v.size() == 2) return 0;
        for (int x : v) for (int y : v) if (x != y) {
            for (int xi : all[x]) for (int yi : all[y]) {
                if (p[xi][yi] != 2) return 0;
            }
        }
        FOR (j, 1, v.size() - 1) ans[v[j]][v[j - 1]] = ans[v[j - 1]][v[j]] = 1;
        ans[v[0]][v.back()] = ans[v.back()][v[0]] = 1;
	}
	build(ans);
	return 1;
}

/*
in1
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
out1
1
0 1 0 0
1 0 1 1
0 1 0 1
0 1 1 0

in2
2
1 0
0 1
out2
1
0 0
0 0

in3
2
1 3
3 1
out3
0
*/

#ifdef WAIMAI
static int nn;
static vector<vector<int>> pp;
static vector<vector<int>> bb;
static bool called = false;

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

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

int main() {
    assert(scanf("%d", &nn) == 1);

    pp.resize(nn);
    for (int i = 0; i < nn; i++) {
        pp[i].resize(nn);
    }

    for (int i = 0; i < nn; i++) {
        for (int j = 0; j < nn; j++) {
            assert(scanf("%d", &pp[i][j]) == 1);
        }
    }
    fclose(stdin);

    int possible = construct(pp);

    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 < nn; i++) {
            for (int j = 0; j < nn; j++) {
                if (j) {
                    printf(" ");
                }
                printf("%d", bb[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...