#include "supertrees.h"
#include <vector>
using namespace std;
#include <bits/stdc++.h>
vector<vector<int>> p;
vector<vector<int>> groups;
vector<vector<int>> answer;
vector<vector<int>> relation1;
vector<int> isIn;
vector<int> isInGroup;
int n;
int ng;
void dfs(int node, int cur, int par)
{
isIn[node] = cur;
groups[cur].push_back(node);
if (par != -1)
{
answer[node][par] = 1;
answer[par][node] = 1;
}
for (int i = 0; i < n; i++)
{
if (isIn[i] == -1 && p[node][i] == 1)
{
dfs(i, cur, node);
}
}
}
void dfs2(int node, int cur)
{
isInGroup[node] = cur;
groups[cur].push_back(node);
for (int i = 0; i < ng; i++)
{
if (isInGroup[i] == -1 && relation1[node][i] == 2)
{
dfs2(i, cur);
}
}
}
int construct(std::vector<std::vector<int>> P) {
p = P;
n = p.size();
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
if (p[i][j] >= 3) return 0;
}
}
isIn.resize(n, -1);
answer.resize(n, vector<int>(n));
int cur = 0;
for (int i = 0; i < n; i++)
{
if (isIn[i] == -1)
{
groups.push_back(vector<int>());
dfs(i, cur, -1);
cur++;
}
}
for (auto g : groups)
{
for (int i = 0; i < g.size(); i++)
{
for (int j = i + 1; j < g.size(); j++)
{
if (p[g[i]][g[j]] != 1) return 0;
}
}
}
ng = groups.size();
relation1.resize(n, vector<int>(n, -1));
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (isIn[i] != isIn[j])
{
if (relation1[isIn[i]][isIn[j]] == -1)
{
relation1[isIn[i]][isIn[j]] = p[i][j];
relation1[isIn[j]][isIn[i]] = p[i][j];
} else if (relation1[isIn[i]][isIn[j]] != p[i][j]) return 0;
}
}
}
isInGroup.resize(n, -1);
groups.clear();
cur = 0;
for (int i = 0; i < ng; i++)
{
if (isInGroup[i] == -1)
{
groups.push_back(vector<int>());
dfs2(i, cur);
cur++;
}
}
vector<int> nodeInGroup(ng);
for (int i = 0; i < n; i++)
{
nodeInGroup[isIn[i]] = i;
}
for (auto g : groups)
{
if (g.size() == 1) continue;
if (g.size() == 2) return 0;
for (int i = 0; i < g.size(); i++)
{
for (int j = i + 1; j < g.size(); j++)
{
if (relation1[g[i]][g[j]] != 2) return 0;
}
int a = i;
int b = (i + 1) % g.size();
answer[nodeInGroup[g[a]]][nodeInGroup[g[b]]] = 1;
answer[nodeInGroup[g[b]]][nodeInGroup[g[a]]] = 1;
}
}
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:79:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 0; i < g.size(); i++)
| ~~^~~~~~~~~~
supertrees.cpp:81:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int j = i + 1; j < g.size(); j++)
| ~~^~~~~~~~~~
supertrees.cpp:130:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | for (int i = 0; i < g.size(); i++)
| ~~^~~~~~~~~~
supertrees.cpp:132:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | for (int j = i + 1; j < g.size(); j++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1484 KB |
Output is correct |
7 |
Correct |
241 ms |
29964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1484 KB |
Output is correct |
7 |
Correct |
241 ms |
29964 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1492 KB |
Output is correct |
13 |
Correct |
239 ms |
29956 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
106 ms |
16120 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
62 ms |
7676 KB |
Output is correct |
21 |
Correct |
234 ms |
29960 KB |
Output is correct |
22 |
Correct |
238 ms |
30020 KB |
Output is correct |
23 |
Correct |
238 ms |
29892 KB |
Output is correct |
24 |
Correct |
239 ms |
29992 KB |
Output is correct |
25 |
Correct |
97 ms |
16068 KB |
Output is correct |
26 |
Correct |
96 ms |
16008 KB |
Output is correct |
27 |
Correct |
251 ms |
29956 KB |
Output is correct |
28 |
Correct |
241 ms |
29960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
11 ms |
1588 KB |
Output is correct |
9 |
Correct |
249 ms |
31884 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1532 KB |
Output is correct |
13 |
Correct |
238 ms |
31996 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
1100 KB |
Output is correct |
17 |
Correct |
117 ms |
21996 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
62 ms |
8164 KB |
Output is correct |
22 |
Correct |
240 ms |
31968 KB |
Output is correct |
23 |
Correct |
251 ms |
32000 KB |
Output is correct |
24 |
Correct |
247 ms |
31908 KB |
Output is correct |
25 |
Correct |
108 ms |
21956 KB |
Output is correct |
26 |
Correct |
111 ms |
22040 KB |
Output is correct |
27 |
Correct |
242 ms |
32004 KB |
Output is correct |
28 |
Correct |
257 ms |
31940 KB |
Output is correct |
29 |
Correct |
105 ms |
21956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
64 ms |
7676 KB |
Output is correct |
5 |
Correct |
236 ms |
29840 KB |
Output is correct |
6 |
Correct |
236 ms |
29904 KB |
Output is correct |
7 |
Correct |
242 ms |
29992 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
61 ms |
7748 KB |
Output is correct |
10 |
Correct |
242 ms |
29936 KB |
Output is correct |
11 |
Correct |
256 ms |
29896 KB |
Output is correct |
12 |
Correct |
252 ms |
30020 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
60 ms |
7676 KB |
Output is correct |
17 |
Correct |
242 ms |
29988 KB |
Output is correct |
18 |
Correct |
241 ms |
30004 KB |
Output is correct |
19 |
Correct |
233 ms |
29892 KB |
Output is correct |
20 |
Correct |
245 ms |
30020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1484 KB |
Output is correct |
7 |
Correct |
241 ms |
29964 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1492 KB |
Output is correct |
13 |
Correct |
239 ms |
29956 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
106 ms |
16120 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
62 ms |
7676 KB |
Output is correct |
21 |
Correct |
234 ms |
29960 KB |
Output is correct |
22 |
Correct |
238 ms |
30020 KB |
Output is correct |
23 |
Correct |
238 ms |
29892 KB |
Output is correct |
24 |
Correct |
239 ms |
29992 KB |
Output is correct |
25 |
Correct |
97 ms |
16068 KB |
Output is correct |
26 |
Correct |
96 ms |
16008 KB |
Output is correct |
27 |
Correct |
251 ms |
29956 KB |
Output is correct |
28 |
Correct |
241 ms |
29960 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
11 ms |
1588 KB |
Output is correct |
37 |
Correct |
249 ms |
31884 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1532 KB |
Output is correct |
41 |
Correct |
238 ms |
31996 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
5 ms |
1100 KB |
Output is correct |
45 |
Correct |
117 ms |
21996 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
62 ms |
8164 KB |
Output is correct |
50 |
Correct |
240 ms |
31968 KB |
Output is correct |
51 |
Correct |
251 ms |
32000 KB |
Output is correct |
52 |
Correct |
247 ms |
31908 KB |
Output is correct |
53 |
Correct |
108 ms |
21956 KB |
Output is correct |
54 |
Correct |
111 ms |
22040 KB |
Output is correct |
55 |
Correct |
242 ms |
32004 KB |
Output is correct |
56 |
Correct |
257 ms |
31940 KB |
Output is correct |
57 |
Correct |
105 ms |
21956 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
5 ms |
972 KB |
Output is correct |
61 |
Correct |
104 ms |
18040 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
67 ms |
8204 KB |
Output is correct |
66 |
Correct |
102 ms |
21956 KB |
Output is correct |
67 |
Correct |
99 ms |
17988 KB |
Output is correct |
68 |
Correct |
107 ms |
21996 KB |
Output is correct |
69 |
Correct |
107 ms |
21996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1484 KB |
Output is correct |
7 |
Correct |
241 ms |
29964 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1492 KB |
Output is correct |
13 |
Correct |
239 ms |
29956 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
106 ms |
16120 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
62 ms |
7676 KB |
Output is correct |
21 |
Correct |
234 ms |
29960 KB |
Output is correct |
22 |
Correct |
238 ms |
30020 KB |
Output is correct |
23 |
Correct |
238 ms |
29892 KB |
Output is correct |
24 |
Correct |
239 ms |
29992 KB |
Output is correct |
25 |
Correct |
97 ms |
16068 KB |
Output is correct |
26 |
Correct |
96 ms |
16008 KB |
Output is correct |
27 |
Correct |
251 ms |
29956 KB |
Output is correct |
28 |
Correct |
241 ms |
29960 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
11 ms |
1588 KB |
Output is correct |
37 |
Correct |
249 ms |
31884 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1532 KB |
Output is correct |
41 |
Correct |
238 ms |
31996 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
5 ms |
1100 KB |
Output is correct |
45 |
Correct |
117 ms |
21996 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
62 ms |
8164 KB |
Output is correct |
50 |
Correct |
240 ms |
31968 KB |
Output is correct |
51 |
Correct |
251 ms |
32000 KB |
Output is correct |
52 |
Correct |
247 ms |
31908 KB |
Output is correct |
53 |
Correct |
108 ms |
21956 KB |
Output is correct |
54 |
Correct |
111 ms |
22040 KB |
Output is correct |
55 |
Correct |
242 ms |
32004 KB |
Output is correct |
56 |
Correct |
257 ms |
31940 KB |
Output is correct |
57 |
Correct |
105 ms |
21956 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
64 ms |
7676 KB |
Output is correct |
62 |
Correct |
236 ms |
29840 KB |
Output is correct |
63 |
Correct |
236 ms |
29904 KB |
Output is correct |
64 |
Correct |
242 ms |
29992 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
61 ms |
7748 KB |
Output is correct |
67 |
Correct |
242 ms |
29936 KB |
Output is correct |
68 |
Correct |
256 ms |
29896 KB |
Output is correct |
69 |
Correct |
252 ms |
30020 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
0 ms |
204 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
60 ms |
7676 KB |
Output is correct |
74 |
Correct |
242 ms |
29988 KB |
Output is correct |
75 |
Correct |
241 ms |
30004 KB |
Output is correct |
76 |
Correct |
233 ms |
29892 KB |
Output is correct |
77 |
Correct |
245 ms |
30020 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
0 ms |
204 KB |
Output is correct |
80 |
Correct |
5 ms |
972 KB |
Output is correct |
81 |
Correct |
104 ms |
18040 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
67 ms |
8204 KB |
Output is correct |
86 |
Correct |
102 ms |
21956 KB |
Output is correct |
87 |
Correct |
99 ms |
17988 KB |
Output is correct |
88 |
Correct |
107 ms |
21996 KB |
Output is correct |
89 |
Correct |
107 ms |
21996 KB |
Output is correct |
90 |
Correct |
1 ms |
204 KB |
Output is correct |
91 |
Correct |
1 ms |
204 KB |
Output is correct |
92 |
Correct |
5 ms |
844 KB |
Output is correct |
93 |
Correct |
101 ms |
14072 KB |
Output is correct |
94 |
Correct |
1 ms |
204 KB |
Output is correct |
95 |
Correct |
1 ms |
204 KB |
Output is correct |
96 |
Correct |
0 ms |
204 KB |
Output is correct |
97 |
Correct |
24 ms |
3700 KB |
Output is correct |
98 |
Correct |
97 ms |
14060 KB |
Output is correct |
99 |
Correct |
126 ms |
14060 KB |
Output is correct |
100 |
Correct |
95 ms |
14072 KB |
Output is correct |
101 |
Correct |
107 ms |
14072 KB |
Output is correct |
102 |
Correct |
96 ms |
14068 KB |
Output is correct |
103 |
Correct |
96 ms |
14072 KB |
Output is correct |
104 |
Correct |
96 ms |
14020 KB |
Output is correct |
105 |
Correct |
100 ms |
14072 KB |
Output is correct |