Submission #841602

# Submission time Handle Problem Language Result Execution time Memory
841602 2023-09-01T18:21:07 Z qthang2k11 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
186 ms 28316 KB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

const int N = 1003;

vector<int> adj[2][N];

struct DisjointSetUnion {
  vector<int> con[N];
  int par[N];
  
  DisjointSetUnion() {
  	for (int i = 0; i < N; i++)
  	  par[i] = i, con[i] = {i};
  }
  
  inline int find(int x) {
    if (x != par[x]) 
      par[x] = find(par[x]);
    return par[x];
  }
  
  void join(int x, int y) {
    x = find(x); y = find(y);
    if (x == y) return;
    if (con[x].size() < con[y].size()) swap(x, y);
    par[y] = x;
    for (int i: con[y])
      con[x].emplace_back(i);
    con[y].clear();
    con[y].shrink_to_fit();
  }
} one, two, positive;

int construct(vector<vector<int>> p) {
	int n = p.size();
	for (int i = 0; i < n; i++) {
	  for (int j = 0; j < n; j++) {
	    if (p[i][j] != p[j][i] || p[i][j] == 3) return 0;
	    if (!p[i][j]) continue;
	    adj[p[i][j] - 1][i].emplace_back(j);
	    positive.join(i, j);
	  }
	}
	for (int i = 0; i < n; i++)
	  for (int j: adj[0][i])
	    one.join(i, j);
	for (int i = 0; i < n; i++)
	  for (int j: adj[1][i])
	    two.join(one.find(i), one.find(j));
	vector<vector<int>> ans(n, vector<int>(n, 0));
	auto add_edge = [&] (int x, int y) -> void {
	  ans[x][y] = ans[y][x] = 1;
	};
	for (int i = 0; i < n; i++) {
	  if (positive.find(i) == i) {
  	  for (int x: positive.con[i])
  	    for (int y: positive.con[i])
  	      if (p[x][y] == 0)
  	        return 0;
	  }
	  if (one.find(i) == i) {
  	  for (int x: one.con[i])
  	    for (int y: one.con[i])
  	      if (p[x][y] != 1)
  	        return 0;
  	  for (int j = 0, o = one.con[i].size(); j < o - 1; j++)
  	    add_edge(one.con[i][j], one.con[i][j + 1]);
	  }
	  if (two.find(i) == i && (int)two.con[i].size() > 1) {
	    if ((int)two.con[i].size() == 2) return 0;
	    for (int j = 0, o = two.con[i].size(); j < o - 1; j++)
	      add_edge(two.con[i][j], two.con[i][j + 1]);
	    add_edge(two.con[i][0], two.con[i].back());
	  }
	}
	build(ans);
	return 1;
}

#ifdef LOCAL
#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>

static int n;
static std::vector<std::vector<int>> p;
static std::vector<std::vector<int>> b;
static bool called = false;

static void check(bool cond, std::string message) {
    if (!cond) {
        printf("%s\n", message.c_str());
        fclose(stdout);
        exit(0);
    }
}

void build(std::vector<std::vector<int>> _b) {
    check(!called, "build is called more than once");
    called = true;
    check((int)_b.size() == n, "Invalid number of rows in b");
    for (int i = 0; i < n; i++) {
        check((int)_b[i].size() == n, "Invalid number of columns in b");
    }
    b = _b;
}

