Submission #389115

# Submission time Handle Problem Language Result Execution time Memory
389115 2021-04-13T16:32:45 Z apostoldaniel854 Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
289 ms 24064 KB
#include <bits/stdc++.h>

using namespace std;


//#define HOME


#ifndef HOME
#include "supertrees.h"
#endif // HOME

#ifdef HOME

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




static void check(bool cond, 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;
}
#endif // HOME

/**
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1

**/
const int max_n = 1000;
int father[1 + max_n];
int find_father (int x) {
    if (father[x] == x)
        return x;
    return father[x] = find_father (father[x]);
}

vector <vector <int>> answer;

void connect (int x, int y, bool add) {
    x = find_father (x);
    y = find_father (y);
    if (x != y) {
        father[x] = y;
        if (add)
            answer[x][y] = answer[y][x] = 1;
    }
}

int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	answer.resize (n);
	for (int i = 0; i < n; i++)
        answer[i].resize (n);
    for (int i = 0; i < n; i++)
        father[i] = i;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 3)
                return 0;
            if (p[i][j] == 1)
                connect (i, j, true);
        }
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (find_father (i) == find_father (j) && p[i][j] != 1)
                return 0;
    vector <bool> is_root (n);
    for (int i = 0; i < n; i++)
        if (find_father (i) == i)
            is_root[i] = true;
    for (int i = 0; i < n; i++)
        father[i] = i;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (is_root[i] && is_root[j] && p[i][j] == 2)
                connect (i, j, false);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (i != j && find_father (i) == find_father (j) && p[i][j] != 2)
                return 0;
    for (int i = 0; i < n; i++) {
        if (find_father (i) == i) {
            vector <int> v;
            for (int j = 0; j < n; j++)
                if (find_father (j) == i)
                    v.push_back (j);
            if (v.size () == 2) return 0;
            if (v.size () >= 3) {
                for (int j = 1; j < v.size (); j++)
                    answer[v[j - 1]][v[j]] = answer[v[j]][v[j - 1]] = 1;
                answer[v[0]][v.back ()] = answer[v.back ()][v[0]] = 1;
            }
        }
    }
    build(answer);
	return 1;
}

#ifdef HOME
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 // HOME

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:110:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |                 for (int j = 1; j < v.size (); j++)
      |                                 ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 300 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 11 ms 1220 KB Output is correct
