Submission #550220

#TimeUsernameProblemLanguageResultExecution timeMemory
550220jesus_coconutConnecting Supertrees (IOI20_supertrees)C++17
100 / 100
253 ms28044 KiB
#include "supertrees.h"
#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
using namespace std;

int n;
struct DSU {
	vector<int> par, rnk;

	DSU (int n) {
		par.resize(n);
		rnk.resize(n);
		iota(all(par), 0);
	}

	int fnd(int x) { return (par[x] == x ? x : (par[x] = fnd(par[x]))); }

	bool unite(int x, int y) {
		x = fnd(x), y = fnd(y);
		if (x == y) return false;
		if (rnk[x] < rnk[y]) swap(x, y);
		if (rnk[x] == rnk[y]) rnk[x]++;
		par[y] = x;
		return true;
	}
};

vector<vector<int>> b, comp, p;

void spoji(int x, int y) {
	b[x][y] = 1;
	b[y][x] = 1;
}

int solve(vector<int> const &v) {
	vector<vector<int>> lines;
	vector<int> bio(size(v));
	for (int i = 0; i < size(v); ++i) {
		if (!bio[i]) {
			bio[i] = 1;
			lines.emplace_back();
			lines.back().push_back(v[i]);
			for (int j = i + 1; j < size(v); ++j) {
				if (p[v[i]][v[j]] == 1) {
					bio[j] = 1;
					spoji(v[i], v[j]);
					lines.back().push_back(v[j]);
				}
			}
			for (int j = 0; j < size(lines.back()); ++j) {
				for (int k = j + 1; k < size(lines.back()); ++k) {
					if (p[lines.back()[j]][lines.back()[k]] != 1) return 0;
				}
			}
		}
	}
	if (lines.size() == 1) return 1;
	if (lines.size() == 2) return 0;
	for (int i = 0; i < size(lines); ++i) {
		for (auto ver : lines[i]) {
			for (int j = 0; j < size(lines); ++j) {
				if (i == j) continue;
				for (auto uer : lines[j]) {
					if (p[ver][uer] != 2) {
						return 0;
					}
				}
			}
		}
	}
	for (int i = 0; i < size(lines); ++i) {
		spoji(lines[i][0], lines[(i + 1) % size(lines)][0]);
	}
	return 1;
}

int construct(std::vector<std::vector<int>> p) {
	::p = p;
	n = size(p);
	b.resize(n, vector<int>(n));
	comp.resize(n);
	DSU subtree(n), cyc(n);

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (p[i][j] == 3 || p[i][j] != p[j][i]) return 0;
			if (p[i][j] == 0) continue;
			if (i == j) {
				if (p[i][j] != 1) return 0;
				else continue;
			}
			subtree.unite(i, j);
			if (p[i][j] == 2) cyc.unite(i, j);
		}
	}
	for (int i = 0; i < n; ++i) comp[subtree.fnd(i)].push_back(i);
	for (int i = 0; i < n; ++i) {
		for (auto v : comp[i]) {
			for (int j = i + 1; j < n; ++j) {
				for (auto u : comp[j]) {
					if (p[v][u] != 0) return 0;
				}
			}
		}
	}
	for (auto &v : comp) {
		if (size(v) <= 1) continue;
		if (!solve(v)) return 0;
	}
	//
	build(b);
	return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'int solve(const std::vector<int>&)':
supertrees.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |  for (int i = 0; i < size(v); ++i) {
      |                  ~~^~~~~~~~~
supertrees.cpp:43:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |    for (int j = i + 1; j < size(v); ++j) {
      |                        ~~^~~~~~~~~
supertrees.cpp:50:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |    for (int j = 0; j < size(lines.back()); ++j) {
      |                    ~~^~~~~~~~~~~~~~~~~~~~
supertrees.cpp:51:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for (int k = j + 1; k < size(lines.back()); ++k) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~
supertrees.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for (int i = 0; i < size(lines); ++i) {
      |                  ~~^~~~~~~~~~~~~
supertrees.cpp:61:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |    for (int j = 0; j < size(lines); ++j) {
      |                    ~~^~~~~~~~~~~~~
supertrees.cpp:71:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for (int i = 0; i < size(lines); ++i) {
      |                  ~~^~~~~~~~~~~~~
#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...