Submission #1085296

#TimeUsernameProblemLanguageResultExecution timeMemory
1085296an22inkleConnecting Supertrees (IOI20_supertrees)C++17
46 / 100
143 ms24400 KiB
#include<bits/stdc++.h>
#include "supertrees.h"
using pair = std::array<int, 2>;
using ll = long long;

void adjlist(std::vector<std::vector<int>> p) {
	int n = p.size();
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if (p[i][j]) std::cout << i << ' ' << j << '\n';
		}
	}
	std::cout << '\n';
}

int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	std::vector<std::vector<int>> ans(n, std::vector<int>(n)), groups;
	std::vector<int> part(n, -1);

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i == j || p[i][j] == 0) continue;
	
			if (part[i] > -1 && part[j] > -1) {
				if (part[i] != part[j]) return 0;
				continue;
			}

			if (part[i] > -1) groups[part[i]].push_back(j), part[j] = part[i];
			else if (part[j] > -1) groups[part[j]].push_back(i), part[i] = part[j];
			else {
				groups.push_back({i, j});
				// groups.back().push_back(i);
				// groups.back().push_back(j);
				part[i] = part[j] = groups.size() - 1;
			}
		}
	}

	for (auto group : groups) {
		int type = 0;
		int m = group.size();
		for (int i = 0; i < m; i++) {
			for (int j = i+1; j < m; j++) {
				type = std::max(type, p[group[i]][group[j]]);
			}
		}

		for (int i = 0; i < m && type == 1; i++) {
			if (i != m - 1) ans[group[i]][group[(i+1)]] = ans[group[(i + 1)]][group[i]] = 1;	
		}

		if (type == 1) continue;

		std::vector<int> o = {group[0]}, vis(m); vis[0] = 1;
		// Add elements that require two paths to all present in o;
		for (int i = 1; i < m; i++) {
			int ok = 1;
			for (auto c : o) {
				if (p[group[i]][c] != 2) ok = 0; 
			}
			if (ok) o.push_back(group[i]), vis[i] = 1;
		}

		for (int i = 1; i <= o.size(); i++) {
			ans[o[i-1]][o[i%o.size()]] = ans[o[i%o.size()]][o[i-1]] = 1;
		}

		for (int i = 0; i < m; i++) {
			if (vis[i]) continue;
			for (auto c : o) {
				if (p[c][group[i]] == 1) {
					ans[c][group[i]] = ans[group[i]][c] = 1;
				}
			}
		}
	}

	// adjlist(ans);
	build(ans);
	return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:66:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |   for (int i = 1; i <= o.size(); 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...