제출 #401186

#제출 시각아이디문제언어결과실행 시간메모리
401186my99n슈퍼트리 잇기 (IOI20_supertrees)C++14
11 / 100
271 ms22852 KiB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

int p[2000], mark[2020];
int find (int u) { return u==p[u]? u:p[u]=find(p[u]); }
bool merge (int i, int j) {
  // cerr << "merge " << find(i) << ' ' << find(j) << endl;
  if (find(i) == find(j)) return false;
  p[find(i)] = find(j);
  return true;
}

void log (vector<vector<int>> s) {
  cerr << "log" << endl;
  for (auto x : s) {
    for (auto y : x) {
      cerr << y << ' ';
    } cerr << endl;
  }
  cerr << endl;
}

int construct(vector<vector<int>> req) {
	int n = req.size();
	vector<vector<int>> answer;
	for (int i = 0; i < n; i++) {
		vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}

  iota(p, p+n+1, 0);
  int s = 0;
  for (int i = 0; i < n; i++){
    for (int j = 0; j < n; j++) {
      if (find(i) == find(j)) continue;
      if (req[i][j] == 1) {
        // cerr << i << ' ' << j << endl;
        mark[i] = mark[j] = 1;
        answer[j][i] = answer[i][j] = 1; 
        merge(i, j);
        s = i;
      }
    }
  }

  // log(answer);

  set<int> next;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) continue;
      if (req[i][j] == 2) {
        if (!mark[i] and !mark[j]) {
          next.insert(i);
          next.insert(j);
        }
      }
    }
  }

  int last = s;
  for (auto x : next) {
    // cerr << x << endl;
    answer[s][x] = answer[x][s] = 1;
    s = x;
  }
  if (last != s) answer[last][s] = answer[s][last] = 1;
  answer[0][0] = 0;

	build(answer);
	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...