Submission #388464

#TimeUsernameProblemLanguageResultExecution timeMemory
388464alexxela12345Connecting Supertrees (IOI20_supertrees)C++17
21 / 100
260 ms22144 KiB
#include "supertrees.h"
#include <vector>
#include <numeric>

using namespace std;

const int MAXN = 1e3;

int pa[MAXN];
int ra[MAXN];

void init() {
    iota(pa, pa + MAXN, 0);
    fill(ra, ra + MAXN, 0);
}

int get(int a) {
    if (a == pa[a])
        return a;
    return pa[a] = get(pa[a]);
}

void merge(int a, int b) {
    a = get(a);
    b = get(b);
    if (a == b)
        return;
    if (ra[b] > ra[a])
        swap(a, b);
    pa[b] = a;
    if (ra[a] == ra[b])
        ra[a]++;
}


int construct(vector<vector<int>> p) {
    init();
	int n = p.size();
	vector<vector<int>> answer;
	for (int i = 0; i < n; i++) {
		vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 1) {
                if (get(i) != get(j)) {
                    merge(i, j);
                    answer[i][j] = 1;
                    answer[j][i] = 1;
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 0) {
                if (get(i) == get(j)) {
                    return 0;
                }
            }
        }
    }
	build(answer);
	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...