#include<bits/stdc++.h>
#include "supertrees.h"
#include <vector>
#define fi first
#define se second
#define pb push_back
using namespace std;
int n;const int N=1009;
vector<vector<int>>ans;
int pa[N],nw[N],s=0,sz[N],p[N][N],vis[N];
vector<int>v[N];vector<int>sta;
vector<int>u;
void merge(int x,int y)
{
x=pa[x];y=pa[y];
if(x==y)return;
if(sz[x]<sz[y])swap(x,y);
for(auto it:v[y])
{
sz[x]++;
v[x].pb(it);
pa[it]=x;
}
}vector<int>h;
int go(int x,int y)
{
h.clear();
sta.clear();int last=x;h.pb(x);
for(auto it:v[x])sta.pb(it);
nw[x]=++s;int three=0;
for(int j=y+1;j<u.size();j++)
{
int i=u[j];
if(vis[i])continue;
int r=1,re=0;
for(auto it:v[i])
{
for(auto pp:sta)
{
if(p[it][pp]==3)three++;
if(p[it][pp]!=2&&p[it][pp]!=3)r=0;
}
}
if(r==0)continue;
vis[i]=1;nw[i]=s;h.pb(i);
for(auto it:v[i])sta.pb(it);
ans[last][i]=1;
ans[i][last]=1;
last=i;
}
if(ans[x][last])return 0;
if(x==last)return 1;
long long al=u.size();al*=al;
if(three>0&&three!=al)return 0;
if(three&&h.size()<4)return 0;
if(three){ans[h[1]][h[3]]=1;
ans[h[3]][h[1]]=1;}
ans[x][last]=1;
ans[last][x]=1;
return 1;
}
vector<int>w;
int construct(vector<vector<int>> P) {
n = P.size();
for(int i=0;i<n;i++)
{
pa[i]=i;sz[i]=1;v[i].pb(i);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)continue;
p[i][j]=P[i][j];
if(p[i][j]==1)merge(i,j);
}
}
for(int i=0;i<n;i++)w.pb(0);
for(int i=0;i<n;i++)
{
ans.pb(w);
}
for(int i=0;i<n;i++)
{
if(pa[i]!=i)continue;
for(auto it:v[i])
{
if(it==i)continue;
ans[i][it]=1;
ans[it][i]=1;
}
for(auto it:v[i])
{
for(auto pp:v[i])
{
if(it==pp)continue;
if(p[it][pp]!=1)return 0;
}
}
u.pb(i);
}
for(int i=0;i<u.size();i++)
{
int it=u[i];
if(vis[it])continue;
vis[it]=1;
int z=go(it,i);
if(z==0)return 0;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)continue;
int x=nw[pa[i]],y=nw[pa[j]];
if(x!=y&&p[i][j]==2||p[i][j]==3)return 0;
}
}
build(ans);return 1;
}
Compilation message
supertrees.cpp: In function 'int go(int, int)':
supertrees.cpp:31:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int j=y+1;j<u.size();j++)
| ~^~~~~~~~~
supertrees.cpp:35:17: warning: unused variable 're' [-Wunused-variable]
35 | int r=1,re=0;
| ^~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i=0;i<u.size();i++)
| ~^~~~~~~~~
supertrees.cpp:116:20: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
116 | if(x!=y&&p[i][j]==2||p[i][j]==3)return 0;
| ~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
13 ms |
1980 KB |
Output is correct |
7 |
Correct |
255 ms |
26092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
13 ms |
1980 KB |
Output is correct |
7 |
Correct |
255 ms |
26092 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
11 ms |
1996 KB |
Output is correct |
13 |
Correct |
242 ms |
25956 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
1612 KB |
Output is correct |
17 |
Correct |
110 ms |
16168 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
67 ms |
7712 KB |
Output is correct |
21 |
Correct |
243 ms |
26024 KB |
Output is correct |
22 |
Correct |
286 ms |
25964 KB |
Output is correct |
23 |
Correct |
242 ms |
25996 KB |
Output is correct |
24 |
Correct |
227 ms |
26076 KB |
Output is correct |
25 |
Correct |
105 ms |
16068 KB |
Output is correct |
26 |
Correct |
95 ms |
16128 KB |
Output is correct |
27 |
Correct |
258 ms |
26080 KB |
Output is correct |
28 |
Correct |
227 ms |
26012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
13 ms |
2008 KB |
Output is correct |
9 |
Correct |
260 ms |
26088 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
10 ms |
1988 KB |
Output is correct |
13 |
Correct |
271 ms |
26084 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
1612 KB |
Output is correct |
17 |
Correct |
106 ms |
16168 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
66 ms |
7688 KB |
Output is correct |
22 |
Correct |
228 ms |
26052 KB |
Output is correct |
23 |
Correct |
242 ms |
26080 KB |
Output is correct |
24 |
Correct |
298 ms |
26024 KB |
Output is correct |
25 |
Correct |
101 ms |
16156 KB |
Output is correct |
26 |
Correct |
107 ms |
16164 KB |
Output is correct |
27 |
Correct |
274 ms |
26008 KB |
Output is correct |
28 |
Correct |
282 ms |
26008 KB |
Output is correct |
29 |
Correct |
102 ms |
16152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
68 ms |
7744 KB |
Output is correct |
5 |
Correct |
232 ms |
26020 KB |
Output is correct |
6 |
Correct |
233 ms |
26072 KB |
Output is correct |
7 |
Correct |
268 ms |
25984 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
75 ms |
7728 KB |
Output is correct |
10 |
Correct |
259 ms |
26052 KB |
Output is correct |
11 |
Correct |
231 ms |
26052 KB |
Output is correct |
12 |
Correct |
248 ms |
26044 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
67 ms |
7736 KB |
Output is correct |
17 |
Correct |
250 ms |
26024 KB |
Output is correct |
18 |
Correct |
259 ms |
26004 KB |
Output is correct |
19 |
Correct |
270 ms |
26068 KB |
Output is correct |
20 |
Correct |
269 ms |
25980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
13 ms |
1980 KB |
Output is correct |
7 |
Correct |
255 ms |
26092 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
11 ms |
1996 KB |
Output is correct |
13 |
Correct |
242 ms |
25956 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
1612 KB |
Output is correct |
17 |
Correct |
110 ms |
16168 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
67 ms |
7712 KB |
Output is correct |
21 |
Correct |
243 ms |
26024 KB |
Output is correct |
22 |
Correct |
286 ms |
25964 KB |
Output is correct |
23 |
Correct |
242 ms |
25996 KB |
Output is correct |
24 |
Correct |
227 ms |
26076 KB |
Output is correct |
25 |
Correct |
105 ms |
16068 KB |
Output is correct |
26 |
Correct |
95 ms |
16128 KB |
Output is correct |
27 |
Correct |
258 ms |
26080 KB |
Output is correct |
28 |
Correct |
227 ms |
26012 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
13 ms |
2008 KB |
Output is correct |
37 |
Correct |
260 ms |
26088 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
10 ms |
1988 KB |
Output is correct |
41 |
Correct |
271 ms |
26084 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
5 ms |
1612 KB |
Output is correct |
45 |
Correct |
106 ms |
16168 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
0 ms |
332 KB |
Output is correct |
49 |
Correct |
66 ms |
7688 KB |
Output is correct |
50 |
Correct |
228 ms |
26052 KB |
Output is correct |
51 |
Correct |
242 ms |
26080 KB |
Output is correct |
52 |
Correct |
298 ms |
26024 KB |
Output is correct |
53 |
Correct |
101 ms |
16156 KB |
Output is correct |
54 |
Correct |
107 ms |
16164 KB |
Output is correct |
55 |
Correct |
274 ms |
26008 KB |
Output is correct |
56 |
Correct |
282 ms |
26008 KB |
Output is correct |
57 |
Correct |
102 ms |
16152 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
5 ms |
1612 KB |
Output is correct |
61 |
Correct |
125 ms |
16152 KB |
Output is correct |
62 |
Correct |
1 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
65 |
Correct |
59 ms |
7704 KB |
Output is correct |
66 |
Correct |
97 ms |
16168 KB |
Output is correct |
67 |
Correct |
118 ms |
16168 KB |
Output is correct |
68 |
Correct |
119 ms |
16156 KB |
Output is correct |
69 |
Correct |
95 ms |
16152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
13 ms |
1980 KB |
Output is correct |
7 |
Correct |
255 ms |
26092 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
11 ms |
1996 KB |
Output is correct |
13 |
Correct |
242 ms |
25956 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
1612 KB |
Output is correct |
17 |
Correct |
110 ms |
16168 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
67 ms |
7712 KB |
Output is correct |
21 |
Correct |
243 ms |
26024 KB |
Output is correct |
22 |
Correct |
286 ms |
25964 KB |
Output is correct |
23 |
Correct |
242 ms |
25996 KB |
Output is correct |
24 |
Correct |
227 ms |
26076 KB |
Output is correct |
25 |
Correct |
105 ms |
16068 KB |
Output is correct |
26 |
Correct |
95 ms |
16128 KB |
Output is correct |
27 |
Correct |
258 ms |
26080 KB |
Output is correct |
28 |
Correct |
227 ms |
26012 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
13 ms |
2008 KB |
Output is correct |
37 |
Correct |
260 ms |
26088 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
10 ms |
1988 KB |
Output is correct |
41 |
Correct |
271 ms |
26084 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
5 ms |
1612 KB |
Output is correct |
45 |
Correct |
106 ms |
16168 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
0 ms |
332 KB |
Output is correct |
49 |
Correct |
66 ms |
7688 KB |
Output is correct |
50 |
Correct |
228 ms |
26052 KB |
Output is correct |
51 |
Correct |
242 ms |
26080 KB |
Output is correct |
52 |
Correct |
298 ms |
26024 KB |
Output is correct |
53 |
Correct |
101 ms |
16156 KB |
Output is correct |
54 |
Correct |
107 ms |
16164 KB |
Output is correct |
55 |
Correct |
274 ms |
26008 KB |
Output is correct |
56 |
Correct |
282 ms |
26008 KB |
Output is correct |
57 |
Correct |
102 ms |
16152 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
0 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
68 ms |
7744 KB |
Output is correct |
62 |
Correct |
232 ms |
26020 KB |
Output is correct |
63 |
Correct |
233 ms |
26072 KB |
Output is correct |
64 |
Correct |
268 ms |
25984 KB |
Output is correct |
65 |
Correct |
1 ms |
332 KB |
Output is correct |
66 |
Correct |
75 ms |
7728 KB |
Output is correct |
67 |
Correct |
259 ms |
26052 KB |
Output is correct |
68 |
Correct |
231 ms |
26052 KB |
Output is correct |
69 |
Correct |
248 ms |
26044 KB |
Output is correct |
70 |
Correct |
1 ms |
332 KB |
Output is correct |
71 |
Correct |
1 ms |
376 KB |
Output is correct |
72 |
Correct |
1 ms |
332 KB |
Output is correct |
73 |
Correct |
67 ms |
7736 KB |
Output is correct |
74 |
Correct |
250 ms |
26024 KB |
Output is correct |
75 |
Correct |
259 ms |
26004 KB |
Output is correct |
76 |
Correct |
270 ms |
26068 KB |
Output is correct |
77 |
Correct |
269 ms |
25980 KB |
Output is correct |
78 |
Correct |
1 ms |
332 KB |
Output is correct |
79 |
Correct |
1 ms |
332 KB |
Output is correct |
80 |
Correct |
5 ms |
1612 KB |
Output is correct |
81 |
Correct |
125 ms |
16152 KB |
Output is correct |
82 |
Correct |
1 ms |
332 KB |
Output is correct |
83 |
Correct |
1 ms |
332 KB |
Output is correct |
84 |
Correct |
1 ms |
332 KB |
Output is correct |
85 |
Correct |
59 ms |
7704 KB |
Output is correct |
86 |
Correct |
97 ms |
16168 KB |
Output is correct |
87 |
Correct |
118 ms |
16168 KB |
Output is correct |
88 |
Correct |
119 ms |
16156 KB |
Output is correct |
89 |
Correct |
95 ms |
16152 KB |
Output is correct |
90 |
Correct |
1 ms |
332 KB |
Output is correct |
91 |
Correct |
1 ms |
364 KB |
Output is correct |
92 |
Correct |
5 ms |
1508 KB |
Output is correct |
93 |
Correct |
124 ms |
16168 KB |
Output is correct |
94 |
Correct |
1 ms |
332 KB |
Output is correct |
95 |
Correct |
1 ms |
332 KB |
Output is correct |
96 |
Correct |
1 ms |
332 KB |
Output is correct |
97 |
Correct |
27 ms |
5280 KB |
Output is correct |
98 |
Correct |
99 ms |
16164 KB |
Output is correct |
99 |
Correct |
114 ms |
16152 KB |
Output is correct |
100 |
Correct |
110 ms |
16160 KB |
Output is correct |
101 |
Correct |
126 ms |
16068 KB |
Output is correct |
102 |
Correct |
100 ms |
16168 KB |
Output is correct |
103 |
Correct |
113 ms |
16168 KB |
Output is correct |
104 |
Correct |
115 ms |
16164 KB |
Output is correct |
105 |
Correct |
102 ms |
16172 KB |
Output is correct |