답안 #366934

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
366934 2021-02-15T18:45:37 Z BlancaHM 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
270 ms 22380 KB
#include <iostream>
#include <vector>
#include <unordered_map>
#include "supertrees.h"
using namespace std;

int raiz(int a, vector<int> & padre) {
    if (padre[a] == a) return a;
    return padre[a] = raiz(padre[a], padre);
}

void unirConjuntos(int a, int b, vector<int> & padre, vector<int> & alturaAprox, int & numeroConjuntos) {
    int raiz_a, raiz_b;
    raiz_a = raiz(a, padre);
    raiz_b = raiz(b, padre);
    if (raiz_a == raiz_b) return;
    numeroConjuntos--;
    if (alturaAprox[raiz_a] > alturaAprox[raiz_b]) {
        padre[raiz_b] = raiz_a;
    } else if (alturaAprox[raiz_b] > alturaAprox[raiz_a]) {
        padre[raiz_a] = raiz_b;
    } else {
        padre[raiz_a] = raiz_b;
        alturaAprox[raiz_b]++;
    }
}

int construct(vector<vector<int>> p) {
	int N = (int) p.size(), numCCs = N;
	vector<int> padre(N), alturaAprox(N, 0);

	for (int i = 0; i < N; i++)
		padre[i] = i;

	// encontramos las componentes conexas
	for (int i = 0; i < N; i++) {
		for (int j = i+1; j < N; j++) {
			if (p[i][j]) {
				if (p[i][j] == 3) {
					return 0;
				}
				unirConjuntos(i, j, padre, alturaAprox, numCCs);
			} else if (raiz(i, padre) == raiz(j, padre))
				return 0;
		}
	}

	// damos un índice a cada raíz
	unordered_map<int, int> raices;
	int c = 0;
	for (int i = 0; i < N; i++) {
		if (padre[i] == i)
			raices[i] = c++;
	}

	// guardamos las componentes conexas
	vector<vector<int>> CCs(numCCs);
	for (int i = 0; i < N; i++)
		CCs[raices[raiz(i, padre)]].push_back(i);

	int CCtamano, u, v, n, raiz_u, raiz_v;

	vector<vector<int>> resultadoFinal(N, vector<int>(N, 0)), serpientes;

	for (int i = 0; i < numCCs; i++) {
		CCtamano = (int) CCs[i].size();
		if (CCtamano == 1) continue;

		// encontramos las "serpientes" de esta componente conexa
		n = CCtamano;
		for (auto node: CCs[i]) {
			padre[node] = node;
			alturaAprox[node] = 0;
		}
		for (int j = 0; j < CCtamano; j++) {
			u = CCs[i][j];
			raiz_u = raiz(u, padre);
			for (int k = j+1; k < CCtamano; k++) {
				v = CCs[i][k];
				if (p[u][v] == 1)
					unirConjuntos(u, v, padre, alturaAprox, n);
				else if (raiz_u == raiz(v, padre))
					return 0;
			}
		}

		// nos aseguramos de que no hay incongruencias en esta componente conexa
		if (n == 2) return 0;

		raices.clear();
		c = 0;
		for (int j = 0; j < CCtamano; j++) {
			u = CCs[i][j];
			raiz_u = raiz(u, padre);
			if (raiz_u == u)
				raices[u] = c++;
			for (int k = j+1; k < CCtamano; k++) {
				v = CCs[i][k];
				raiz_v = raiz(v, padre);
				if (raiz_u != raiz_v) {
					if (p[u][v] != 2) {
						return 0;
					}
				} else if (p[u][v] != 1) {
					return 0;
				}
			}
		}

		// conectamos los elementos dentro de una serpiente
		serpientes = vector<vector<int>>(n);
		for (int j = 0; j < CCtamano; j++)
			serpientes[raices[raiz(CCs[i][j], padre)]].push_back(CCs[i][j]);
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < (int) serpientes[j].size() - 1; k++)
				resultadoFinal[serpientes[j][k]][serpientes[j][k+1]] = resultadoFinal[serpientes[j][k+1]][serpientes[j][k]] = 1;
		}

		// conectamos el ciclo de cabezas de serpiente
		for (int j = 0; j < n-1; j++)
			resultadoFinal[serpientes[j][0]][serpientes[j+1][0]] = resultadoFinal[serpientes[j+1][0]][serpientes[j][0]] = 1;

		if (n > 1)
			resultadoFinal[serpientes[0][0]][serpientes[n-1][0]] = resultadoFinal[serpientes[n-1][0]][serpientes[0][0]] = 1;
	}
	build(resultadoFinal);
	return 1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 257 ms 22252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 257 ms 22252 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 10 ms 1260 KB Output is correct
