This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
typedef long long ll;
const int N = 1005;
int n;
int p[N][N];
int linija(){
vector <vector<int>> ans;
for(int i = 1; i <= n; i++){
vector <int> fg;
for(int j = 1; j <= n; j++){
if(j == i+1 || j == i-1) fg.push_back(1);
else fg.push_back(0);
}
ans.push_back(fg);
}
build(ans);
return 1;
}
bool visited[N];
int komp[N];
int cnt = 1;
vector <int> sad;
void dfs(int x){
visited[x] = 1;
komp[x] = cnt;
sad.push_back(x);
for(int j = 1; j <= n; j++){
if(p[x][j] && !visited[j]) dfs(j);
}
}
int drvo(){
vector <vector<int>> ans;
for(int i = 0; i < n; i++){
vector <int> sta(n,0);
ans.push_back(sta);
}
for(int i = 1; i <= n; i++){
if(visited[i]) continue;
sad.clear();
dfs(i);
for(int j = 0; j < sad.size()-1; j++){
ans[sad[j]-1][sad[j+1]-1] = ans[sad[j+1]-1][sad[j]-1] = 1;
}
cnt++;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(komp[i] == komp[j] && !p[i][j]) return 0;
}
}
build(ans);
return 1;
}
int krugovi(){
vector <vector<int>> ans;
for(int i = 0; i < n; i++){
vector <int> sta(n,0);
ans.push_back(sta);
}
for(int i = 1; i <= n; i++){
if(visited[i]) continue;
sad.clear();
dfs(i);
if(sad.size() == 2) return 0;
for(int j = 0; j < sad.size()-1; j++){
ans[sad[j]-1][sad[j+1]-1] = ans[sad[j+1]-1][sad[j]-1] = 1;
}
if(sad.size() > 1) ans[sad.back()-1][sad[0]-1] = ans[sad[0]-1][sad.back()-1] = 1;
cnt++;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(komp[i] == komp[j] && !p[i][j]) return 0;
}
}
build(ans);
return 1;
}
int pre[N],bio[N],biopre[N],komstb[N],rut[N];
int brs = 1;
bool nevalja = 0;
vector <int> stablo;
void dfs1(int x){
bio[x] = 1;
komstb[x] = brs;
stablo.push_back(x);
for(int h = 1; h <= n; h++){
if(p[x][h] == 1){
if(pre[x] || biopre[x]){
nevalja = 1;
return;
}
if(!bio[h]) dfs1(h);
}
}
}
vector <vector<int>> stb[N];
int dodva(){
vector <vector<int>> ans;
for(int i = 0; i < n; i++){
vector <int> sta(n,0);
ans.push_back(sta);
}
for(int k = 1; k <= n; k++){
for(int i = 1; i <= n; i++){
if(p[i][k] == 1) continue;
for(int j = 1; j <= n; j++){
if(p[i][j] == 1 && p[j][k] == 1/* && p[i][k] != 1*/) return 0;
}
}
}
for(int i = 1; i <= n; i++){
if(visited[i]) continue;
sad.clear();
for(int j = 1; j <= n; j++) pre[j] = visited[j];
dfs(i);
/// resi komponentu
stb[cnt].push_back({0});
for(auto f : sad) bio[f] = komstb[f] = 0;
brs = 1;
for(auto u : sad){
if(bio[u]){
continue;
}
stablo.clear();
nevalja = 0;
dfs1(u);
if(nevalja) return 0;
for(auto h : stablo) biopre[h] = 1;
stb[cnt].push_back(stablo);
rut[brs] = u;
brs++;
}
for(int j = 1; j < brs; j++){
for(int h = 0; h < stb[cnt][j].size()-1; h++){
ans[stb[cnt][j][h]-1][stb[cnt][j][h+1]-1] = ans[stb[cnt][j][h+1]-1][stb[cnt][j][h]-1] = 1;
}
if(j < brs-1) ans[rut[j]-1][rut[j+1]-1] = ans[rut[j+1]-1][rut[j]-1] = 1;
}
if(brs-1 >= 2) ans[rut[brs-1]-1][rut[1]-1] = ans[rut[1]-1][rut[brs-1]-1] = 1;
cnt++;
}
for(int i = 1; i < cnt; i++){
for(int j = 1; j < stb[cnt].size(); j++){
for(auto u : stb[cnt][j]){
for(auto v : stb[cnt][j]){
if(p[u][v] != 1) return 0;
}
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(komstb[i] != komstb[j] && p[i][j] != 2) return 0;
}
}
build(ans);
return 1;
}
int construct(vector <vector<int>> nz){
n = nz.size();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
p[i][j] = nz[i-1][j-1];
}
}
bool prvi = 0;
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) prvi |= (p[i][j] != 1);
if(!prvi){
return linija();
}
bool drugi = 1;
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(p[i][j] >= 2) drugi = 0;
if(drugi){
return drvo();
}
bool treci = 1;
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(i != j && p[i][j] != 0 && p[i][j] != 2) treci = 0;
if(treci){
return krugovi();
}
return dodva();
}
Compilation message (stderr)
supertrees.cpp: In function 'int drvo()':
supertrees.cpp:51:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int j = 0; j < sad.size()-1; j++){
| ~~^~~~~~~~~~~~~~
supertrees.cpp: In function 'int krugovi()':
supertrees.cpp:79:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int j = 0; j < sad.size()-1; j++){
| ~~^~~~~~~~~~~~~~
supertrees.cpp: In function 'int dodva()':
supertrees.cpp:160:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
160 | for(int h = 0; h < stb[cnt][j].size()-1; h++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:171:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
171 | for(int j = 1; j < stb[cnt].size(); j++){
| ~~^~~~~~~~~~~~~~~~~
# | 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... |