Submission #373499

#TimeUsernameProblemLanguageResultExecution timeMemory
373499Matteo_VerzConnecting Supertrees (IOI20_supertrees)C++17
100 / 100
273 ms24300 KiB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

vector <vector <int>> answer;
vector <int> viz;
vector <int> countroot;
vector <int> roots;
bitset <1001> isroot;
bool ok = true;

void Solve_Component(int node, vector <vector <int>> &p, int comp) {
  int n = p.size();
  vector <int> realnodes;

  for(int i = 0; i < n; i++)
    if(p[node][i] > 0 || i == node) {
      viz[i] = comp; // mark each node as part of the component
      realnodes.push_back(i); // we keep a vector with only the nodes in the component
    }

  for(int i : realnodes) {
    for(int j = 0; j < n; j++)
      if(p[i][j] == 0 && p[node][j] != 0) { // if there is path from i to j, from j to k, but not from i to k
        ok = false;
        return;
      } else if(p[i][j] != 0 && p[node][j] == 0) { // same shit
        ok = false;
        return;
      }
  }

  for(int i : realnodes)
    isroot[i] = true; // we consider each node as the "root"

  roots.clear();
  for(int i = 0; i < realnodes.size(); i++) { // we go through each node
    if(isroot[realnodes[i]] == true) {
      roots.push_back(realnodes[i]);
      countroot[realnodes[i]]++;
      if(countroot[realnodes[i]] > 1) { // if the node is a root of more than 1 tree
        ok = false;
        return;
      }

      for(int j = i + 1; j < realnodes.size(); j++) {
        if(p[realnodes[i]][realnodes[j]] == 1) { // if there is only 1 path from i to j => i is parent of j
          answer[realnodes[i]][realnodes[j]] = answer[realnodes[j]][realnodes[i]] = 1;
          isroot[realnodes[j]] = false;
        } else if(p[realnodes[i]][realnodes[j]] == 0) { // they are in the same component, but no path between them
          ok = false;
          return;
        }
      }
    }
  }

  if(roots.size() == 1) return; // it's fine, we only have a tree
  if(roots.size() == 2) { // we have a cycle of length 2 => impossible
    ok = false;
    return;
  }

  for(int i = 0; i < roots.size() - 1; i++)
    answer[roots[i]][roots[i + 1]] = answer[roots[i + 1]][roots[i]] = 1; // we connect the roots
  answer[roots[0]][roots.back()] = answer[roots.back()][roots[0]] = 1;
}

int construct(vector<vector<int>> p) {
  int n = p.size();

  answer.resize(n);
  for(int i = 0; i < n; i++) answer[i].resize(n);

  // last subtask, p[i][j] == 3
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++)
      if(p[i][j] > 2) return 0;
  }

  viz.resize(n);
  countroot.resize(n);
  int cc(0);
  for(int i = 0; i < n; i++)
     if(viz[i] == 0)
       Solve_Component(i, p, ++cc); // solve for each component

  if(ok == false)
    return 0;

  build(answer);
  return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'void Solve_Component(int, std::vector<std::vector<int> >&, int)':
supertrees.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |   for(int i = 0; i < realnodes.size(); i++) { // we go through each node
      |                  ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:47:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |       for(int j = i + 1; j < realnodes.size(); j++) {
      |                          ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:65:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |   for(int i = 0; i < roots.size() - 1; 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...