Submission #577046

# Submission time Handle Problem Language Result Execution time Memory
577046 2022-06-13T23:20:46 Z rc_catunta Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
225 ms 22272 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

vector<int> padre,rnk,tam;

void init(int n){
    padre.assign(n,0);
    rnk.assign(n,1);
    tam.assign(n,1);
    for(int i=0;i<n;i++){
        padre[i]=i; // Es su propio padre
    }
}

int buscar(int x){
    if(padre[x]==x) return x;
    else return padre[x]=buscar(padre[x]);
}

bool mismo_conjunto(int a,int b){
    return buscar(a)==buscar(b);
}

void unir(int a,int b){
    if(!mismo_conjunto(a,b)){
        a = buscar(a);
        b = buscar(b);
        if(rnk[a]>=rnk[b]){
            // B se una a A
            padre[b] = a;
            tam[a]+=tam[b];
            if(rnk[a]==rnk[b]) rnk[a]++;
        }
        else{
            // A se une a B
            padre[a] = b;
            tam[b] += tam[a];
        }
    }
}

int construct(vector<vector<int> > p) {
	int n = p.size();
    // Armamos el Union Find
    vector<vector<int> > answer(n,vector<int>(n)); // Creamos la matriz de adyacencia
    init(n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(p[i][j]==3) return 0;
            if(p[i][j]==1){
                if(!mismo_conjunto(i,j)){
                    unir(i,j);
                    answer[i][j]=1;
                    answer[j][i]=1;
                }
            }
        }
    }
    // Prueba de Completitutd del Primer Union Find
    // Verificar si p es correcto, si existe un grafo que lo satisfaga
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(mismo_conjunto(i,j) and p[i][j]==0){
                //cout<<"COMPLETITUD 2\n";
                return 0;
			}
        }
	}
    // Ponemos a todos los representantes de los grafos linea en mapas de codificacion y decodificacion
    unordered_map <int,int> code,decode;
    int ind = 0;
    for(int i=0;i<int(padre.size());i++){
        int pi = buscar(i);
        if(pi==i){
            code[i]=ind;
            decode[ind]=i;
            ind++;
        }
    }
    int c_rep = code.size();
    //cout<<"Hay "<<c_rep<<" representantes\n";
    init(c_rep);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(p[i][j]==2){
                if(code.find(i)!=code.end() and code.find(j)!=code.end()){
                    unir(code[i],code[j]);
                }
            }
            
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(code.find(i)!=code.end() and code.find(j)!=code.end() and i!=j){
                    if(p[i][j]==0 and mismo_conjunto(code[i],code[j])){
                        //cout<<"COMPLETITUD 2\n";
                        //cout<<i<<" "<<j<<"\n";
                        return 0;
                    }
        }
                }
            
    }
    // Creamos los "conjuntos"
    unordered_map<int,vector<int> >conjuntos;
    for(int i=0;i<c_rep;i++){
        int rep = buscar(i);
        if(tam[rep]==2) return 0; // No puede haber un ciclo de 2 elementos
        if(conjuntos.find(rep) == conjuntos.end()){
            // No existe aun esa llave
            conjuntos[rep] = vector<int>();            
        }
        conjuntos[rep].push_back(i);
    }
    // Construimos la matriz de adyacencia
    for(auto elem: conjuntos){
        vector<int> lista = elem.second;
        if(lista.size() == 1) continue;
        for(int i=0;i<int(lista.size());i++){
            int a = lista[i];
            int b = lista[(i+1)%lista.size()];
            answer[decode[a]][decode[b]] = 1;
            answer[decode[b]][decode[a]] = 1;
        }
    }
    build(answer);
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 187 ms 22020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 187 ms 22020 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 8 ms 1108 KB Output is correct
13 Correct 187 ms 22128 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 724 KB Output is correct
17 Correct 112 ms 12032 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 47 ms 5708 KB Output is correct
21 Correct 189 ms 21964 KB Output is correct
22 Correct 192 ms 22100 KB Output is correct
23 Correct 199 ms 22016 KB Output is correct
24 Correct 200 ms 22148 KB Output is correct
25 Correct 81 ms 12032 KB Output is correct
26 Correct 73 ms 12052 KB Output is correct
27 Correct 203 ms 21940 KB Output is correct
28 Correct 187 ms 22144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 8 ms 1236 KB Output is correct
9 Correct 192 ms 22160 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1176 KB Output is correct
13 Correct 225 ms 22064 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 772 KB Output is correct
17 Correct 104 ms 12176 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 52 ms 5748 KB Output is correct
22 Correct 218 ms 22132 KB Output is correct
23 Correct 209 ms 22144 KB Output is correct
24 Correct 220 ms 22164 KB Output is correct
25 Correct 111 ms 12228 KB Output is correct
26 Correct 88 ms 12160 KB Output is correct
27 Correct 205 ms 22224 KB Output is correct
28 Correct 220 ms 22040 KB Output is correct
29 Correct 107 ms 12224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 50 ms 5764 KB Output is correct
5 Correct 204 ms 22024 KB Output is correct
6 Correct 198 ms 22160 KB Output is correct
7 Correct 208 ms 22044 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 56 ms 5780 KB Output is correct
10 Correct 204 ms 22096 KB Output is correct
11 Correct 209 ms 22156 KB Output is correct
12 Correct 217 ms 22160 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 50 ms 5740 KB Output is correct
17 Correct 205 ms 22032 KB Output is correct
18 Correct 210 ms 22200 KB Output is correct
19 Correct 206 ms 22092 KB Output is correct
20 Correct 196 ms 22272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 187 ms 22020 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 8 ms 1108 KB Output is correct
13 Correct 187 ms 22128 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 724 KB Output is correct
17 Correct 112 ms 12032 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 47 ms 5708 KB Output is correct
21 Correct 189 ms 21964 KB Output is correct
22 Correct 192 ms 22100 KB Output is correct
23 Correct 199 ms 22016 KB Output is correct
24 Correct 200 ms 22148 KB Output is correct
25 Correct 81 ms 12032 KB Output is correct
26 Correct 73 ms 12052 KB Output is correct
27 Correct 203 ms 21940 KB Output is correct
28 Correct 187 ms 22144 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1236 KB Output is correct
37 Correct 192 ms 22160 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1176 KB Output is correct
41 Correct 225 ms 22064 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 5 ms 772 KB Output is correct
45 Correct 104 ms 12176 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 52 ms 5748 KB Output is correct
50 Correct 218 ms 22132 KB Output is correct
51 Correct 209 ms 22144 KB Output is correct
52 Correct 220 ms 22164 KB Output is correct
53 Correct 111 ms 12228 KB Output is correct
54 Correct 88 ms 12160 KB Output is correct
55 Correct 205 ms 22224 KB Output is correct
56 Correct 220 ms 22040 KB Output is correct
57 Correct 107 ms 12224 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 4 ms 724 KB Output is correct
61 Correct 93 ms 12040 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 55 ms 5768 KB Output is correct
66 Correct 87 ms 12160 KB Output is correct
67 Correct 76 ms 12036 KB Output is correct
68 Correct 84 ms 12164 KB Output is correct
69 Correct 102 ms 12220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 187 ms 22020 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 8 ms 1108 KB Output is correct
13 Correct 187 ms 22128 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 724 KB Output is correct
17 Correct 112 ms 12032 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 47 ms 5708 KB Output is correct
21 Correct 189 ms 21964 KB Output is correct
22 Correct 192 ms 22100 KB Output is correct
23 Correct 199 ms 22016 KB Output is correct
24 Correct 200 ms 22148 KB Output is correct
25 Correct 81 ms 12032 KB Output is correct
26 Correct 73 ms 12052 KB Output is correct
27 Correct 203 ms 21940 KB Output is correct
28 Correct 187 ms 22144 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1236 KB Output is correct
37 Correct 192 ms 22160 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1176 KB Output is correct
41 Correct 225 ms 22064 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 5 ms 772 KB Output is correct
45 Correct 104 ms 12176 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 52 ms 5748 KB Output is correct
50 Correct 218 ms 22132 KB Output is correct
51 Correct 209 ms 22144 KB Output is correct
52 Correct 220 ms 22164 KB Output is correct
53 Correct 111 ms 12228 KB Output is correct
54 Correct 88 ms 12160 KB Output is correct
55 Correct 205 ms 22224 KB Output is correct
56 Correct 220 ms 22040 KB Output is correct
57 Correct 107 ms 12224 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 50 ms 5764 KB Output is correct
62 Correct 204 ms 22024 KB Output is correct
63 Correct 198 ms 22160 KB Output is correct
64 Correct 208 ms 22044 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 56 ms 5780 KB Output is correct
67 Correct 204 ms 22096 KB Output is correct
68 Correct 209 ms 22156 KB Output is correct
69 Correct 217 ms 22160 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 50 ms 5740 KB Output is correct
74 Correct 205 ms 22032 KB Output is correct
75 Correct 210 ms 22200 KB Output is correct
76 Correct 206 ms 22092 KB Output is correct
77 Correct 196 ms 22272 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 4 ms 724 KB Output is correct
81 Correct 93 ms 12040 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct
84 Correct 0 ms 212 KB Output is correct
85 Correct 55 ms 5768 KB Output is correct
86 Correct 87 ms 12160 KB Output is correct
87 Correct 76 ms 12036 KB Output is correct
88 Correct 84 ms 12164 KB Output is correct
89 Correct 102 ms 12220 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 4 ms 724 KB Output is correct
93 Correct 80 ms 12108 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 22 ms 3700 KB Output is correct
98 Correct 101 ms 14068 KB Output is correct
99 Correct 101 ms 14072 KB Output is correct
100 Correct 76 ms 14080 KB Output is correct
101 Correct 77 ms 13996 KB Output is correct
102 Correct 77 ms 14084 KB Output is correct
103 Correct 93 ms 14000 KB Output is correct
104 Correct 86 ms 14068 KB Output is correct
105 Correct 92 ms 14064 KB Output is correct