#include "supertrees.h"
#include<bits/stdc++.h>
using namespace std;
const int N=1001;
int pa1[N],pa2[N],compo1[N],compo2[N];
bool vis1[N],vis2[N],way[N];
vector<int>adj[N],adj1[N],adj2[N],root1,root2;
int dsu1(int x){
if(pa1[x]==x)return x;
else return pa1[x]=dsu1(pa1[x]);
}
int dsu2(int x){
if(pa2[x]==x)return x;
else return pa2[x]=dsu2(pa2[x]);
}
void dfs(int u,int p){
way[u]++;
if(way[u]>1)return ;
for(int v:adj[u]){
if(v!=p)dfs(v,u);
}
}
int construct(vector<vector<int>> p) {
int n=p.size();
vector<vector<int>> answer;
for (int i = 0; i < n; i++) {
vector<int> row;
row.resize(n);
answer.push_back(row);
pa1[i]=i;
pa2[i]=i;
}
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(p[i][j]==3)return 0;
if(p[i][j]==1)pa1[dsu1(j)]=dsu1(i);
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(i!=j&&dsu1(i)==dsu1(j)){
if(p[i][j]==0||p[i][j]==2)return 0;
adj1[i].push_back(j);
adj1[j].push_back(i);
}
}
}
for(int i=0; i<n; i++){
if(vis1[i])continue;
root1.push_back(i);
compo1[i]=i;
for(int j:adj1[i]){
compo1[j]=i;
vis1[j]=true;
answer[i][j]=1;
answer[j][i]=1;
}
}
for(int i=0; i<root1.size(); i++){
for(int j=i+1; j<root1.size(); j++){
int a=root1[i],b=root1[j];
if(p[a][b]==2)pa2[dsu2(a)]=dsu2(b);
}
}
for(int i:root1){
for(int j:root1){
if(i!=j&&dsu2(i)==dsu2(j)){
if(p[i][j]==0||p[i][j]==1)return 0;
adj2[i].push_back(j);
adj2[j].push_back(i);
}
}
}
for(int i:root1){
if(vis2[i])continue;
root2.push_back(i);
compo2[i]=i;
for(int j:adj2[i]){
compo2[j]=i;
vis2[j]=true;
}
}
for(int i=0; i<n; i++){
for(int j:root2){
if(compo1[i]==compo1[j])compo2[i]=j;
}
}
for(int i:root2){
for(int j=1; j<adj2[i].size(); j++){
answer[adj2[i][j]][adj2[i][j-1]]=1;
answer[adj2[i][j-1]][adj2[i][j]]=1;
}
if(adj2[i].empty())continue;
answer[adj2[i][0]][i]=1;
answer[i][adj2[i][0]]=1;
answer[adj2[i][adj2[i].size()-1]][i]=1;
answer[i][adj2[i][adj2[i].size()-1]]=1;
}
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(answer[i][j]){
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++)way[j]=0;
dfs(i,-1);
for(int j=0; j<n; j++)if(way[j]!=p[i][j])return 0;
}
build(answer);
return 1;
}
/*
6
1 2 2 0 0 1
2 1 2 1 0 0
2 2 2 0 1 0
0 1 0 1 2 2
0 0 1 2 1 2
1 0 0 2 2 1
*/
Compilation message
supertrees.cpp: In function 'void dfs(int, int)':
supertrees.cpp:21:10: warning: use of an operand of type 'bool' in 'operator++' is deprecated [-Wdeprecated]
21 | way[u]++;
| ~~~~~^
supertrees.cpp:22:14: warning: comparison of constant '1' with boolean expression is always false [-Wbool-compare]
22 | if(way[u]>1)return ;
| ~~~~~~^~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:64:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i=0; i<root1.size(); i++){
| ~^~~~~~~~~~~~~
supertrees.cpp:65:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int j=i+1; j<root1.size(); j++){
| ~^~~~~~~~~~~~~
supertrees.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int j=1; j<adj2[i].size(); j++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1628 KB |
Output is correct |
7 |
Correct |
166 ms |
30288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1628 KB |
Output is correct |
7 |
Correct |
166 ms |
30288 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
159 ms |
22116 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
70 ms |
12116 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
39 ms |
5980 KB |
Output is correct |
21 |
Correct |
155 ms |
23120 KB |
Output is correct |
22 |
Correct |
157 ms |
22352 KB |
Output is correct |
23 |
Correct |
175 ms |
26204 KB |
Output is correct |
24 |
Correct |
166 ms |
22352 KB |
Output is correct |
25 |
Correct |
57 ms |
12372 KB |
Output is correct |
26 |
Correct |
57 ms |
12112 KB |
Output is correct |
27 |
Correct |
174 ms |
28240 KB |
Output is correct |
28 |
Correct |
158 ms |
22096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Execution timed out |
1178 ms |
1580576 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
1176 ms |
1665036 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1628 KB |
Output is correct |
7 |
Correct |
166 ms |
30288 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
159 ms |
22116 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
70 ms |
12116 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
39 ms |
5980 KB |
Output is correct |
21 |
Correct |
155 ms |
23120 KB |
Output is correct |
22 |
Correct |
157 ms |
22352 KB |
Output is correct |
23 |
Correct |
175 ms |
26204 KB |
Output is correct |
24 |
Correct |
166 ms |
22352 KB |
Output is correct |
25 |
Correct |
57 ms |
12372 KB |
Output is correct |
26 |
Correct |
57 ms |
12112 KB |
Output is correct |
27 |
Correct |
174 ms |
28240 KB |
Output is correct |
28 |
Correct |
158 ms |
22096 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Execution timed out |
1178 ms |
1580576 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1628 KB |
Output is correct |
7 |
Correct |
166 ms |
30288 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
159 ms |
22116 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
70 ms |
12116 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
39 ms |
5980 KB |
Output is correct |
21 |
Correct |
155 ms |
23120 KB |
Output is correct |
22 |
Correct |
157 ms |
22352 KB |
Output is correct |
23 |
Correct |
175 ms |
26204 KB |
Output is correct |
24 |
Correct |
166 ms |
22352 KB |
Output is correct |
25 |
Correct |
57 ms |
12372 KB |
Output is correct |
26 |
Correct |
57 ms |
12112 KB |
Output is correct |
27 |
Correct |
174 ms |
28240 KB |
Output is correct |
28 |
Correct |
158 ms |
22096 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Execution timed out |
1178 ms |
1580576 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |