Submission #776758

#TimeUsernameProblemLanguageResultExecution timeMemory
776758_martynasConnecting Supertrees (IOI20_supertrees)C++14
100 / 100
191 ms24052 KiB
#include "supertrees.h" #include <vector> #include <algorithm> #include <iostream> using namespace std; int n; bool check_contr(int u, vector<bool> &visited, vector<bool> &in_group, vector<int> &group, vector<vector<int>> &p) { visited[u] = in_group[u] = true; group.push_back(u); for(int v = 0; v < n; v++) { if(p[u][v] == 0) { if(in_group[v]) return true; } else if(!visited[v]) { if(check_contr(v, visited, in_group, group, p)) return true; } } return false; } int construct(vector<vector<int>> p) { // // sample interaction // int n = p.size(); // vector<vector<int>> answer; // for (int i = 0; i < n; i++) { // vector<int> row; // row.resize(n); // answer.push_back(row); // } // build(answer); // return 1; n = p.size(); // Impossible when any p_i == 3 for(auto &v : p) for(int x : v) if(x == 3) return 0; // Check contradictions (a reaches b and b reaches c implies a reaches c) vector<bool> visited(n), in_group(n); vector<vector<int>> groups; for(int i = 0; i < n; i++) { if(!visited[i]) { vector<int> group; if(check_contr(i, visited, in_group, group, p)) return 0; for(int x : group) in_group[x] = false; groups.push_back(group); } } vector<vector<int>> ans(n, vector<int>(n)); for(auto &g : groups) { // Every vertex p values within a group are either 1 or 2. // There is at most a single cycle in a group. // Vertices part of the cycle should have p == 2 // for all other member of the groups except the subtree attached // After making the cycle a root other members // should have p == 1 to other vertices in the same subtree // and p == 2 to other vertices. // // Edge case: only 2 subtrees in a group are impossible vector<int> root(g.size(), -1); int subtrees = 0; for(int i = 0; i < g.size(); i++) { if(root[i] == -1) { // new subtree subtrees++; root[i] = g[i]; for(int j = 0; j < g.size(); j++) { if(i == j) continue; if(p[g[i]][g[j]] != 2) { if(root[j] != -1) { return 0; // contradiction } root[j] = root[i]; } } } else { // check old subtree for(int j = 0; j < g.size(); j++) { if(i == j) continue; if(p[g[i]][g[j]] != 2) { if(root[j] == -1 || root[i] != root[j]) { return 0; // contradiction } } } } } if(subtrees == 2) return 0; for(int i = 0; i < g.size(); i++) { ans[g[i]][root[i]] = ans[root[i]][g[i]] = 1; } sort(root.begin(), root.end()); root.erase(unique(root.begin(), root.end()), root.end()); for(int i = 0; i < root.size(); i++) { ans[root[i]][root[(i+1)%root.size()]] = ans[root[(i+1)%root.size()]][root[i]] = 1; } } // ensure b[i][i] = 0 for(int i = 0; i < n; i++) { ans[i][i] = 0; } build(ans); return 1; }

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:63:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         for(int i = 0; i < g.size(); i++) {
      |                        ~~^~~~~~~~~~
supertrees.cpp:68:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |                 for(int j = 0; j < g.size(); j++) {
      |                                ~~^~~~~~~~~~
supertrees.cpp:80:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |                 for(int j = 0; j < g.size(); j++) {
      |                                ~~^~~~~~~~~~
supertrees.cpp:91:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         for(int i = 0; i < g.size(); i++) {
      |                        ~~^~~~~~~~~~
supertrees.cpp:96:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |         for(int i = 0; i < root.size(); i++) {
      |                        ~~^~~~~~~~~~~~~
#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...