7 Correct 268 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 300 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 11 ms 1220 KB Output is correct
7 Correct 268 ms 23936 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1228 KB Output is correct
13 Correct 263 ms 23932 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 208 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 120 ms 14068 KB Output is correct
18 Correct 0 ms 296 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 66 ms 6196 KB Output is correct
21 Correct 266 ms 23892 KB Output is correct
22 Correct 259 ms 23936 KB Output is correct
23 Correct 283 ms 24048 KB Output is correct
24 Correct 264 ms 23948 KB Output is correct
25 Correct 108 ms 14020 KB Output is correct
26 Correct 103 ms 14076 KB Output is correct
27 Correct 279 ms 23932 KB Output is correct
28 Correct 255 ms 24000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 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 1228 KB Output is correct
9 Correct 256 ms 23932 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1228 KB Output is correct
13 Correct 269 ms 23924 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 126 ms 14080 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 65 ms 6136 KB Output is correct
22 Correct 268 ms 23924 KB Output is correct
23 Correct 255 ms 23988 KB Output is correct
24 Correct 281 ms 24004 KB Output is correct
25 Correct 112 ms 14076 KB Output is correct
26 Correct 109 ms 14076 KB Output is correct
27 Correct 262 ms 23984 KB Output is correct
28 Correct 289 ms 23980 KB Output is correct
29 Correct 118 ms 14148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 0 ms 300 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 66 ms 6252 KB Output is correct
5 Correct 274 ms 24000 KB Output is correct
6 Correct 264 ms 23992 KB Output is correct
7 Correct 287 ms 24016 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 72 ms 6256 KB Output is correct
10 Correct 267 ms 24000 KB Output is correct
11 Correct 258 ms 24064 KB Output is correct
12 Correct 276 ms 24004 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 67 ms 6156 KB Output is correct
17 Correct 260 ms 23924 KB Output is correct
18 Correct 270 ms 23984 KB Output is correct
19 Correct 275 ms 23936 KB Output is correct
20 Correct 261 ms 23996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 300 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 11 ms 1220 KB Output is correct
7 Correct 268 ms 23936 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1228 KB Output is correct
13 Correct 263 ms 23932 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 208 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 120 ms 14068 KB Output is correct
18 Correct 0 ms 296 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 66 ms 6196 KB Output is correct
21 Correct 266 ms 23892 KB Output is correct
22 Correct 259 ms 23936 KB Output is correct
23 Correct 283 ms 24048 KB Output is correct
24 Correct 264 ms 23948 KB Output is correct
25 Correct 108 ms 14020 KB Output is correct
26 Correct 103 ms 14076 KB Output is correct
27 Correct 279 ms 23932 KB Output is correct
28 Correct 255 ms 24000 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 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 1228 KB Output is correct
37 Correct 256 ms 23932 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 11 ms 1228 KB Output is correct
41 Correct 269 ms 23924 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 844 KB Output is correct
45 Correct 126 ms 14080 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 65 ms 6136 KB Output is correct
50 Correct 268 ms 23924 KB Output is correct
51 Correct 255 ms 23988 KB Output is correct
52 Correct 281 ms 24004 KB Output is correct
53 Correct 112 ms 14076 KB Output is correct
54 Correct 109 ms 14076 KB Output is correct
55 Correct 262 ms 23984 KB Output is correct
56 Correct 289 ms 23980 KB Output is correct
57 Correct 118 ms 14148 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 5 ms 844 KB Output is correct
61 Correct 115 ms 14080 KB Output is correct
62 Correct 0 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 6304 KB Output is correct
66 Correct 110 ms 14020 KB Output is correct
67 Correct 106 ms 14020 KB Output is correct
68 Correct 115 ms 14068 KB Output is correct
69 Correct 118 ms 14064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 300 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 11 ms 1220 KB Output is correct
7 Correct 268 ms 23936 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1228 KB Output is correct
13 Correct 263 ms 23932 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 208 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 120 ms 14068 KB Output is correct
18 Correct 0 ms 296 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 66 ms 6196 KB Output is correct
21 Correct 266 ms 23892 KB Output is correct
22 Correct 259 ms 23936 KB Output is correct
23 Correct 283 ms 24048 KB Output is correct
24 Correct 264 ms 23948 KB Output is correct
25 Correct 108 ms 14020 KB Output is correct
26 Correct 103 ms 14076 KB Output is correct
27 Correct 279 ms 23932 KB Output is correct
28 Correct 255 ms 24000 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 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 1228 KB Output is correct
37 Correct 256 ms 23932 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 11 ms 1228 KB Output is correct
41 Correct 269 ms 23924 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 844 KB Output is correct
45 Correct 126 ms 14080 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 65 ms 6136 KB Output is correct
50 Correct 268 ms 23924 KB Output is correct
51 Correct 255 ms 23988 KB Output is correct
52 Correct 281 ms 24004 KB Output is correct
53 Correct 112 ms 14076 KB Output is correct
54 Correct 109 ms 14076 KB Output is correct
55 Correct 262 ms 23984 KB Output is correct
56 Correct 289 ms 23980 KB Output is correct
57 Correct 118 ms 14148 KB Output is correct
58 Correct 1 ms 300 KB Output is correct
59 Correct 0 ms 300 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 66 ms 6252 KB Output is correct
62 Correct 274 ms 24000 KB Output is correct
63 Correct 264 ms 23992 KB Output is correct
64 Correct 287 ms 24016 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 72 ms 6256 KB Output is correct
67 Correct 267 ms 24000 KB Output is correct
68 Correct 258 ms 24064 KB Output is correct
69 Correct 276 ms 24004 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 67 ms 6156 KB Output is correct
74 Correct 260 ms 23924 KB Output is correct
75 Correct 270 ms 23984 KB Output is correct
76 Correct 275 ms 23936 KB Output is correct
77 Correct 261 ms 23996 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 5 ms 844 KB Output is correct
81 Correct 115 ms 14080 KB Output is correct
82 Correct 0 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 6304 KB Output is correct
86 Correct 110 ms 14020 KB Output is correct
87 Correct 106 ms 14020 KB Output is correct
88 Correct 115 ms 14068 KB Output is correct
89 Correct 118 ms 14064 KB Output is correct
90 Correct 1 ms 204 KB Output is correct
91 Correct 0 ms 204 KB Output is correct
92 Correct 5 ms 844 KB Output is correct
93 Correct 105 ms 14076 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 204 KB Output is correct
97 Correct 26 ms 3708 KB Output is correct
98 Correct 103 ms 14068 KB Output is correct
99 Correct 103 ms 14076 KB Output is correct
100 Correct 103 ms 14072 KB Output is correct
101 Correct 106 ms 14020 KB Output is correct
102 Correct 106 ms 14020 KB Output is correct
103 Correct 104 ms 14080 KB Output is correct
104 Correct 103 ms 14068 KB Output is correct
105 Correct 107 ms 14020 KB Output is correct