제출 #303179

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

int construct(vector<vector<int>> p)
{
	int N = p.size();
	for (int i=0;i<N;i++) for (int j=0;j<N;j++) if (p[i][j] == 3) return 0;

	vector<vector<int>> answer(N, vector<int>(N, 0));
	vector<int> line(N, 0), compo(N, 0);
	for (int i=0;i<N;i++) line[i] = compo[i] = i;
	function<int(int, vector<int>&)> get; get = [&](int n, vector<int> &par){ return par[n] == n ? n : par[n] = get(par[n], par); };
	for (int i=0;i<N;i++) for (int j=i+1;j<N;j++){
		if (p[i][j]) compo[get(j, compo)] = get(i, compo);
		if (p[i][j] == 1) line[get(j, line)] = get(i, line);
	}
	for (int i=0;i<N;i++) if (get(i, compo) == i){
		vector <int> cycle;
		for (int j=0;j<N;j++) if (get(j, compo) == i && get(j, line) == j) cycle.push_back(j);
		int n = cycle.size();
		if (n == 1) continue;
		if (n == 2) return 0;
		for (int j=0;j<n;j++) answer[cycle[j]][cycle[(j+1)%n]] = answer[cycle[(j+1)%n]][cycle[j]] = 1;
	}
	for (int i=0;i<N;i++) if (get(i, line) != i) answer[get(i, line)][i] = answer[i][get(i, line)] = 1;
	for (int i=0;i<N;i++) for (int j=i+1;j<N;j++){
		if (get(i, line) == get(j, line)){
			if (p[i][j] != 1) return 0;
		}
		else if (get(i, compo) == get(j, compo)){
			if (p[i][j] != 2) return 0;
		}else{
			if (p[i][j] != 0) return 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...