Submission #365120

#TimeUsernameProblemLanguageResultExecution timeMemory
365120FarriusConnecting Supertrees (IOI20_supertrees)C++14
0 / 100
1 ms364 KiB
#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<pair<int, 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], 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 (auto cur : uno) { sol[cur.first][cur.second] = 1; } } /* //crear la sol haciendo trees con los cc for (int i = 0; i < n; i++) { if ((int) g[i].size() < 2) continue; for (int j = 1; j < (int) g[i].size(); j++) { sol[g[i][j - 1]][g[i][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 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...