Submission #603448

#TimeUsernameProblemLanguageResultExecution timeMemory
603448Sam_a17슈퍼트리 잇기 (IOI20_supertrees)C++14
21 / 100
244 ms24024 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 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;
  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(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;
  }

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