제출 #309387

#제출 시각아이디문제언어결과실행 시간메모리
309387georgerapeanu슈퍼트리 잇기 (IOI20_supertrees)C++17
56 / 100
266 ms22392 KiB
#include "supertrees.h" #include <vector> #include <cstdio> using namespace std; struct dsu_t{ int n; int father[1005]; dsu_t(int n){ this->n = n; for(int i = 0;i < n;i++){ father[i] = -1; } } int find_root(int nod){ if(father[nod] == -1){ return nod; } return father[nod] = find_root(father[nod]); } bool unite(int x,int y){ x = find_root(x); y = find_root(y); if(x == y){ return false; } father[x] = y; return true; } }; int construct(vector<vector<int>> p) { int n = p.size(); vector<vector<int>> answer(n,vector<int>(n,0)); dsu_t dsu(n); for (int i = 0; i < n; i++) { for(int j = 0;j < n;j++){ if(p[i][j] == 3){ return 0; } else if(p[i][j] == 1){ if(dsu.unite(i,j)){ answer[i][j] = answer[j][i] = 1; } } } } for (int i = 0; i < n; i++) { for(int j = 0;j < n;j++){ if(p[i][j] == 2 || p[i][j] == 0){ if(dsu.find_root(i) == dsu.find_root(j)){ return 0; } } } } dsu_t dsu2(n); for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(p[i][j] == 2){ dsu2.unite(dsu.find_root(i),dsu.find_root(j)); } } } vector<vector<int> > nodes(n,vector<int>()); for(int i = 0;i < n;i++){ nodes[dsu2.find_root(i)].push_back(i); } for(int i = 0;i < n;i++){ if(nodes[i].size() == 2){ return 0; } if(nodes[i].size() > 2){ int len = nodes[i].size(); for(int j = 0;j < len;j++){ answer[nodes[i][j]][nodes[i][(j + 1) % len]] = 1; answer[nodes[i][(j + 1) % len]][nodes[i][j]] = 1; } } } build(answer); 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...