Submission #1119045

#TimeUsernameProblemLanguageResultExecution timeMemory
1119045mannshah1211Connecting Supertrees (IOI20_supertrees)C++17
21 / 100
199 ms24232 KiB
/**
 *    author: tourist
 *    created:
**/
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

void build(vector<vector<int>> b);

class dsu {
 public:
  vector<int> p;
  int n;
  
  dsu(int _n) : n(_n) {
    p.resize(n);
    iota(p.begin(), p.end(), 0);
  }
  
  int get(int x) {
    return ((p[x] == x) ? x : (p[x] = get(p[x])));
  }
  
  bool unite(int u, int v) {
    u = get(u); v = get(v);
    if (u != v) {
      p[v] = u;
      return true;
    }
    return false;
  }
};

int construct(vector<vector<int>> p) {
  int n = p.size();
  dsu ds(n);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (p[i][j] > 0) {
        ds.unite(i, j);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (!p[i][j]) {
        if (ds.get(i) == ds.get(j)) {
          return 0;
        }
      }
    }
  }
  dsu two(n), one(n);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (p[i][j] == 1) {
        one.unite(i, j);
      }
      if (p[i][j] == 2) {
        two.unite(i, j);
      }
    }
  }
  vector<vector<int>> b(n, vector<int>(n));
  vector<vector<int>> comps(n);
  for (int i = 0; i < n; i++) {
    comps[one.get(i)].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    if (comps[i].size() <= 1) {
      continue;
    }
    for (int j = 0; j + 1 < comps[i].size(); j++) {
      b[comps[i][j + 1]][comps[i][j]] = true;
      b[comps[i][j]][comps[i][j + 1]] = true;
    }
  }
  for (int i = 0; i < n; i++) {
    comps[i].clear();
  }
  /*for (int i = 0; i < n; i++) {
    comps[two.get(i)].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    if (comps[i].size() <= 1) {
      continue;
    }
    for (int j = 0; j < comps[i].size(); j++) {
      b[comps[i][(j + 1) % comps[i].size()]][comps[i][j]] = true;
      b[comps[i][j]][comps[i][(j + 1) % comps[i].size()]] = true;
    }
  }*/
  build(b);
  return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:81:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     for (int j = 0; j + 1 < comps[i].size(); j++) {
      |                     ~~~~~~^~~~~~~~~~~~~~~~~
#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...