Submission #603469

#TimeUsernameProblemLanguageResultExecution timeMemory
603469Sam_a17Connecting Supertrees (IOI20_supertrees)C++14
21 / 100
239 ms22036 KiB
// #include "A.cpp"
#include <bits/stdc++.h>
// #include <vector>
using namespace std;

#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(x) cout << #x << " " << x << endl;
#define uniq(x) x.resize(unique(all(x)) - x.begin());

#define pb push_back
#define ld long double
#define ll long long

void build(std::vector<std::vector<int>> p);

const int N = 2e5 + 10;
int par[N], sz[N];

int find(int a) {
  if(a != par[a]) {
    par[a] = find(par[a]);
  }
  return par[a];
}

int same(int a, int b) {
  if(find(a) == find(b)) {
    return 1;
  } else {
    return 0;
  }
}

int merge(int a, int b) {
  a = find(a), b = find(b);
  if(a == b) {
    return 0;
  }

  if(sz[a] < sz[b]) {
    swap(a, b);
  }

  par[b] = a, sz[a] += sz[b];
  return 1;
}

int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	bool allOne = true, lowerOne = true, zeroOrtwo = true;
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      if(p[i][j] != 1) {
        allOne = false;
      }

      if(p[i][j] > 1) {
        lowerOne = false;
      }

      if(p[i][j] == 1 || p[i][j] == 3) {
        zeroOrtwo = false;
      }
    }
  }

  if(allOne) {
    vector<vector<int>> answ(n, vector<int> (n, 0));
    for(int i = 0; i < n - 1; i++){
      answ[i][i + 1] = 1;
      answ[i + 1][i] = 1;
    }
	  build(answ);
    return 1;
  }

  if(lowerOne) {
    vector<vector<int>> answ(n, vector<int> (n, 0));
    
    for(int i = 0; i < n; i++) {
      par[i] = i, sz[i] = 1;
    }
    
    for(int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        if(p[i][j] == 1) {
          if(merge(i, j)) {
            answ[i][j] = answ[j][i] = 1;
          }
        }
      }
    }


    for(int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        if(same(i, j) != p[i][j]) {
          return 0;
        }
      }
    }

	  build(answ);
    return 1;
  }

  if(zeroOrtwo){
    vector<vector<int>> answ(n, vector<int> (n, 0));
    for(int i = 0; i < n; i++) {
      par[i] = i, sz[i] = 1;
    }
    
    // vector<int> out(n, 0);
    vector<int> ans;
    for(int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        if(p[i][j] == 2) {
          ans.push_back(i);
          ans.push_back(j);
        }
      }
    }

    sort(all(ans));
    uniq(ans);

    if(sz(ans) == 2) {
      return 0;
    }

    for(int i = 1; i < sz(ans); i++) {
      answ[ans[i]][ans[i - 1]] = 1;
      answ[ans[i - 1]][ans[i]] = 1;
    }


    answ[ans.back()][ans.front()] = 1;
    answ[ans.front()][ans.back()] = 1;

    build(answ);
    return 1;
  }



	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...