#include "supertrees.h"
#include <vector>
#include<bits/stdc++.h>
using namespace std;
using ll = int;
struct dsu{
ll n;
vector<ll> p;
dsu(ll N):n(N),p(N,-1){}
ll g(ll i){
return p[i]<0?i:p[i]=g(p[i]);
}
bool e(ll a, ll b){
return g(a)==g(b);
}
bool m(ll a, ll b){
a = g(a);
b = g(b);
if(a==b) return false;
if (p[a]>p[b]) swap(a,b);
p[a] += p[b];
p[b] = a;
return true;
}
};
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
vector<vector<ll>> ans(n,vector<ll>(n,0));
dsu d(n);
for(ll i = 0;i<n;i++) for(ll j = 0;j<n;j++){
if (p[i][j]==3) return 0;
if (p[i][j]) d.m(i,j);
}
for(ll i = 0;i<n;i++) for(ll j = 0;j<n;j++){
if (!p[i][j]&&d.e(i,j)) return 0;
}
vector<ll> g;
for(ll i = 0;i<n;i++) if(d.g(i)==i) g.push_back(i);
ll gc = g.size();
vector<vector<ll>> gs(gc);
for(ll i = 0;i<n;i++) gs[lower_bound(g.begin(),g.end(),d.g(i))-g.begin()].push_back(i);
for(auto &o : gs) if(o.size()>1){
ll ty = 3;
ll is_1 = 1, is_2 = 1;
for(ll i : o) for(ll j : o) if(i!=j){
if (p[i][j]!=1) is_1=0;
if (p[i][j]!=2) is_2=0;
}
if(is_1) ty=1;
if(is_2) ty=2;
//cerr << "type=" << ty << endl;
if (ty!=3){
for(ll i = 1;i<o.size();i++) ans[o[i-1]][o[i]] = ans[o[i]][o[i-1]] = 1;
if (ty == 2){
if (o.size()==2) return 0;
ans[o.front()][o.back()] = ans[o.back()][o.front()] = 1;
}
}else{
ll m = o.size();
dsu d(m);
for(ll i = 0;i<m;i++)for(ll j = 0;j<m;j++) if(i!=j){
if (p[o[i]][o[j]]==1) {
//cerr << "gp " << i << " " << j << endl;
d.m(i,j);
}
}
for(ll i = 0;i<m;i++)for(ll j = 0;j<m;j++) if(i!=j){
if (d.e(i,j)&&p[o[i]][o[j]]!=1) return 0;
}
vector<ll> c;
for(ll i = 0;i<m;i++) if(d.g(i)==i) c.push_back(i);
ll cc = c.size();
//cerr << "cc=" << cc << endl;
if (cc<3) return 0;
vector<vector<ll>> cs(cc);
for(ll i = 0;i<m;i++) cs[lower_bound(c.begin(),c.end(),d.g(i))-c.begin()].push_back(o[i]);
/*for(ll i = 0;i<cc;i++){
cerr << "cg " << i << ":";
for(ll j : cs[i]) cerr << " " << j;
cerr << "\n";
}*/
for(auto &cl : cs){
for(ll i = 1;i<cl.size();i++) ans[cl[i-1]][cl[i]] = ans[cl[i]][cl[i-1]] = 1;
}
for(ll i = 1;i<cc;i++) ans[cs[i-1][0]][cs[i][0]] = ans[cs[i][0]][cs[i-1][0]] = 1;
ans[cs.front()[0]][cs.back()[0]] = ans[cs.back()[0]][cs.front()[0]] = 1;
}
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:54:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(ll i = 1;i<o.size();i++) ans[o[i-1]][o[i]] = ans[o[i]][o[i-1]] = 1;
| ~^~~~~~~~~
supertrees.cpp:84:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(ll i = 1;i<cl.size();i++) ans[cl[i-1]][cl[i]] = ans[cl[i]][cl[i-1]] = 1;
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1116 KB |
Output is correct |
7 |
Correct |
93 ms |
21956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1116 KB |
Output is correct |
7 |
Correct |
93 ms |
21956 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
4 ms |
1112 KB |
Output is correct |
13 |
Correct |
91 ms |
22220 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
49 ms |
12180 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
28 ms |
5760 KB |
Output is correct |
21 |
Correct |
98 ms |
22100 KB |
Output is correct |
22 |
Correct |
99 ms |
22096 KB |
Output is correct |
23 |
Correct |
102 ms |
22160 KB |
Output is correct |
24 |
Correct |
93 ms |
22036 KB |
Output is correct |
25 |
Correct |
41 ms |
12116 KB |
Output is correct |
26 |
Correct |
39 ms |
12112 KB |
Output is correct |
27 |
Correct |
100 ms |
22044 KB |
Output is correct |
28 |
Correct |
93 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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
1116 KB |
Output is correct |
9 |
Correct |
97 ms |
22036 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
94 ms |
22036 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
53 ms |
12056 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
23 ms |
5784 KB |
Output is correct |
22 |
Correct |
96 ms |
22040 KB |
Output is correct |
23 |
Correct |
93 ms |
22100 KB |
Output is correct |
24 |
Correct |
99 ms |
22036 KB |
Output is correct |
25 |
Correct |
41 ms |
12112 KB |
Output is correct |
26 |
Correct |
41 ms |
12112 KB |
Output is correct |
27 |
Correct |
97 ms |
22080 KB |
Output is correct |
28 |
Correct |
105 ms |
22044 KB |
Output is correct |
29 |
Correct |
42 ms |
12312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
24 ms |
5780 KB |
Output is correct |
5 |
Correct |
94 ms |
22040 KB |
Output is correct |
6 |
Correct |
95 ms |
22100 KB |
Output is correct |
7 |
Correct |
98 ms |
22028 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
24 ms |
5780 KB |
Output is correct |
10 |
Correct |
98 ms |
22100 KB |
Output is correct |
11 |
Correct |
92 ms |
22040 KB |
Output is correct |
12 |
Correct |
100 ms |
22040 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
23 ms |
5784 KB |
Output is correct |
17 |
Correct |
126 ms |
22100 KB |
Output is correct |
18 |
Correct |
102 ms |
22136 KB |
Output is correct |
19 |
Correct |
101 ms |
22100 KB |
Output is correct |
20 |
Correct |
91 ms |
22044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1116 KB |
Output is correct |
7 |
Correct |
93 ms |
21956 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
4 ms |
1112 KB |
Output is correct |
13 |
Correct |
91 ms |
22220 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
49 ms |
12180 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
28 ms |
5760 KB |
Output is correct |
21 |
Correct |
98 ms |
22100 KB |
Output is correct |
22 |
Correct |
99 ms |
22096 KB |
Output is correct |
23 |
Correct |
102 ms |
22160 KB |
Output is correct |
24 |
Correct |
93 ms |
22036 KB |
Output is correct |
25 |
Correct |
41 ms |
12116 KB |
Output is correct |
26 |
Correct |
39 ms |
12112 KB |
Output is correct |
27 |
Correct |
100 ms |
22044 KB |
Output is correct |
28 |
Correct |
93 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 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
4 ms |
1116 KB |
Output is correct |
37 |
Correct |
97 ms |
22036 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1116 KB |
Output is correct |
41 |
Correct |
94 ms |
22036 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
53 ms |
12056 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
23 ms |
5784 KB |
Output is correct |
50 |
Correct |
96 ms |
22040 KB |
Output is correct |
51 |
Correct |
93 ms |
22100 KB |
Output is correct |
52 |
Correct |
99 ms |
22036 KB |
Output is correct |
53 |
Correct |
41 ms |
12112 KB |
Output is correct |
54 |
Correct |
41 ms |
12112 KB |
Output is correct |
55 |
Correct |
97 ms |
22080 KB |
Output is correct |
56 |
Correct |
105 ms |
22044 KB |
Output is correct |
57 |
Correct |
42 ms |
12312 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
3 ms |
856 KB |
Output is correct |
61 |
Correct |
48 ms |
12048 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
49 ms |
5712 KB |
Output is correct |
66 |
Correct |
42 ms |
12112 KB |
Output is correct |
67 |
Correct |
42 ms |
12112 KB |
Output is correct |
68 |
Correct |
71 ms |
12040 KB |
Output is correct |
69 |
Correct |
51 ms |
12316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1116 KB |
Output is correct |
7 |
Correct |
93 ms |
21956 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
4 ms |
1112 KB |
Output is correct |
13 |
Correct |
91 ms |
22220 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
49 ms |
12180 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
28 ms |
5760 KB |
Output is correct |
21 |
Correct |
98 ms |
22100 KB |
Output is correct |
22 |
Correct |
99 ms |
22096 KB |
Output is correct |
23 |
Correct |
102 ms |
22160 KB |
Output is correct |
24 |
Correct |
93 ms |
22036 KB |
Output is correct |
25 |
Correct |
41 ms |
12116 KB |
Output is correct |
26 |
Correct |
39 ms |
12112 KB |
Output is correct |
27 |
Correct |
100 ms |
22044 KB |
Output is correct |
28 |
Correct |
93 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 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
4 ms |
1116 KB |
Output is correct |
37 |
Correct |
97 ms |
22036 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1116 KB |
Output is correct |
41 |
Correct |
94 ms |
22036 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
53 ms |
12056 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
23 ms |
5784 KB |
Output is correct |
50 |
Correct |
96 ms |
22040 KB |
Output is correct |
51 |
Correct |
93 ms |
22100 KB |
Output is correct |
52 |
Correct |
99 ms |
22036 KB |
Output is correct |
53 |
Correct |
41 ms |
12112 KB |
Output is correct |
54 |
Correct |
41 ms |
12112 KB |
Output is correct |
55 |
Correct |
97 ms |
22080 KB |
Output is correct |
56 |
Correct |
105 ms |
22044 KB |
Output is correct |
57 |
Correct |
42 ms |
12312 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
24 ms |
5780 KB |
Output is correct |
62 |
Correct |
94 ms |
22040 KB |
Output is correct |
63 |
Correct |
95 ms |
22100 KB |
Output is correct |
64 |
Correct |
98 ms |
22028 KB |
Output is correct |
65 |
Correct |
0 ms |
344 KB |
Output is correct |
66 |
Correct |
24 ms |
5780 KB |
Output is correct |
67 |
Correct |
98 ms |
22100 KB |
Output is correct |
68 |
Correct |
92 ms |
22040 KB |
Output is correct |
69 |
Correct |
100 ms |
22040 KB |
Output is correct |
70 |
Correct |
0 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
23 ms |
5784 KB |
Output is correct |
74 |
Correct |
126 ms |
22100 KB |
Output is correct |
75 |
Correct |
102 ms |
22136 KB |
Output is correct |
76 |
Correct |
101 ms |
22100 KB |
Output is correct |
77 |
Correct |
91 ms |
22044 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
3 ms |
856 KB |
Output is correct |
81 |
Correct |
48 ms |
12048 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
49 ms |
5712 KB |
Output is correct |
86 |
Correct |
42 ms |
12112 KB |
Output is correct |
87 |
Correct |
42 ms |
12112 KB |
Output is correct |
88 |
Correct |
71 ms |
12040 KB |
Output is correct |
89 |
Correct |
51 ms |
12316 KB |
Output is correct |
90 |
Correct |
0 ms |
348 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
2 ms |
852 KB |
Output is correct |
93 |
Correct |
47 ms |
12056 KB |
Output is correct |
94 |
Correct |
0 ms |
348 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
12 ms |
3420 KB |
Output is correct |
98 |
Correct |
42 ms |
12044 KB |
Output is correct |
99 |
Correct |
41 ms |
12036 KB |
Output is correct |
100 |
Correct |
42 ms |
12056 KB |
Output is correct |
101 |
Correct |
59 ms |
12040 KB |
Output is correct |
102 |
Correct |
39 ms |
12052 KB |
Output is correct |
103 |
Correct |
39 ms |
12064 KB |
Output is correct |
104 |
Correct |
47 ms |
12116 KB |
Output is correct |
105 |
Correct |
54 ms |
12040 KB |
Output is correct |