Submission #823117

#TimeUsernameProblemLanguageResultExecution timeMemory
823117Sohsoh84Connecting Supertrees (IOI20_supertrees)C++17
19 / 100
161 ms22064 KiB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5 + 10;

#define sep		' '
#define debug(x)	cerr << #x << ": " << x << endl;

int f[MAXN], sz[MAXN], L[MAXN], f2[MAXN];

vector<vector<int>> answer;

inline void add(int i, int j) {
	answer[i][j] = answer[j][i] = 1;
}

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

	vector<int> roots;
	for (int i = 0; i < n; i++) {
		f[i] = i;
		int lst = i;
		for (int j = 0; j < i; j++)
			if (p[i][j] == 1)
				lst = j;

		if (lst != i) add(lst, i), f[i] = f[lst];
		else roots.push_back(i);
	}

	int m = roots.size();
	for (int i = 0; i < m; i++) {
		f2[i] = i;	

		int lst = i;
		for (int j = 0; j < i; j++)
			if (p[roots[i]][roots[j]] == 2)
				lst = j;

		if (lst != i) {
			add(roots[i], roots[lst]);
			f2[i] = f2[lst];
			L[f2[i]] = i;
		} else L[i] = i;

		sz[f2[i]]++;
	}

	for (int i = 0; i < m; i++) {
		if (L[i] && L[i] != i) {
			add(roots[i], roots[L[i]]);
		}

		if (sz[i] == 2) return 0;
	}

	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if (f[i] == f[j] && p[i][j] != 1) return 0;
			else if (f2[f[i]] == f2[f[j]] && p[i][j] != 2) return 0;
			if (f2[f[i]] != f2[f[j]] && p[i][j] != 0) 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...