Submission #379528

#TimeUsernameProblemLanguageResultExecution timeMemory
379528SuhaibSawalha1Connecting Supertrees (IOI20_supertrees)C++14
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;

void build (vector<vector<int>> p) {
  for (auto &i : p) {
    for (int j : i) cout << j << " "; cout << "\n";
  }
}

struct DSU {

  vector<int> dsu, sz;

  DSU (int n) {
    dsu.resize(n);
    iota(dsu.begin(), dsu.end(), 0);
    sz.resize(n, 1);
  }

  int find (int u) {
    return u == dsu[u] ? u : dsu[u] = find(dsu[u]);
  }

  bool unite (int u, int v) {
    if ((u = find(u)) != (v = find(v))) {
      if (sz[u] < sz[v]) {
        swap(u, v);
      }
      dsu[v] = u;
      sz[u] += sz[v];
      return 1;
    }
    return 0;
  }
};
    
int construct(vector<vector<int>> p) {
  int n = p.size();
  vector<vector<int>> ans(n, vector<int>(n));
  DSU nodes(n);
  bool two = 0;
  auto sit = [&] (int i, int j, int t) {
    ans[i][j] = ans[j][i] = t;
  };
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (p[i][j] == 1) {
        sit(i, j, nodes.unite(i, j));
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (p[i][j] == 0 && nodes.find(i) == nodes.find(j)) {
        return 0;
      }
    }
  }
  vector<vector<int>> c(n);
  for (int i = 0; i < n; ++i) {
    c[nodes.find(i)].push_back(i);
  }
  DSU cmp(n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (p[i][j] == 2) {
        cmp.unite(nodes.find(i), nodes.find(j));
      }
    }
  }
  vector<vector<int>> C(n);
  for (int i = 0; i < n; ++i) {
    C[cmp.find(i)].push_back(i);
  }
  for (int i = 0; i < n; ++i) {
    if (i != cmp.find(i)) {
      continue;
    }
    for (int j = 0; j < n; ++j) {
      if (nodes.find(i) != nodes.find(j) && cmp.find(i) == cmp.find(j) && p[i][j] != 2) {
        return 0;
      }
    }
    for (int j : c[i]) {
      for (int k = 0; k < n; ++k) {
        if ((p[k][i] == 2) ^ (p[j][k] == 2)) {
          return 0;
        }
      }
    }
    for (int j = 0; j < C[i].size(); ++j) {
      for (int k = j + 1; k < C[i].size(); ++k) {
        if (p[C[i][j]][C[i][k]] ^ 2) {
          return 0;
        }
      }
    }
    if (C[i].size() == 2) {
      return 0;
    }
    if (C[i].size() > 2) {
      sit(C[i][0], C[i][1], 1);
      sit(C[i][1], C[i][2], 1);
      for (int j = 3; j < C[i].size(); ++j) {
        sit(C[i][j], C[i][j - 1], 1);
      }
      sit(C[i].back(), C[i][0], 1);
    }
  }
  build(ans);
  return 1;
}

int main (){

  #ifndef ONLINE_JUDGE
    freopen("SuhaibSawalha1","r",stdin);
  #endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);

  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }  
  construct(a);

  return 0;
}

Compilation message (stderr)

supertrees.cpp: In function 'void build(std::vector<std::vector<int> >)':
supertrees.cpp:6:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
    6 |     for (int j : i) cout << j << " "; cout << "\n";
      |     ^~~
supertrees.cpp:6:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
    6 |     for (int j : i) cout << j << " "; cout << "\n";
      |                                       ^~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:91:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |     for (int j = 0; j < C[i].size(); ++j) {
      |                     ~~^~~~~~~~~~~~~
supertrees.cpp:92:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |       for (int k = j + 1; k < C[i].size(); ++k) {
      |                           ~~^~~~~~~~~~~~~
supertrees.cpp:104:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |       for (int j = 3; j < C[i].size(); ++j) {
      |                       ~~^~~~~~~~~~~~~
supertrees.cpp:41:8: warning: unused variable 'two' [-Wunused-variable]
   41 |   bool two = 0;
      |        ^~~
supertrees.cpp: In function 'int main()':
supertrees.cpp:117:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  117 |     freopen("SuhaibSawalha1","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/ccv7bak6.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/cc0FAeCK.o:supertrees.cpp:(.text.startup+0x0): first defined here
/tmp/ccv7bak6.o: In function `build(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)':
grader.cpp:(.text+0x1d0): multiple definition of `build(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)'
/tmp/cc0FAeCK.o:supertrees.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status