Submission #306329

#TimeUsernameProblemLanguageResultExecution timeMemory
306329mielloConnecting Supertrees (IOI20_supertrees)C++14
0 / 100
1100 ms26208 KiB
#include "supertrees.h"
#include <bits/stdc++.h>
 
using namespace std;
const int MXN = 1005;
 
vector<int> par_line(MXN, 0), par_comp(MXN, 0), line[MXN], cycle_vertex[MXN];
vector<bool> check(MXN, false);
std::vector<std::vector<int>> answer;
int temp[MXN][MXN], n;

int root(int i, vector<int> &par) {
	while(i != par[par[i]]) {
		par[i] = par[par[i]];
		i = par[par[i]];
	}
	return i;
}
 
void mergeset(int a, int b, vector<int> &par) {
	par[root(a, par)] = root(b, par);
}

void dfs(int u, int b, int prev) {
	for(int i = 0; i < n; i++) {
		if(i == u || i == prev || i == b) {
			continue;
		}
		if(answer[u][i]) {
			if(!check[i]) {
				check[i] = true;
				temp[b][i]++;
				dfs(i, b, u);
				check[i] = false;
			}
		}
	}
}

int construct(std::vector<std::vector<int>> p) {
	n = p.size();
	for (int i = 0; i < n; i++) {
		std::vector<int> row;
		row.resize(n);
		answer.push_back(row);
		par_line[i] = i;
		par_comp[i] = i;
	}
	for(int i = 0; i < n; i++) {
		bool cycle_data = false, found_comp = false;
		for(int j = 0; j < n; j++) {
			if(i == j) continue;
			if(p[i][j] != 0) {
				found_comp = true;
				if(p[i][j] == 2) {
					cycle_data = true;
					if(root(i, par_comp) != root(j, par_comp)) {
						mergeset(i, j, par_comp);
					}
					continue;
				}
				if(root(i, par_line) != root(j, par_line)) {
					mergeset(i, j, par_line);
				}
			}
		}
		if(!cycle_data) {
			par_comp[i] = -1;
		}
		if(!found_comp) {
			par_line[i] = -1;
		}
	}
	for(int i = 0; i < n; i++) {
		if(par_line[i] != -1) {
			line[root(i, par_line)].push_back(i);
		}
	}
	for(int i = 0; i < n; i++) {
		int sz = line[i].size();
		if(sz) {
			if(par_comp[line[i][0]] != -1){
				cycle_vertex[root(line[i][0], par_comp)].push_back(line[i][0]);
			}
		}
		for(int j = 0; j < sz - 1; j++) {
			int x = line[i][j], y = line[i][j + 1];
			answer[x][y] = answer[y][x] = 1;
		}
	}
	for(int i = 0; i < n; i++) {
		int sz = cycle_vertex[i].size();
		if(sz >= 3) {
			cycle_vertex[i].push_back(cycle_vertex[i][0]);
			for(int j = 0; j < sz; j++) {
				int x = cycle_vertex[i][j], y = cycle_vertex[i][j + 1];
				answer[x][y] = answer[y][x] = 1;
			}
		} else if(sz > 0) {
			return 0;
		}
	}
	for(int i = 0; i < n; i++) {
		dfs(i, i, -1);
		temp[i][i] = 1;
	}
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			// printf("%d ", temp[i][j]);
			if(temp[i][j] != p[i][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...