#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
struct DSU {
vector<int> p; void init(int N) { p = vector<int>(N,-1); }
int get(int x) { return p[x] < 0 ? x : p[x] = get(p[x]); }
bool sameSet(int a, int b) { return get(a) == get(b); }
int size(int x) { return -p[get(x)]; }
bool merge(int x, int y) {
x = get(x), y = get(y); if (x == y) return 0;
if (p[x] > p[y]) swap(x,y);
p[x] += p[y]; p[y] = x; return 1;
}
} ds1, ds2;
void build(vector<vector<int>> b);
//void build(vector<vector<int>> b){
// for(vector<int> a:b){
// for (int i = 0; i < a.size(); i++) {
// cout << a[i] << ' ';
// }
// cout << '\n';
// }
//}
int N;
int construct(vector<vector<int> > p){
N = p.size();
vector<vector<int>> b(N, vector<int>(N, 0));
ds1.init(N+1); ds2.init(N+1);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(i==j) continue;
if(p[i][j]!=0) ds1.merge(i, j);
if(p[i][j]==3) return 0;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(i==j) continue;
if(p[i][j] ==0 && ds1.sameSet(i, j)) return 0;
}
}
for (int i = 0; i < N; i++) {
if(ds1.p[i]>=0) continue;
unordered_set<int> added;
for (int j = 0; j < N; j++) {
if(!ds1.sameSet(i, j)) continue;
for (int k = 0; k < N; k++) {
if(!ds1.sameSet(i, k) || j==k) continue;
if(p[j][k] == 1) {
ds2.merge(j, k);
}
}
}
for (int j = 0; j < N; j++) {
if(!ds1.sameSet(i, j)) continue;
if(ds2.get(j) == j) added.insert(j);
}
for (int j = 0; j < N; j++) {
if(ds2.get(j) != j || !added.count(j)) continue;
int prev = j;
for (int k = 0; k < N; k++) {
if(j==k || !ds2.sameSet(j, k)) continue;
if(p[j][k] !=1) return 0;
b[prev][k] = b[k][prev] = 1;
prev= k;
}
}
if(added.size()==2) return 0;
if(added.size()>=3){
int prev = -1, fst = -1;
for(auto it:added){
if(prev==-1) prev = it, fst = it;
else{
b[prev][it] = b[it][prev] = 1;
prev = it;
}
}
b[fst][prev] = b[prev][fst] = 1;
}
}
build(b);
return 1;
}
//int main(){
// cin >> N;
// vector<vector<int>> b(N, vector<int>(N, 0));
// for (int i = 0; i < N; i++) {
// for (int j = 0; j < N; j++) {
// cin >> b[i][j];
// }
// }
// construct(b);
// return 0;
//}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1260 KB |
Output is correct |
7 |
Correct |
268 ms |
24172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1260 KB |
Output is correct |
7 |
Correct |
268 ms |
24172 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
12 ms |
1260 KB |
Output is correct |
13 |
Correct |
277 ms |
24172 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
876 KB |
Output is correct |
17 |
Correct |
121 ms |
14188 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
70 ms |
6508 KB |
Output is correct |
21 |
Correct |
272 ms |
24044 KB |
Output is correct |
22 |
Correct |
273 ms |
24072 KB |
Output is correct |
23 |
Correct |
280 ms |
24172 KB |
Output is correct |
24 |
Correct |
282 ms |
24016 KB |
Output is correct |
25 |
Correct |
113 ms |
14188 KB |
Output is correct |
26 |
Correct |
105 ms |
14188 KB |
Output is correct |
27 |
Correct |
278 ms |
24044 KB |
Output is correct |
28 |
Correct |
277 ms |
24044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
12 ms |
1260 KB |
Output is correct |
9 |
Correct |
276 ms |
24044 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1260 KB |
Output is correct |
13 |
Correct |
267 ms |
24044 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
876 KB |
Output is correct |
17 |
Correct |
119 ms |
14188 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
69 ms |
6380 KB |
Output is correct |
22 |
Correct |
270 ms |
24016 KB |
Output is correct |
23 |
Correct |
269 ms |
24044 KB |
Output is correct |
24 |
Correct |
275 ms |
24092 KB |
Output is correct |
25 |
Correct |
116 ms |
14188 KB |
Output is correct |
26 |
Correct |
108 ms |
14188 KB |
Output is correct |
27 |
Correct |
274 ms |
24044 KB |
Output is correct |
28 |
Correct |
279 ms |
24044 KB |
Output is correct |
29 |
Correct |
117 ms |
14188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
68 ms |
6252 KB |
Output is correct |
5 |
Correct |
267 ms |
24044 KB |
Output is correct |
6 |
Correct |
274 ms |
24172 KB |
Output is correct |
7 |
Correct |
277 ms |
24044 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
68 ms |
6336 KB |
Output is correct |
10 |
Correct |
267 ms |
24044 KB |
Output is correct |
11 |
Correct |
274 ms |
24044 KB |
Output is correct |
12 |
Correct |
283 ms |
24172 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
69 ms |
6252 KB |
Output is correct |
17 |
Correct |
270 ms |
24044 KB |
Output is correct |
18 |
Correct |
270 ms |
24068 KB |
Output is correct |
19 |
Correct |
264 ms |
24172 KB |
Output is correct |
20 |
Correct |
275 ms |
24068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1260 KB |
Output is correct |
7 |
Correct |
268 ms |
24172 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
12 ms |
1260 KB |
Output is correct |
13 |
Correct |
277 ms |
24172 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
876 KB |
Output is correct |
17 |
Correct |
121 ms |
14188 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
70 ms |
6508 KB |
Output is correct |
21 |
Correct |
272 ms |
24044 KB |
Output is correct |
22 |
Correct |
273 ms |
24072 KB |
Output is correct |
23 |
Correct |
280 ms |
24172 KB |
Output is correct |
24 |
Correct |
282 ms |
24016 KB |
Output is correct |
25 |
Correct |
113 ms |
14188 KB |
Output is correct |
26 |
Correct |
105 ms |
14188 KB |
Output is correct |
27 |
Correct |
278 ms |
24044 KB |
Output is correct |
28 |
Correct |
277 ms |
24044 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
12 ms |
1260 KB |
Output is correct |
37 |
Correct |
276 ms |
24044 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
11 ms |
1260 KB |
Output is correct |
41 |
Correct |
267 ms |
24044 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
5 ms |
876 KB |
Output is correct |
45 |
Correct |
119 ms |
14188 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
69 ms |
6380 KB |
Output is correct |
50 |
Correct |
270 ms |
24016 KB |
Output is correct |
51 |
Correct |
269 ms |
24044 KB |
Output is correct |
52 |
Correct |
275 ms |
24092 KB |
Output is correct |
53 |
Correct |
116 ms |
14188 KB |
Output is correct |
54 |
Correct |
108 ms |
14188 KB |
Output is correct |
55 |
Correct |
274 ms |
24044 KB |
Output is correct |
56 |
Correct |
279 ms |
24044 KB |
Output is correct |
57 |
Correct |
117 ms |
14188 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
5 ms |
876 KB |
Output is correct |
61 |
Correct |
119 ms |
14188 KB |
Output is correct |
62 |
Correct |
1 ms |
396 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
1 ms |
364 KB |
Output is correct |
65 |
Correct |
68 ms |
6252 KB |
Output is correct |
66 |
Correct |
107 ms |
14188 KB |
Output is correct |
67 |
Correct |
109 ms |
14188 KB |
Output is correct |
68 |
Correct |
107 ms |
14188 KB |
Output is correct |
69 |
Correct |
114 ms |
14188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1260 KB |
Output is correct |
7 |
Correct |
268 ms |
24172 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
12 ms |
1260 KB |
Output is correct |
13 |
Correct |
277 ms |
24172 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
876 KB |
Output is correct |
17 |
Correct |
121 ms |
14188 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
70 ms |
6508 KB |
Output is correct |
21 |
Correct |
272 ms |
24044 KB |
Output is correct |
22 |
Correct |
273 ms |
24072 KB |
Output is correct |
23 |
Correct |
280 ms |
24172 KB |
Output is correct |
24 |
Correct |
282 ms |
24016 KB |
Output is correct |
25 |
Correct |
113 ms |
14188 KB |
Output is correct |
26 |
Correct |
105 ms |
14188 KB |
Output is correct |
27 |
Correct |
278 ms |
24044 KB |
Output is correct |
28 |
Correct |
277 ms |
24044 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
12 ms |
1260 KB |
Output is correct |
37 |
Correct |
276 ms |
24044 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
11 ms |
1260 KB |
Output is correct |
41 |
Correct |
267 ms |
24044 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
5 ms |
876 KB |
Output is correct |
45 |
Correct |
119 ms |
14188 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
69 ms |
6380 KB |
Output is correct |
50 |
Correct |
270 ms |
24016 KB |
Output is correct |
51 |
Correct |
269 ms |
24044 KB |
Output is correct |
52 |
Correct |
275 ms |
24092 KB |
Output is correct |
53 |
Correct |
116 ms |
14188 KB |
Output is correct |
54 |
Correct |
108 ms |
14188 KB |
Output is correct |
55 |
Correct |
274 ms |
24044 KB |
Output is correct |
56 |
Correct |
279 ms |
24044 KB |
Output is correct |
57 |
Correct |
117 ms |
14188 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
68 ms |
6252 KB |
Output is correct |
62 |
Correct |
267 ms |
24044 KB |
Output is correct |
63 |
Correct |
274 ms |
24172 KB |
Output is correct |
64 |
Correct |
277 ms |
24044 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
68 ms |
6336 KB |
Output is correct |
67 |
Correct |
267 ms |
24044 KB |
Output is correct |
68 |
Correct |
274 ms |
24044 KB |
Output is correct |
69 |
Correct |
283 ms |
24172 KB |
Output is correct |
70 |
Correct |
1 ms |
364 KB |
Output is correct |
71 |
Correct |
1 ms |
364 KB |
Output is correct |
72 |
Correct |
1 ms |
364 KB |
Output is correct |
73 |
Correct |
69 ms |
6252 KB |
Output is correct |
74 |
Correct |
270 ms |
24044 KB |
Output is correct |
75 |
Correct |
270 ms |
24068 KB |
Output is correct |
76 |
Correct |
264 ms |
24172 KB |
Output is correct |
77 |
Correct |
275 ms |
24068 KB |
Output is correct |
78 |
Correct |
1 ms |
364 KB |
Output is correct |
79 |
Correct |
1 ms |
364 KB |
Output is correct |
80 |
Correct |
5 ms |
876 KB |
Output is correct |
81 |
Correct |
119 ms |
14188 KB |
Output is correct |
82 |
Correct |
1 ms |
396 KB |
Output is correct |
83 |
Correct |
1 ms |
364 KB |
Output is correct |
84 |
Correct |
1 ms |
364 KB |
Output is correct |
85 |
Correct |
68 ms |
6252 KB |
Output is correct |
86 |
Correct |
107 ms |
14188 KB |
Output is correct |
87 |
Correct |
109 ms |
14188 KB |
Output is correct |
88 |
Correct |
107 ms |
14188 KB |
Output is correct |
89 |
Correct |
114 ms |
14188 KB |
Output is correct |
90 |
Correct |
1 ms |
364 KB |
Output is correct |
91 |
Correct |
1 ms |
364 KB |
Output is correct |
92 |
Correct |
5 ms |
876 KB |
Output is correct |
93 |
Correct |
108 ms |
14084 KB |
Output is correct |
94 |
Correct |
1 ms |
364 KB |
Output is correct |
95 |
Correct |
1 ms |
364 KB |
Output is correct |
96 |
Correct |
1 ms |
364 KB |
Output is correct |
97 |
Correct |
26 ms |
3820 KB |
Output is correct |
98 |
Correct |
107 ms |
14188 KB |
Output is correct |
99 |
Correct |
107 ms |
14188 KB |
Output is correct |
100 |
Correct |
105 ms |
14188 KB |
Output is correct |
101 |
Correct |
106 ms |
14188 KB |
Output is correct |
102 |
Correct |
103 ms |
14188 KB |
Output is correct |
103 |
Correct |
104 ms |
14188 KB |
Output is correct |
104 |
Correct |
103 ms |
14188 KB |
Output is correct |
105 |
Correct |
111 ms |
14188 KB |
Output is correct |