Submission #572169

#TimeUsernameProblemLanguageResultExecution timeMemory
572169fuad27Connecting Supertrees (IOI20_supertrees)C++17
46 / 100
182 ms22044 KiB
#include "supertrees.h"
#include <vector>
#include<bits/stdc++.h>
using namespace std;

struct DSU {
	vector<int> e;
	DSU(int N) { e = vector<int>(N, -1); }
	int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
	bool same_set(int a, int b) { return get(a) == get(b); }
	int size(int x) { return -e[get(x)]; }
	bool unite(int x, int y) {
		x = get(x), y = get(y);
		if (x == y) return false;
		if (e[x] > e[y]) swap(x, y);
		e[x] += e[y]; e[y] = x; return true;
	}
};


int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	DSU dsu1(n+1);
	for(int i = 0;i<n;i++) {
		for(int j = 0;j<n;j++) {
			if(p[i][j] == 3 or p[i][j]!=p[j][i])return 0;
			if(p[i][j])dsu1.unite(i,j);
		}
	}
	for(int i = 0;i<n;i++) {
		for(int j= 0;j<n;j++) {
			if((p[i][j]>0) and (dsu1.same_set(i,j)==0))return 0;
		}
	}	
	vector<vector<int>> ans(n,vector<int> (n,0));
	{
		vector<bool> vis(n);
		vector<int> t;
		for(int i = 0;i<n;i++) {
			if(vis[i])continue;
			vector<int> cur;
			for(int j = 0;j<n;j++) {
				if(p[i][j] == 1) {
					cur.push_back(j);
					vis[j]=1;
				}
			}
			for(int j = 0;j<(int)cur.size()-1;j++) {
				ans[cur[j]][cur[j+1]]=1;
				ans[cur[j+1]][cur[j]]=1;
			}
			t.push_back(i);
		}
		for(int i = 0;i<n;i++)vis[i]=0;
		vector<pair<int,int>> cur;
		vector<int> beg;
		for(int j:t) {
			bool flag = 0;
			for(pair<int,int> &i:cur) {
				if(p[j][i.first] == 2) {
					ans[i.first][j] = ans[j][i.first] = 1;
					i.first=j;
					i.second++;
					flag=1;
					break;
				}
			}
			if(!flag){
				cur.push_back(make_pair(j,1));
				beg.push_back(j);
			}
		}
		for(int i = 0;i<(int)cur.size();i++) {
			if(cur[i].first!=beg[i] and p[cur[i].first][beg[i]] == 1)return 0;
			if(cur[i].first!=beg[i]) {
				ans[cur[i].first][beg[i]] = ans[beg[i]][cur[i].first] = 1;
			}
			if(cur[i].second == 2)return 0;
		}
	}
	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...