제출 #1320147

#제출 시각아이디문제언어결과실행 시간메모리
1320147nicolo_010슈퍼트리 잇기 (IOI20_supertrees)C++20
100 / 100
101 ms22188 KiB
#include "supertrees.h" #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; struct DSU { vector<int> rank, parent; DSU(int n) { rank.assign(n, 1); parent.resize(n); for (int i=0; i<n; i++) { parent[i] = i; } } int find(int n) { return (n == parent[n] ? n : parent[n] = find(parent[n])); } void unite(int n1, int n2) { int p1 = find(n1); int p2 = find(n2); if (p1==p2) return; rank[p1] += rank[p2]; parent[p2] = p1; } }; int construct(std::vector<std::vector<int>> p) { int n = p.size(); vector<vector<int>> ans(n, vector<int>(n, 0)); bool can = true; 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] != p[j][i]) can = false; } } if (!can) { return 0; } DSU dsu1(n); DSU dsu2(n); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if (p[i][j] == 1) dsu1.unite(i, j); if (p[i][j] == 2) dsu2.unite(i, j); } } //Unir 1-componente vector<bool> vis(n, false); for (int i=0; i<n; i++) { int pi = dsu1.find(i); if (pi==i) { vector<int> cmp = {i}; for (int j=0; j<n; j++) { if (j==i) continue; int pj = dsu1.find(j); if (pj == i) { cmp.push_back(j); } } for (auto x : cmp) { vis[x] = true; ans[i][x] = ans[x][i] = 1; } for (auto j1 : cmp) { for (auto j2 : cmp) { if (p[j1][j2] == p[j2][j1] && p[j1][j2] == 0) { return 0; } } } } } //Unir 2-componentes for (int i=0; i<n; i++) { int pi = dsu2.find(i); if (pi==i) { vector<int> cmp; for (int j=0; j<n; j++) { int pj = dsu2.find(j); if (pj == i) { cmp.push_back(j); } } int m = cmp.size(); set<int> starred; //starred son los roots de estrellas. //Debo hacer que el ciclo este conectado a todas estas estrellas vector<int> cycle; for (auto x : cmp) { if (!vis[x]) { cycle.push_back(x); } else { int px = dsu1.find(x); if (px==x) { //x es el root starred.insert(x); } } } for (auto x : starred) { cycle.push_back(x); } //En el ciclo deben estar estas estrellas colgando m = cycle.size(); for (int j=0; j<m; j++) { int u = cycle[j]; int v = cycle[(j+1)%m]; ans[u][v] = ans[v][u] = 1; } if (cycle.size() == 2) return 0; for (auto x : cmp) { for (auto y : cmp) { if (p[x][y] == 0) return 0; } } } } for (int i=0; i<n; i++) { ans[i][i] = 0; } build(ans); 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...