Submission #927702

# Submission time Handle Problem Language Result Execution time Memory
927702 2024-02-15T08:53:07 Z haxorman Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
179 ms 22588 KB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;

const int mxN = 1007;

int dsu[mxN][2];
bool mark[mxN], done[mxN];
vector<int> cmp[mxN][2];

int find(int x, int id) {
    return dsu[x][id] < 0 ? x : dsu[x][id] = find(dsu[x][id], id);
}

bool unite(int x, int y, int id) {
    x = find(x, id), y = find(y, id);

    if (x == y) {
        return false;
    }

    if (dsu[x][id] > dsu[y][id]) {
        swap(x, y);
    }
    dsu[x][id] += dsu[y][id];
    dsu[y][id] = x;

    for (auto c : cmp[y][id]) {
        cmp[x][id].push_back(c);
    }

    return true;
}

int construct(vector<vector<int>> p) {
	int n = p.size();
    memset(dsu, -1, sizeof(dsu));
    vector<vector<int>> ans(n, vector<int>(n));
    
    for (int i = 0; i < n; ++i) {
        cmp[i][0].push_back(i);
        cmp[i][1].push_back(i);
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (p[i][j] == 1) {
                if (unite(i, j, 0)) {
                    ans[i][j] = ans[j][i] = 1;
                }
            } 
        }
    }
    
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (p[i][j] == 2) {
                int x = find(i, 0), y = find(j, 0);
                if (x == y) {
                    return 0;
                }

                if (find(x, 1) == find(y, 1)) {
                    continue;
                }

                for (auto u : cmp[x][0]) {
                    for (auto v : cmp[y][0]) {
                        if (p[u][v] != 2) {
                            return 0;
                        }
                    }
                }
                unite(x, y, 1);
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int x = find(i, 0), y = find(j, 0);
            if (!p[i][j] && (x == y || find(x, 1) == find(y, 1))) {
                return 0;
            }

            if (p[i][j] == 1 && x != y && find(x, 1) == find(y,1)) {
                return 0;
            }

            if (p[i][j] == 3) {
                return 0;
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        int x = find(i, 0);
        int z = find(x, 1);
        if (done[z] || cmp[z][1].size() == 1) {
            continue;
        }

        if (cmp[z][1].size() == 2) {
            return 0;
        } 

        for (int j = 0; j < cmp[z][1].size() - 1; ++j) {
            int a = cmp[z][1][j], b = cmp[z][1][j+1];
            ans[a][b] = ans[b][a] = 1;
        }
        int a = cmp[z][1][0], b = cmp[z][1].back();
        ans[a][b] = ans[b][a] = 1;
    }
    build(ans);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:107:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |         for (int j = 0; j < cmp[z][1].size() - 1; ++j) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 154 ms 22172 KB Output is correct
# Verdict Execution time Memory 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 154 ms 22172 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1368 KB Output is correct
13 Correct 151 ms 22588 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 73 ms 12344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 39 ms 5972 KB Output is correct
21 Correct 156 ms 22088 KB Output is correct
22 Correct 156 ms 22200 KB Output is correct
23 Correct 167 ms 22352 KB Output is correct
24 Correct 158 ms 22092 KB Output is correct
25 Correct 66 ms 12348 KB Output is correct
26 Correct 68 ms 12184 KB Output is correct
27 Correct 163 ms 22100 KB Output is correct
28 Correct 171 ms 22100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 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 8 ms 1372 KB Output is correct
9 Correct 162 ms 22076 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 6 ms 1372 KB Output is correct
13 Correct 179 ms 22288 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 82 ms 12348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 42 ms 5916 KB Output is correct
22 Correct 161 ms 22240 KB Output is correct
23 Correct 161 ms 22356 KB Output is correct
24 Correct 158 ms 22076 KB Output is correct
25 Correct 75 ms 12372 KB Output is correct
26 Correct 70 ms 12344 KB Output is correct
27 Correct 155 ms 22100 KB Output is correct
28 Correct 159 ms 22092 KB Output is correct
29 Correct 70 ms 12368 KB Output is correct
# Verdict Execution time Memory 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 39 ms 5780 KB Output is correct
5 Correct 155 ms 22240 KB Output is correct
6 Correct 152 ms 22096 KB Output is correct
7 Correct 163 ms 22092 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 39 ms 5868 KB Output is correct
10 Correct 157 ms 22096 KB Output is correct
11 Correct 157 ms 22096 KB Output is correct
12 Correct 159 ms 22100 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 40 ms 5760 KB Output is correct
17 Correct 158 ms 22096 KB Output is correct
18 Correct 171 ms 22248 KB Output is correct
19 Correct 157 ms 22096 KB Output is correct
20 Correct 151 ms 22356 KB Output is correct
# Verdict Execution time Memory 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 154 ms 22172 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1368 KB Output is correct
13 Correct 151 ms 22588 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 73 ms 12344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 39 ms 5972 KB Output is correct
21 Correct 156 ms 22088 KB Output is correct
22 Correct 156 ms 22200 KB Output is correct
23 Correct 167 ms 22352 KB Output is correct
24 Correct 158 ms 22092 KB Output is correct
25 Correct 66 ms 12348 KB Output is correct
26 Correct 68 ms 12184 KB Output is correct
27 Correct 163 ms 22100 KB Output is correct
28 Correct 171 ms 22100 KB Output is correct
29 Correct 1 ms 344 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 8 ms 1372 KB Output is correct
37 Correct 162 ms 22076 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 6 ms 1372 KB Output is correct
41 Correct 179 ms 22288 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 860 KB Output is correct
45 Correct 82 ms 12348 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 42 ms 5916 KB Output is correct
50 Correct 161 ms 22240 KB Output is correct
51 Correct 161 ms 22356 KB Output is correct
52 Correct 158 ms 22076 KB Output is correct
53 Correct 75 ms 12372 KB Output is correct
54 Correct 70 ms 12344 KB Output is correct
55 Correct 155 ms 22100 KB Output is correct
56 Correct 159 ms 22092 KB Output is correct
57 Correct 70 ms 12368 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 3 ms 908 KB Output is correct
61 Correct 73 ms 12388 KB Output is correct
62 Correct 1 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 40 ms 5972 KB Output is correct
66 Correct 67 ms 12368 KB Output is correct
67 Correct 69 ms 12348 KB Output is correct
68 Correct 83 ms 12360 KB Output is correct
69 Correct 64 ms 12360 KB Output is correct
# Verdict Execution time Memory 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 154 ms 22172 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1368 KB Output is correct
13 Correct 151 ms 22588 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 73 ms 12344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 39 ms 5972 KB Output is correct
21 Correct 156 ms 22088 KB Output is correct
22 Correct 156 ms 22200 KB Output is correct
23 Correct 167 ms 22352 KB Output is correct
24 Correct 158 ms 22092 KB Output is correct
25 Correct 66 ms 12348 KB Output is correct
26 Correct 68 ms 12184 KB Output is correct
27 Correct 163 ms 22100 KB Output is correct
28 Correct 171 ms 22100 KB Output is correct
29 Correct 1 ms 344 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 8 ms 1372 KB Output is correct
37 Correct 162 ms 22076 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 6 ms 1372 KB Output is correct
41 Correct 179 ms 22288 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 860 KB Output is correct
45 Correct 82 ms 12348 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 42 ms 5916 KB Output is correct
50 Correct 161 ms 22240 KB Output is correct
51 Correct 161 ms 22356 KB Output is correct
52 Correct 158 ms 22076 KB Output is correct
53 Correct 75 ms 12372 KB Output is correct
54 Correct 70 ms 12344 KB Output is correct
55 Correct 155 ms 22100 KB Output is correct
56 Correct 159 ms 22092 KB Output is correct
57 Correct 70 ms 12368 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 39 ms 5780 KB Output is correct
62 Correct 155 ms 22240 KB Output is correct
63 Correct 152 ms 22096 KB Output is correct
64 Correct 163 ms 22092 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 39 ms 5868 KB Output is correct
67 Correct 157 ms 22096 KB Output is correct
68 Correct 157 ms 22096 KB Output is correct
69 Correct 159 ms 22100 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 1 ms 348 KB Output is correct
73 Correct 40 ms 5760 KB Output is correct
74 Correct 158 ms 22096 KB Output is correct
75 Correct 171 ms 22248 KB Output is correct
76 Correct 157 ms 22096 KB Output is correct
77 Correct 151 ms 22356 KB Output is correct
78 Correct 1 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 3 ms 908 KB Output is correct
81 Correct 73 ms 12388 KB Output is correct
82 Correct 1 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 40 ms 5972 KB Output is correct
86 Correct 67 ms 12368 KB Output is correct
87 Correct 69 ms 12348 KB Output is correct
88 Correct 83 ms 12360 KB Output is correct
89 Correct 64 ms 12360 KB Output is correct
90 Correct 0 ms 344 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 3 ms 860 KB Output is correct
93 Correct 69 ms 12368 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 17 ms 3896 KB Output is correct
98 Correct 69 ms 13700 KB Output is correct
99 Correct 68 ms 13648 KB Output is correct
100 Correct 82 ms 13740 KB Output is correct
101 Correct 65 ms 13792 KB Output is correct
102 Correct 64 ms 13824 KB Output is correct
103 Correct 71 ms 13808 KB Output is correct
104 Correct 70 ms 13648 KB Output is correct
105 Correct 70 ms 13660 KB Output is correct