Submission #302167

#TimeUsernameProblemLanguageResultExecution timeMemory
302167radaiosm7Connecting Supertrees (IOI20_supertrees)C++17
100 / 100
264 ms22264 KiB
#include "supertrees.h" #include <bits/stdc++.h> using namespace std; int par[1005]; int depth[1005]; vector<int> teams[1005]; vector<int> cycle; int FindPre(int x) { if (par[x] == x) { return x; } else { par[x] = FindPre(par[x]); return par[x]; } } bool IsSame(int x, int y) { if (FindPre(x) == FindPre(y)) { return true; } else { return false; } } void Union(int x, int y) { int PX = FindPre(x); int PY = FindPre(y); if (depth[PX] > depth[PY]) { par[PY] = PX; } else if (depth[PX] < depth[PY]) { par[PX] = PY; } else { par[PY] = PX; depth[PX]++; } } int construct(vector<vector<int>> p) { int n = p.size(); vector<vector<int>> ans(n, vector<int>(n, 0)); for (int i=0; i < n; i++) { par[i] = i; depth[i] = 1; } for (int i=0; i < n; i++) { for (int j=0; j < n; j++) { if (p[i][j] != 0) { Union(i,j); } if (p[i][j] == 3) { return 0; } } } for (int i=0; i < n; i++) { for (int j=0; j < n; j++) { if (p[i][j] == 0 && IsSame(i,j)) { return 0; } } } for (int i=0; i < n; i++) { teams[FindPre(i)].push_back(i); } for (int i=0; i < n; i++) { par[i] = i; depth[i] = 1; } for (int gr=0; gr < n; gr++) { if ((int)teams[gr].size() == 0) { continue; } for (int i = 0; i < (int)teams[gr].size(); i++) { for (int j = i+1; j < (int)teams[gr].size(); j++) { if (p[teams[gr][i]][teams[gr][j]] == 1) { Union(teams[gr][i], teams[gr][j]); } } } for (int i = 0; i < (int)teams[gr].size(); i++) { for (int j = i+1; j < (int)teams[gr].size(); j++) { if (p[teams[gr][i]][teams[gr][j]] == 2 && IsSame(teams[gr][i], teams[gr][j])) { return 0; } } } cycle.clear(); for (auto curr : teams[gr]) { if (par[curr] == curr) { cycle.push_back(curr); } else { ans[curr][par[curr]] = ans[par[curr]][curr] = 1; } } if ((int)cycle.size() == 2) { return 0; } else if ((int)cycle.size() == 1) { continue; } else { for (int i=0; i+1 < (int)cycle.size(); i++) { ans[cycle[i]][cycle[i+1]] = ans[cycle[i+1]][cycle[i]] = 1; } ans[cycle[0]][cycle.back()] = ans[cycle.back()][cycle[0]] = 1; } } 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...