Submission #1200387

#TimeUsernameProblemLanguageResultExecution timeMemory
1200387peraConnecting Supertrees (IOI20_supertrees)C++20
0 / 100
286 ms22256 KiB
#include "supertrees.h"
#include "bits/stdc++.h"
#include <vector>
using namespace std;
struct dsu{
   int n , cnt;
   vector<int> P , sz;
   dsu(int _size){
      n = _size;
      cnt = _size;
      P = vector<int>(_size + 1);
      iota(P.begin() , P.end() , 0);
      sz = vector<int>(_size + 1);
      fill(sz.begin() , sz.end() , 1);
   }  
   function<int(int)> find = [&](int u){
      return u == P[u] ? u : P[u] = find(P[u]);
   };
   function<bool(int , int)> same = [&](int u , int v){
      return find(u) == find(v);
   };
   function<bool(int , int)> join = [&](int u , int v){
      u = find(u);
      v = find(v);
      if(u != v){
         if(sz[u] < sz[v]){
            swap(u , v);
         }
         P[v] = u;
         sz[u] += sz[v];
         --cnt;
         return true;
      }
      return false;
   };
};
int construct(std::vector<std::vector<int>> p) {
	int n = (int)p.size();
   dsu d(n);
   for(int i = 0;i < n;i ++){
      for(int j = 0;j < n;j ++){
         if(p[i][j] > 0){
            d.join(i , j);
         }
      }
   }
   for(int i = 0;i < n;i ++){
      for(int j = 0;j < n;j ++){
         if(d.same(i , j) && !p[i][j]){
            return 0;
         }
      }
   }
   dsu dx(n);
   for(int i = 0;i < n;i ++){
      for(int j = 0;j < n;j ++){
         if(p[i][j] == 1){
            dx.join(i , j);
         }
      }
   }
   for(int i = 0;i < n;i ++){
      for(int j = 0;j < n;j ++){
         if(dx.same(i , j) && p[i][j] != 1){
            return 0;
         }
      }
   }
   vector<vector<int>> ans(n , vector<int>(n));
   vector<vector<int>> v(n);
   for(int i = 0;i < n;i ++){
      v[dx.find(i)].emplace_back(i);
   }
   for(int i = 0;i < n;i ++){
      for(int j = 0;j + 1 < (int)v[i].size();j ++){
         ans[v[i][j]][v[i][j + 1]] = 1;
         ans[v[i][j + 1]][v[i][j]] = 1;
      }
   }
   vector<int> st(n);
   vector<vector<int>> cyc(n);
   for(int i = 0;i < n;i ++){
      int c1 = 0 , c2 = 0;
      for(int j = 0;j < n;j ++){
         c1 += (p[i][j] == 1);
         c2 += (p[i][j] == 2);
      }
      if(c1 == 1 && c2 == d.sz[d.find(i)] - 1 && c2 > 0){
         cyc[d.find(i)].emplace_back(i);
      }
   } 
   for(int i = 0;i < n;i ++){
      if(d.find(i) == i){
         set<int> S;
         for(int x : cyc[i]){
            S.insert(x);
         }
         for(int x : v[i]){
            S.insert(x);
         }
         if((int)S.size() != d.sz[i]){
            return 0;
         }
         if((int)v[i].size() > 0){
            vector<bool> in_cyc(n);
            for(int x : cyc[i]){
               in_cyc[x] = true;
            }
            bool done = true;
            for(int &x : v[i]){
               if(in_cyc[x]){
                  swap(x , v[i][0]);
                  done = true;
               }
            }
            if(!done){
               cyc[i].emplace_back(v[i][0]);
            }
         }  
         if((int)cyc.size() == 2){
            return 0;
         }
         if((int)cyc[i].size() > 1){
            for(int j = 0;j < (int)cyc[i].size();j ++){
               int e = (j + 1) % ((int)cyc[i].size());
               ans[cyc[i][j]][cyc[i][e]] = 1;
               ans[cyc[i][e]][cyc[i][j]] = 1;
            }
         }
         for(int j = 0;j + 1 < (int)v[i].size();j ++){
            ans[v[i][j]][v[i][j + 1]] = 1;
            ans[v[i][j + 1]][v[i][j]] = 1;
         }
      }
   }
   build(ans);
   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...