13 Correct 243 ms 22252 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 620 KB Output is correct
17 Correct 107 ms 8160 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 61 ms 5868 KB Output is correct
21 Correct 249 ms 22252 KB Output is correct
22 Correct 244 ms 22124 KB Output is correct
23 Correct 264 ms 22112 KB Output is correct
24 Correct 245 ms 22292 KB Output is correct
25 Correct 106 ms 8160 KB Output is correct
26 Correct 102 ms 8172 KB Output is correct
27 Correct 261 ms 22124 KB Output is correct
28 Correct 243 ms 22252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 10 ms 1260 KB Output is correct
9 Correct 258 ms 22252 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 14 ms 1260 KB Output is correct
13 Correct 250 ms 22280 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 620 KB Output is correct
17 Correct 107 ms 8172 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 62 ms 5868 KB Output is correct
22 Correct 252 ms 22252 KB Output is correct
23 Correct 244 ms 22124 KB Output is correct
24 Correct 257 ms 22124 KB Output is correct
25 Correct 106 ms 12268 KB Output is correct
26 Correct 104 ms 8172 KB Output is correct
27 Correct 248 ms 22380 KB Output is correct
28 Correct 270 ms 22112 KB Output is correct
29 Correct 108 ms 12268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 62 ms 5868 KB Output is correct
5 Correct 248 ms 22124 KB Output is correct
6 Correct 244 ms 22124 KB Output is correct
7 Correct 262 ms 22124 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 62 ms 5868 KB Output is correct
10 Correct 251 ms 22124 KB Output is correct
11 Correct 249 ms 22124 KB Output is correct
12 Correct 256 ms 22124 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 64 ms 5868 KB Output is correct
17 Correct 248 ms 22252 KB Output is correct
18 Correct 249 ms 22124 KB Output is correct
19 Correct 250 ms 22112 KB Output is correct
20 Correct 247 ms 22240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 257 ms 22252 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 10 ms 1260 KB Output is correct
13 Correct 243 ms 22252 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 620 KB Output is correct
17 Correct 107 ms 8160 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 61 ms 5868 KB Output is correct
21 Correct 249 ms 22252 KB Output is correct
22 Correct 244 ms 22124 KB Output is correct
23 Correct 264 ms 22112 KB Output is correct
24 Correct 245 ms 22292 KB Output is correct
25 Correct 106 ms 8160 KB Output is correct
26 Correct 102 ms 8172 KB Output is correct
27 Correct 261 ms 22124 KB Output is correct
28 Correct 243 ms 22252 KB Output is correct
29 Correct 0 ms 364 KB Output is correct
30 Correct 0 ms 364 KB Output is correct
31 Correct 0 ms 364 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 0 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 10 ms 1260 KB Output is correct
37 Correct 258 ms 22252 KB Output is correct
38 Correct 0 ms 364 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 14 ms 1260 KB Output is correct
41 Correct 250 ms 22280 KB Output is correct
42 Correct 0 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 620 KB Output is correct
45 Correct 107 ms 8172 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 62 ms 5868 KB Output is correct
50 Correct 252 ms 22252 KB Output is correct
51 Correct 244 ms 22124 KB Output is correct
52 Correct 257 ms 22124 KB Output is correct
53 Correct 106 ms 12268 KB Output is correct
54 Correct 104 ms 8172 KB Output is correct
55 Correct 248 ms 22380 KB Output is correct
56 Correct 270 ms 22112 KB Output is correct
57 Correct 108 ms 12268 KB Output is correct
58 Correct 0 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 5 ms 620 KB Output is correct
61 Correct 105 ms 8172 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 62 ms 5868 KB Output is correct
66 Correct 102 ms 8172 KB Output is correct
67 Correct 102 ms 8172 KB Output is correct
68 Correct 104 ms 8172 KB Output is correct
69 Correct 105 ms 12268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 257 ms 22252 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 10 ms 1260 KB Output is correct
13 Correct 243 ms 22252 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 620 KB Output is correct
17 Correct 107 ms 8160 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 61 ms 5868 KB Output is correct
21 Correct 249 ms 22252 KB Output is correct
22 Correct 244 ms 22124 KB Output is correct
23 Correct 264 ms 22112 KB Output is correct
24 Correct 245 ms 22292 KB Output is correct
25 Correct 106 ms 8160 KB Output is correct
26 Correct 102 ms 8172 KB Output is correct
27 Correct 261 ms 22124 KB Output is correct
28 Correct 243 ms 22252 KB Output is correct
29 Correct 0 ms 364 KB Output is correct
30 Correct 0 ms 364 KB Output is correct
31 Correct 0 ms 364 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 0 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 10 ms 1260 KB Output is correct
37 Correct 258 ms 22252 KB Output is correct
38 Correct 0 ms 364 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 14 ms 1260 KB Output is correct
41 Correct 250 ms 22280 KB Output is correct
42 Correct 0 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 620 KB Output is correct
45 Correct 107 ms 8172 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 62 ms 5868 KB Output is correct
50 Correct 252 ms 22252 KB Output is correct
51 Correct 244 ms 22124 KB Output is correct
52 Correct 257 ms 22124 KB Output is correct
53 Correct 106 ms 12268 KB Output is correct
54 Correct 104 ms 8172 KB Output is correct
55 Correct 248 ms 22380 KB Output is correct
56 Correct 270 ms 22112 KB Output is correct
57 Correct 108 ms 12268 KB Output is correct
58 Correct 0 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 62 ms 5868 KB Output is correct
62 Correct 248 ms 22124 KB Output is correct
63 Correct 244 ms 22124 KB Output is correct
64 Correct 262 ms 22124 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 62 ms 5868 KB Output is correct
67 Correct 251 ms 22124 KB Output is correct
68 Correct 249 ms 22124 KB Output is correct
69 Correct 256 ms 22124 KB Output is correct
70 Correct 1 ms 364 KB Output is correct
71 Correct 0 ms 364 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 64 ms 5868 KB Output is correct
74 Correct 248 ms 22252 KB Output is correct
75 Correct 249 ms 22124 KB Output is correct
76 Correct 250 ms 22112 KB Output is correct
77 Correct 247 ms 22240 KB Output is correct
78 Correct 0 ms 364 KB Output is correct
79 Correct 1 ms 364 KB Output is correct
80 Correct 5 ms 620 KB Output is correct
81 Correct 105 ms 8172 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 1 ms 364 KB Output is correct
85 Correct 62 ms 5868 KB Output is correct
86 Correct 102 ms 8172 KB Output is correct
87 Correct 102 ms 8172 KB Output is correct
88 Correct 104 ms 8172 KB Output is correct
89 Correct 105 ms 12268 KB Output is correct
90 Correct 0 ms 364 KB Output is correct
91 Correct 1 ms 364 KB Output is correct
92 Correct 5 ms 620 KB Output is correct
93 Correct 104 ms 8172 KB Output is correct
94 Correct 1 ms 268 KB Output is correct
95 Correct 0 ms 364 KB Output is correct
96 Correct 0 ms 364 KB Output is correct
97 Correct 26 ms 2284 KB Output is correct
98 Correct 113 ms 8172 KB Output is correct
99 Correct 104 ms 8172 KB Output is correct
100 Correct 104 ms 8160 KB Output is correct
101 Correct 101 ms 8172 KB Output is correct
102 Correct 102 ms 8172 KB Output is correct
103 Correct 116 ms 8172 KB Output is correct
104 Correct 101 ms 8172 KB Output is correct
105 Correct 118 ms 8172 KB Output is correct