Submission #304863

#TimeUsernameProblemLanguageResultExecution timeMemory
304863daveeedConnecting Supertrees (IOI20_supertrees)C++17
21 / 100
260 ms22320 KiB
#include "supertrees.h"
#include <vector>

const int MAXN = 1000;

int parent[MAXN], sz[MAXN];

int find(int p){
    while (p != parent[p])
        p = parent[p];
    return p;
}

void merge(int p, int q) {
    int rootP = find(p);
    int rootQ = find(q);
    if (rootP == rootQ) return;
    if (sz[rootP] < sz[rootQ]) {
        parent[rootP] = rootQ;
        sz[rootQ] += sz[rootP];
    }
    else {
        parent[rootQ] = rootP;
        sz[rootP] += sz[rootQ];
    }
}

int construct(std::vector<std::vector<int> > p) {
	int n = p.size();
	std::vector<std::vector<int> > answer;
	for (int i = 0; i < n; i++) {
		std::vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}

    for(int i = 0; i < n; i++){
        parent[i] = i;
    }

    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++){
            if(p[i][j])
                merge(i, j);
        }
    }

    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++){
            if(find(i) == find(j) && !p[i][j])
                return 0;
        }
    }

    for(int i = 0; i < n; i++){
        int r = find(i);
        if(r != i){
            answer[r][i] = 1;
            answer[i][r] = 1;
        }
    }

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