답안 #369737

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
369737 2021-02-22T10:36:05 Z MilosMilutinovic 슈퍼트리 잇기 (IOI20_supertrees) C++14
0 / 100
1 ms 492 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

const int mxN=1e3;

int r[mxN], sz[mxN];
vector<int> g[mxN];

int find(int x) {
	return x^r[x]?r[x]=find(r[x]):x;
}

void join(int x, int y) {
	x=find(x),y=find(y);
	if(x==y)
		return;
	if(sz[x]>sz[y])
		swap(x,y);
	r[x]=y;
	sz[y]+=sz[x];
}

int construct(vector<vector<int>> p) {
	int n=(int)p.size();
	for(int i=0; i<n; ++i)
		for(int j=0; j<n; ++j)
			if(p[i][j]==3)
				return 0;
	iota(r, r+n, 0);
	memset(sz, 1, sizeof(sz));
	for(int i=0; i<n; ++i)
		for(int j=i+1; j<n; ++j)
			if(p[i][j]==1)
				join(i,j);
	vector<vector<int>> ans(n, vector<int>(n));
	vector<int> roots;
	for(int i=0; i<n; ++i) {
		int j=find(i);
		if(j==i)
			roots.pb(i);
		else
			ans[i][j]=ans[j][i]=1;
	}
	for(int i=0; i<n; ++i)
		for(int j=0; j<n; ++j)
			if(p[i][j]!=p[find(i)][find(j)])
				return 0;
	iota(r, r+n, 0);
	memset(sz, 1, sizeof(sz));
	for(int i:roots)
		for(int j:roots)
			if(p[i][j]==2)
				join(i,j);
	for(int i:roots)
		if(sz[find(i)]>=2)
			g[find(i)].pb(i);
	for(int i=0; i<n; ++i) {
		//if(!g[i].empty()&&(int)g[i].size()<=2)
		//	return 0;
		assert(g[i].empty());
		if(g[i].empty())
			continue;
		for(int x:g[i]) {
			for(int y:g[i]) {
				if(x!=y&&p[x][y]!=2)
					return 0;
			}
		}
		for(int j=1;j<(int)g[i].size();j++) {
			int x=g[i][j],y=g[i][j-1];
			ans[x][y]=ans[y][x]=1;
		}
		int x=g[i][0],y=g[i].back();
		ans[x][y]=ans[y][x]=1;
	}
	build(ans);
	return 1;
}

# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 492 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 492 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 492 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 492 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 492 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 492 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -