Submission #800085

#TimeUsernameProblemLanguageResultExecution timeMemory
800085becaidoConnecting Supertrees (IOI20_supertrees)C++17
40 / 100
179 ms23948 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;
bool vs[SIZE];
vector<vector<int>> ans;

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