int main() {
    assert(scanf("%d", &n) == 1);
    
    p.resize(n);
    for (int i = 0; i < n; i++) {
        p[i].resize(n);
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            assert(scanf("%d", &p[i][j]) == 1);
        }
    }
    fclose(stdin);

    int possible = construct(p);

    check(possible == 0 || possible == 1, "Invalid return value of construct");
    if (possible == 1) {
        check(called, "construct returned 1 without calling build");
    } else {
        check(!called, "construct called build but returned 0");
    }

    printf("%d\n", possible);
    if (possible == 1) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (j) {
                    printf(" ");
                }
                printf("%d", b[i][j]);
            }
            printf("\n");
        }
    }
    fclose(stdout);
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 7 ms 1796 KB Output is correct
7 Correct 159 ms 28316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 7 ms 1796 KB Output is correct
7 Correct 159 ms 28316 KB Output is correct
8 Correct 1 ms 672 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 6 ms 1416 KB Output is correct
13 Correct 144 ms 24404 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 4 ms 1116 KB Output is correct
17 Correct 81 ms 17008 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 36 ms 6484 KB Output is correct
21 Correct 146 ms 24676 KB Output is correct
22 Correct 146 ms 24328 KB Output is correct
23 Correct 160 ms 26108 KB Output is correct
24 Correct 156 ms 24172 KB Output is correct
25 Correct 63 ms 14700 KB Output is correct
26 Correct 61 ms 14420 KB Output is correct
27 Correct 160 ms 27220 KB Output is correct
28 Correct 146 ms 24224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 668 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 6 ms 1628 KB Output is correct
9 Correct 143 ms 24272 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 7 ms 1700 KB Output is correct
13 Correct 157 ms 28244 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 4 ms 1196 KB Output is correct
17 Correct 77 ms 16792 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 45 ms 6436 KB Output is correct
22 Correct 145 ms 24808 KB Output is correct
23 Correct 145 ms 24232 KB Output is correct
24 Correct 186 ms 27364 KB Output is correct
25 Correct 64 ms 14452 KB Output is correct
26 Correct 64 ms 14952 KB Output is correct
27 Correct 143 ms 24176 KB Output is correct
28 Correct 172 ms 26224 KB Output is correct
29 Correct 60 ms 14420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 36 ms 6524 KB Output is correct
5 Correct 145 ms 24660 KB Output is correct
6 Correct 145 ms 24292 KB Output is correct
7 Correct 161 ms 26480 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 36 ms 6492 KB Output is correct
10 Correct 151 ms 24872 KB Output is correct
11 Correct 144 ms 24224 KB Output is correct
12 Correct 159 ms 27380 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 37 ms 6484 KB Output is correct
17 Correct 146 ms 24688 KB Output is correct
18 Correct 154 ms 24828 KB Output is correct
19 Correct 143 ms 24568 KB Output is correct
20 Correct 140 ms 24408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 7 ms 1796 KB Output is correct
7 Correct 159 ms 28316 KB Output is correct
8 Correct 1 ms 672 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 6 ms 1416 KB Output is correct
13 Correct 144 ms 24404 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 4 ms 1116 KB Output is correct
17 Correct 81 ms 17008 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 36 ms 6484 KB Output is correct
21 Correct 146 ms 24676 KB Output is correct
22 Correct 146 ms 24328 KB Output is correct
23 Correct 160 ms 26108 KB Output is correct
24 Correct 156 ms 24172 KB Output is correct
25 Correct 63 ms 14700 KB Output is correct
26 Correct 61 ms 14420 KB Output is correct
27 Correct 160 ms 27220 KB Output is correct
28 Correct 146 ms 24224 KB Output is correct
29 Correct 1 ms 668 KB Output is correct
30 Correct 0 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 0 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 0 ms 604 KB Output is correct
36 Correct 6 ms 1628 KB Output is correct
37 Correct 143 ms 24272 KB Output is correct
38 Correct 0 ms 604 KB Output is correct
39 Correct 0 ms 604 KB Output is correct
40 Correct 7 ms 1700 KB Output is correct
41 Correct 157 ms 28244 KB Output is correct
42 Correct 0 ms 604 KB Output is correct
43 Correct 0 ms 604 KB Output is correct
44 Correct 4 ms 1196 KB Output is correct
45 Correct 77 ms 16792 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 0 ms 604 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 45 ms 6436 KB Output is correct
50 Correct 145 ms 24808 KB Output is correct
51 Correct 145 ms 24232 KB Output is correct
52 Correct 186 ms 27364 KB Output is correct
53 Correct 64 ms 14452 KB Output is correct
54 Correct 64 ms 14952 KB Output is correct
55 Correct 143 ms 24176 KB Output is correct
56 Correct 172 ms 26224 KB Output is correct
57 Correct 60 ms 14420 KB Output is correct
58 Correct 1 ms 860 KB Output is correct
59 Correct 0 ms 604 KB Output is correct
60 Correct 4 ms 1372 KB Output is correct
61 Correct 79 ms 18416 KB Output is correct
62 Correct 1 ms 856 KB Output is correct
63 Correct 1 ms 604 KB Output is correct
64 Correct 1 ms 604 KB Output is correct
65 Correct 44 ms 6628 KB Output is correct
66 Correct 63 ms 14876 KB Output is correct
67 Correct 69 ms 14952 KB Output is correct
68 Correct 62 ms 14576 KB Output is correct
69 Correct 62 ms 14576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 7 ms 1796 KB Output is correct
7 Correct 159 ms 28316 KB Output is correct
8 Correct 1 ms 672 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 6 ms 1416 KB Output is correct
13 Correct 144 ms 24404 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 4 ms 1116 KB Output is correct
17 Correct 81 ms 17008 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 36 ms 6484 KB Output is correct
21 Correct 146 ms 24676 KB Output is correct
22 Correct 146 ms 24328 KB Output is correct
23 Correct 160 ms 26108 KB Output is correct
24 Correct 156 ms 24172 KB Output is correct
25 Correct 63 ms 14700 KB Output is correct
26 Correct 61 ms 14420 KB Output is correct
27 Correct 160 ms 27220 KB Output is correct
28 Correct 146 ms 24224 KB Output is correct
29 Correct 1 ms 668 KB Output is correct
30 Correct 0 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 0 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 0 ms 604 KB Output is correct
36 Correct 6 ms 1628 KB Output is correct
37 Correct 143 ms 24272 KB Output is correct
38 Correct 0 ms 604 KB Output is correct
39 Correct 0 ms 604 KB Output is correct
40 Correct 7 ms 1700 KB Output is correct
41 Correct 157 ms 28244 KB Output is correct
42 Correct 0 ms 604 KB Output is correct
43 Correct 0 ms 604 KB Output is correct
44 Correct 4 ms 1196 KB Output is correct
45 Correct 77 ms 16792 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 0 ms 604 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 45 ms 6436 KB Output is correct
50 Correct 145 ms 24808 KB Output is correct
51 Correct 145 ms 24232 KB Output is correct
52 Correct 186 ms 27364 KB Output is correct
53 Correct 64 ms 14452 KB Output is correct
54 Correct 64 ms 14952 KB Output is correct
55 Correct 143 ms 24176 KB Output is correct
56 Correct 172 ms 26224 KB Output is correct
57 Correct 60 ms 14420 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 0 ms 604 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 36 ms 6524 KB Output is correct
62 Correct 145 ms 24660 KB Output is correct
63 Correct 145 ms 24292 KB Output is correct
64 Correct 161 ms 26480 KB Output is correct
65 Correct 1 ms 604 KB Output is correct
66 Correct 36 ms 6492 KB Output is correct
67 Correct 151 ms 24872 KB Output is correct
68 Correct 144 ms 24224 KB Output is correct
69 Correct 159 ms 27380 KB Output is correct
70 Correct 1 ms 604 KB Output is correct
71 Correct 1 ms 604 KB Output is correct
72 Correct 0 ms 604 KB Output is correct
73 Correct 37 ms 6484 KB Output is correct
74 Correct 146 ms 24688 KB Output is correct
75 Correct 154 ms 24828 KB Output is correct
76 Correct 143 ms 24568 KB Output is correct
77 Correct 140 ms 24408 KB Output is correct
78 Correct 1 ms 860 KB Output is correct
79 Correct 0 ms 604 KB Output is correct
80 Correct 4 ms 1372 KB Output is correct
81 Correct 79 ms 18416 KB Output is correct
82 Correct 1 ms 856 KB Output is correct
83 Correct 1 ms 604 KB Output is correct
84 Correct 1 ms 604 KB Output is correct
85 Correct 44 ms 6628 KB Output is correct
86 Correct 63 ms 14876 KB Output is correct
87 Correct 69 ms 14952 KB Output is correct
88 Correct 62 ms 14576 KB Output is correct
89 Correct 62 ms 14576 KB Output is correct
90 Correct 1 ms 604 KB Output is correct
91 Correct 0 ms 604 KB Output is correct
92 Correct 3 ms 860 KB Output is correct
93 Correct 61 ms 10340 KB Output is correct
94 Correct 1 ms 604 KB Output is correct
95 Correct 0 ms 604 KB Output is correct
96 Correct 1 ms 856 KB Output is correct
97 Correct 15 ms 2928 KB Output is correct
98 Correct 59 ms 10324 KB Output is correct
99 Correct 63 ms 10584 KB Output is correct
100 Correct 57 ms 10324 KB Output is correct
101 Correct 58 ms 10468 KB Output is correct
102 Correct 57 ms 10324 KB Output is correct
103 Correct 58 ms 10480 KB Output is correct
104 Correct 58 ms 10472 KB Output is correct
105 Correct 62 ms 10480 KB Output is correct