Submission #603733

#TimeUsernameProblemLanguageResultExecution timeMemory
603733TigryonochekkConnecting Supertrees (IOI20_supertrees)C++17
40 / 100
395 ms77072 KiB
#include <iostream>
#include "supertrees.h"
#include <vector>
#include <set>
using namespace std;
const int N = 1002;

vector<vector<int>> p;
vector<vector<int>> answer;
set<int> dsu[N];
vector<int> ds[N];
int com[N];

void dfs(int v) {
	for (auto to : dsu[v]) {
		if (com[to]) continue;
		com[to] = com[v];
		dfs(to);
	}
}

void connect(int a, int b) {
	if (a == b) return;
	answer[a][b] = 1;
	answer[b][a] = 1;
}

void solve(vector<int> v) {
	int n = v.size();
	vector<bool> f(n);
	fill(f.begin(), f.end(), true);
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if (p[i][j] == 1) {
				f[i] = 0;
				f[j] = 0;
			}
		}
	}
	vector<int> u(n);
	int first = -1, last = -1;
	for (int i = 0; i < n; i++) {
		if (f[i]) {
			if (first < 0) {
				first = i;
				last = i;
			}
			connect(last, i);
			last = i;
			u[i] = 1;
		}
	}
	for (int i = 0; i < n; i++) {
		if (!f[i] && !u[i]) {
			if (first < 0) {
				first = i;
				last = i;
			}
			connect(last, i);
			last = i;
			u[i] = 1;
			for (int j = 0; j < n; j++) {
				if (j == i) continue;
				if (p[i][j] == 1) {
					connect(i, j);
					u[j] = 1;
				}
			}
		}
	}
	connect(last, first);
}

int construct(vector<vector<int>> pp) {
	p = pp;
	int n = p.size();
	answer.resize(n);
	for (int i = 0; i < n; i++) {
		answer[i].resize(n);
	}
	bool no1 = true, no2 = true;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (j == i) continue;
			if (p[i][j]) {
				dsu[i].insert(j);
				ds[i].push_back(j);
			}
			if (p[i][j] == 2)
				no2 = false;
			if (p[i][j] == 1)
				no1 = false;
		}
	}
	int k = 1;
	for (int i = 0; i < n; i++) {
		if (!com[i]) {
			com[i] = k++;
			dfs(i);
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (j == i) continue;
			if (com[i] == com[j] && dsu[i].find(j) == dsu[i].end()) return 0;
			if (com[i] != com[j] && dsu[i].find(j) != dsu[i].end()) return 0;
		}
	}
	if(no2)
	for (int i = 0; i < n; i++) {
		if (com[i]) {
			com[i] = 0;
			for (int j : dsu[i]) {
				answer[i][j] = 1;
				answer[j][i] = 1;
				com[j] = 0;
			}
		}
	}
	else if(no1) {
		for (int i = 0; i < n; i++) {
			if (com[i]) {
				if (ds[i].size() == 1) return 0;
				if (ds[i].size() == 0) continue;
				//cout << ds[i].size() << " " << dsu[i].size() << endl;
				answer[i][ds[i][0]] = 1;
				answer[ds[i][0]][i] = 1;
				answer[i][ds[i].back()] = 1;
				answer[ds[i].back()][i] = 1;
				com[i] = 0;
				for (int j = 0; j < ds[i].size() - 1; j++) {
					answer[ds[i][j]][ds[i][j + 1]] = 1;
					answer[ds[i][j + 1]][ds[i][j]] = 1;
					com[ds[i][j]] = 0;
				}
				com[ds[i].back()] = 0;
			}
		}
	}
	else {
		for (int i = 0; i < n; i++) {
			if (com[i]) {
				if (ds[i].size() == 0) continue;
				vector<int> c;
				c.push_back(i);
				com[i] = 0;
				for (int j : ds[i]) {
					c.push_back(j);
					com[j] = 0;
				}
				solve(c);
			}
		}
	}
	build(answer);
	return 1;
}

/*
3 
1 0 0 
0 1 1 
0 1 1 

4 
1 0 0 0 
0 1 2 2 
0 2 1 2 
0 2 2 1 

3 
1 0 0 
0 1 2 
0 2 1 

4 
1 1 2 2 
1 1 2 2 
2 2 1 2 
2 2 2 1 
*/

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:131:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |     for (int j = 0; j < ds[i].size() - 1; j++) {
      |                     ~~^~~~~~~~~~~~~~~~~~
#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...