Submission #1291768

#TimeUsernameProblemLanguageResultExecution timeMemory
1291768SofiatpcConnecting Supertrees (IOI20_supertrees)C++20
100 / 100
100 ms22184 KiB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1005;
int rep[MAXN], comp[MAXN];

int construct(vector<vector<int>> p) {
	int n = p.size();

	vector<vector<int>> ans; ans.resize(n);
	for(int i = 0; i < n; i++){
		ans[i].resize(n);
		for(int j = 0; j < n; j++){
			ans[i][j] = 0;
			if(p[i][j] == 3)return 0;
		}
		rep[i] = -1;
	}

	for(int i = 0; i < n; i++){
		if(rep[i] != -1){
			for(int j = 0; j < n; j++){
				if(p[i][j] == 1 && rep[j] != rep[i])return 0;
				if(p[i][j] != 1 && rep[j] == rep[i])return 0;
			}
		}else{
			int lst = -1;
			for(int j = 0; j < n; j++)
				if(p[i][j] == 1){
					rep[j] = i;
					if(lst != -1){ans[lst][j] = 1; ans[j][lst] = 1;}
					lst = j;
				}
		}
	}

	int cur = 0;
	for(int i = 0; i < n; i++){
		if(comp[i] != 0){
			for(int j = 0; j < n; j++){
				if(p[i][j] > 0 && comp[j] != comp[i])return 0;
				if(p[i][j] == 0 && comp[j] == comp[i])return 0;
			}
		}else{
			set<int> st; cur++;
			for(int j = 0; j < n; j++)
				if(p[i][j]){
					st.insert(rep[j]);
					comp[j] = cur;
				}

			if(st.size() == 1)continue;
			if(st.size() == 2)return 0;

			auto it = st.begin(); it++;
			for(; it != st.end(); it++){
				auto lst = it; lst--;
				ans[*lst][*it] = 1; ans[*it][*lst] = 1;
			}
			it--;
			ans[*st.begin()][*it] = 1; ans[*it][*st.begin()] = 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...