Submission #335953

#TimeUsernameProblemLanguageResultExecution timeMemory
335953richyrichConnecting Supertrees (IOI20_supertrees)C++17
40 / 100
346 ms24276 KiB
#include "supertrees.h" #include <vector> #include <bits/stdc++.h> using namespace std; const int N = 1e3+3; const int inf = 1e9; std::vector<std::vector<int>> answer; int par1[N], ran1[N], par2[N], ran2[N]; int find_p1(int a) { return (a == par1[a]) ? a : find_p1(par1[a]); } int find_p2(int a) { return (a == par2[a]) ? a : find_p2(par2[a]); } void unitee1(int a, int b) { a = find_p1(a), b = find_p1(b); if(a == b) return; if(ran1[a] < ran1[b]) swap(a, b); par1[b] = par1[a]; if(ran1[a]==ran1[b]) ran1[a]++; } void unitee2(int a, int b) { a = find_p2(a), b = find_p2(b); if(a == b) return; if(ran2[a] < ran2[b]) swap(a, b); par2[b] = par2[a]; if(ran2[a]==ran2[b]) ran2[a]++; } int construct(std::vector<std::vector<int>> p) { int n = p.size(); for (int i = 0; i < n; i++) { std::vector<int> row; row.resize(n); answer.push_back(row); } for(int i = 0; i < n; i++) par2[i] = par1[i] = i, ran2[i] = ran1[i] = 1; 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] == 1) { unitee1(i, j); } } } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { answer[i][j] = 0; if(i==j) continue; if(p[i][j] == 0 && find_p1(i)==find_p1(j)) return 0; if(p[i][j]==1) { int pp = find_p1(i); if(pp != i)answer[pp][i] = 1, answer[i][pp] = 1; if(pp != j)answer[pp][j] = 1, answer[j][pp] = 1; } } } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(p[i][j] == 2) { unitee2(find_p1(i), find_p1(j)); } } } map<int, set<int>> M; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(i==j) { answer[i][j] = 0; continue; } int a = find_p1(i), b = find_p1(j); if(p[i][j] == 0 && find_p2(a)==find_p2(b)) return 0; if(p[i][j]== 2 && find_p2(a)==find_p2(b)) { int pp = find_p2(a); if(pp != i)M[pp].insert(a); if(pp != j)M[pp].insert(b); } } } for(auto c : M) { int f = c.first, prev = c.first; for(auto x : c.second) { answer[prev][x] = 1, answer[x][prev] = 1; prev = x; } if(c.second.size() > 1){ answer[f][prev] = answer[prev][f] = 1; } else { return 0; } } 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...