Submission #365134

# Submission time Handle Problem Language Result Execution time Memory
365134 2021-02-10T23:14:06 Z Farrius Connecting Supertrees (IOI20_supertrees) C++14
0 / 100
1 ms 256 KB
#include "supertrees.h"
#include <vector>

using namespace std;

struct DSU {
	vector<int> e;
	void init (int n) {e = vector<int>(n, -1);}
	int get (int x) {return e[x] < 0 ? x : e[x] = get(e[x]);}
	bool sameSet (int x, int y) {return get(x) == get(y);}
	bool unite (int x, int y) {
		x = get(x), y = get(y);
		if (x == y) return false;
		if (e[x] > e[y]) swap(x, y);
		e[x] += e[y];
		e[y] = x;
		return true;
	}
};

int construct(vector<vector<int>> p) {
	//take input
	int n = p.size();
	vector<vector<int>> sol(n, vector<int>(n));
	DSU dsu;
	dsu.init(n);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (p[i][j] >= 1) {
				dsu.unite(i, j);
			}
		}
	}
	//comprovar si ok
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (p[i][j] == 0 && dsu.sameSet(i, j)) return 0;
		}
	}
	//crear graph con los componentes creados
	vector<int> g[n];
	for (int i = 0; i < n; i++) {
		int par = dsu.get(i);
		g[par].push_back(i);
	}
	for (int i = 0; i < n; i++) {
		int m = (int) g[i].size();
		if (m == 0) continue;
		vector<int> doble;
		vector<int> uno;
		for (int j = 0; j < m; j++) {
			for (int k = 0; k < m; k++) {
				if (j == k) continue;
				if (p[g[i][j]][g[i][k]] == 2) {
					doble.push_back(g[i][j]);
					doble.push_back(g[i][k]);
				} else {
					uno.push_back(g[i][j]);
					uno.push_back(g[i][k]);
				}
			}
		}
		//crear un cycle con los que valen dos
		for (int j = 0; j < (int) doble.size(); j++) {
			if (j == 0) sol[doble[j]][doble[(int) doble.size() -1]] = 1;
			else sol[doble[j - 1]][doble[j]] = 1;
		}
		//un camino directo directo entre los dos
		for (int j = 1; j < (int) uno.size(); j++) {
			sol[uno[j - 1]][uno[j]] = 1;
		}
	}
	//ajustar la sol
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (sol[i][j] == 1) sol[j][i] = 1;
		}
	}
	//output
	build(sol);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Incorrect 0 ms 256 KB b[1][1] is not 0
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Incorrect 0 ms 256 KB b[1][1] is not 0
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Incorrect 0 ms 256 KB Answer gives possible 1 while actual possible 0
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Incorrect 0 ms 256 KB b[2][2] is not 0
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Incorrect 0 ms 256 KB b[1][1] is not 0
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Incorrect 0 ms 256 KB b[1][1] is not 0
3 Halted 0 ms 0 KB -