이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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()==1) return true;
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;
}
컴파일 시 표준 에러 (stderr) 메시지
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:46:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | 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:58:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i=0;i<g.size();i++){
| ~^~~~~~~~~
supertrees.cpp:60:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int j=0;j<g.size();j++)
| ~^~~~~~~~~
supertrees.cpp:63:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | if(aux==g.size()-1) mid.insert(g[i]);
| ~~~^~~~~~~~~~~~
supertrees.cpp:66:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int i=0;i<g.size();i++){
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |