Submission #577892

#TimeUsernameProblemLanguageResultExecution timeMemory
5778921neConnecting Supertrees (IOI20_supertrees)C++14
0 / 100
1 ms300 KiB
#include "supertrees.h"
#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
struct DSU{
	vector<int>parent;
	vector<int>sz;
	vector<vector<int>>comp;
	void build(int n){
		parent.resize(n);
		sz.resize(n);
		comp.resize(n);
		for (int i = 0;i<n;++i){
			parent[i] = i;
			sz[i] = 1;
			comp[i].push_back(i);
		}
	}
	int findsets(int v){
		if (v == parent[v])return v;
		parent[v] = findsets(parent[v]);
		return parent[v];
	}
	bool unionset(int a,int b){
		int u = findsets(a);
		int v = findsets(b);
		if (u == v)return false;
		if (sz[u] < sz[v])swap(u,v);
		parent[v] = u;
		sz[u]+=sz[v];
		sz[v] = 0;
		while(!comp[v].empty()){
			comp[u].push_back(comp[v].back());
			comp[v].pop_back();
		}
		return true;
	};
};
 
int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	std::vector<std::vector<int>> answer(n,vector<int>(n,0));
	for (int i = 0;i<n;++i){
		for (int j = 0;j<n;++j){
			if (p[i][j] == 3)return 0;
		}
	}
	DSU st;
	st.build(n);
	for (int i = 0;i<n;++i){
		for (int j = i + 1;j<n;++j){
			if (p[i][j]==1){
				int u = st.findsets(i);
				int v = st.findsets(j);
				answer[u][v] = true;
				answer[v][u] = true;
				st.unionset(i,j);
			}
		}
	}
	for (int i = 0;i<n;++i){
		for (int j = i + 1;j<n;++j){
			if (st.findsets(i) == st.findsets(j)){
				st.unionset(i,j);
			}
		}
	}
	for (int i = 0;i<n;++i){
		for (int j = i + 1;j<n;++j){
			if (p[i][j]){
				answer[i][j] = true;
				answer[j][i] = true;
				st.unionset(i,j);
				int y = st.findsets(i);
				for (auto x:st.comp[y]){
					p[i][x]--;
					p[j][x]--;
					p[x][i]--;
					p[x][j]--;
				}
			}
		}
	}
	for (int i = 0;i<n;++i){
		for (int j = 0;j < n;++j){
			if (i == j)continue;
			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...