제출 #351282

#제출 시각아이디문제언어결과실행 시간메모리
351282BlancaHM슈퍼트리 잇기 (IOI20_supertrees)C++14
11 / 100
277 ms22380 KiB
#include <iostream> #include <vector> #include <unordered_map> #include "supertrees.h" using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; int root(int a, vector<int> & parent) { if (a == parent[a]) return a; return parent[a] = root(parent[a], parent); } void merge(int i, int j, vector<int> & parent, vector<int> & rnk, int & numCCs) { int root_i = root(i, parent), root_j = root(j, parent); if (root_i == root_j) return; numCCs--; if (rnk[i] != rnk[j]) { if (rnk[i] > rnk[j]) parent[root_j] = parent[root_i]; else parent[root_i] = parent[root_j]; } else { parent[root_i] = parent[root_j]; rnk[j]++; } } int construct(vvi p) { int N = (int) p.size(), numCCs = N; vector<int> parent(N), rnk(N, 0); for (int i = 0; i < N; i++) parent[i] = i; 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; } merge(i, j, parent, rnk, numCCs); } else if (root(i, parent) == root(j, parent)) return 0; } } unordered_map<int, int> roots; int c = 0; for (int i = 0; i < N; i++) { if (parent[i] == i) roots[i] = c++; } vvi CCs(numCCs, vi()); for (int i = 0; i < N; i++) CCs[roots[root(i, parent)]].push_back(i); vi allTwos, rest; vvi result(N, vi(N, 0)); bool twos; for (int i = 0; i < numCCs; i++) { if ((int) CCs[i].size() == 1) continue; allTwos.clear(); rest.clear(); for (int j = 0; j < (int) CCs[i].size(); j++) { twos = true; for (int k = 0; k < (int) CCs[i].size(); k++) { if (k == j) continue; if (p[CCs[i][j]][CCs[i][k]] == 1) { twos = false; break; } } if (twos) allTwos.push_back(CCs[i][j]); else rest.push_back(CCs[i][j]); } for (int i = 0; i < (int) allTwos.size()-1; i++) result[allTwos[i]][allTwos[i+1]] = result[allTwos[i+1]][allTwos[i]] = 1; for (int i = 0; i < (int) rest.size()-1; i++) result[rest[i]][rest[i+1]] = result[rest[i+1]][rest[i]] = 1; if ((int) allTwos.size() == 1) return 0; if (rest.empty()) result[allTwos[0]][allTwos[(int) allTwos.size()-1]] = result[allTwos[(int) allTwos.size()-1]][allTwos[0]] = 1; else if (allTwos.size()) result[allTwos[0]][rest[0]] = result[rest[0]][allTwos[0]] = result[rest[0]][allTwos[(int) allTwos.size()-1]] = result[allTwos[(int) allTwos.size()-1]][rest[0]] = 1; } build(result); 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...