답안 #434672

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
434672 2021-06-21T14:25:28 Z pere_gil 슈퍼트리 잇기 (IOI20_supertrees) C++14
0 / 100
1 ms 204 KB
#include "supertrees.h"
#include "bits/stdc++.h"
using namespace std;

#define vvi vector<vector<int> >

void pri_graph(vvi &v){
	for(int i=0;i<v.size();i++)
		for(int j=i;j<v.size();j++)
			if(v[i][j]) printf("%d <-> %d\n",i,j);
	printf("\n");
}

void init(int n, vector<int> &p){
	for(int i=0;i<n;i++) p[i]=i;
}

int find_set(int n, vector<int> &p){
	return (n==p[n]) ? n : p[n]=find_set(p[n],p);
}

bool same_set(int a, int b, vector<int> &p){
	return find_set(a,p)==find_set(b,p);
}

void union_set(int a, int b, vector<int> &p){
	if(!same_set(a,b,p)) p[find_set(a,p)]=find_set(b,p);
}

vector<int> to_vector(unordered_set<int> &s){
	unordered_set<int> :: iterator it;
	vector<int> v;
	for(it=s.begin();it!=s.end();it++)
		v.push_back(*it);
	return v;
}

void join_1(vector<int> &g, vvi &res){
	for(int i=1;i<g.size();i++)
		res[g[i-1]][g[i]]=res[g[i]][g[i-1]]=1;
}

bool join_2(vector<int> &g, vvi &res){
	if(g.size()<3) return false;
	for(int i=0;i<g.size();i++){
		int j=(i+1)%g.size();
		res[g[i]][g[j]]=res[g[j]][g[i]]=1;
	}
	return true;
}

bool make_group(int n, vector<int> &g, vvi &v, vvi &res){
	vector<int> p(n);
	init(n,p);

	unordered_set<int> mid;
	for(int i=0;i<g.size();i++){
		int aux=0;
		for(int j=0;j<g.size();j++)
			if(v[g[i]][g[j]]==1) union_set(g[i],g[j],p);
			else aux++;
		if(aux==g.size()-1) mid.insert(g[i]);
	}
	vvi one(n);
	for(int i=0;i<g.size();i++){
		if(mid.find(g[i])==mid.end())
			one[find_set(g[i],p)].push_back(g[i]);
		if(g[i]==find_set(g[i],p)) mid.insert(g[i]);
	}
	vector<int> v_mid=to_vector(mid);

	/*
	printf("The middle: ");
	for(int i=0;i<v_mid.size();i++) printf("%d ",v_mid[i]);
	printf("\nGroup of one\n");
	for(int i=0;i<n;i++){
		if(!one[i].size()) continue;
		for(int j=0;j<one[i].size();j++) printf("%d ",one[i][j]);
		printf("\n");
	}
	*/

	for(int i=0;i<n;i++)
		if(one[i].size()) join_1(one[i],res);
	return join_2(v_mid,res);
}

int construct(vvi v){
	int n=v.size();

	vector<int> p(n);
	init(n,p);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if(v[i][j]) union_set(i,j,p);

    vvi g(n);
	for(int i=0;i<n;i++)
		g[find_set(i,p)].push_back(i);

	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if(same_set(i,j,p) && v[i][j]==0) return 0;

	vvi res(n,vector<int> (n,0));
	for(int i=0;i<n;i++)
		if(g[i].size()>0)
			if(!make_group(n,g[i],v,res)) return 0;
	
	//pri_graph(res);
	
	build(res);
	return 1;
}

Compilation message

supertrees.cpp: In function 'void pri_graph(std::vector<std::vector<int> >&)':
supertrees.cpp:8:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 |  for(int i=0;i<v.size();i++)
      |              ~^~~~~~~~~
supertrees.cpp:9:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 |   for(int j=i;j<v.size();j++)
      |               ~^~~~~~~~~
supertrees.cpp: In function 'void join_1(std::vector<int>&, std::vector<std::vector<int> >&)':
supertrees.cpp:39:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |  for(int i=1;i<g.size();i++)
      |              ~^~~~~~~~~
supertrees.cpp: In function 'bool join_2(std::vector<int>&, std::vector<std::vector<int> >&)':
supertrees.cpp:45:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |  for(int i=0;i<g.size();i++){
      |              ~^~~~~~~~~
supertrees.cpp: In function 'bool make_group(int, std::vector<int>&, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:57:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |  for(int i=0;i<g.size();i++){
      |              ~^~~~~~~~~
supertrees.cpp:59:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   for(int j=0;j<g.size();j++)
      |               ~^~~~~~~~~
supertrees.cpp:62:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |   if(aux==g.size()-1) mid.insert(g[i]);
      |      ~~~^~~~~~~~~~~~
supertrees.cpp:65:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |  for(int i=0;i<g.size();i++){
      |              ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 204 KB Answer gives possible 0 while actual possible 1
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 204 KB Answer gives possible 0 while actual possible 1
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Answer gives possible 0 while actual possible 1
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 204 KB Answer gives possible 0 while actual possible 1
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 204 KB Answer gives possible 0 while actual possible 1
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 204 KB Answer gives possible 0 while actual possible 1
2 Halted 0 ms 0 KB -