Submission #405974

# Submission time Handle Problem Language Result Execution time Memory
405974 2021-05-17T05:59:49 Z tiberiu Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
322 ms 24132 KB
#include <iostream>
#include <vector>
#include "supertrees.h"

using namespace std;

const int MAX_N = 1000;
int UF[MAX_N];

int root(int x) {
    return UF[x] == -1 ? x : UF[x] = root(UF[x]);
}

void unite(int x, int y) {
    x = root(x);
    y = root(y);

    if (x != y)
        UF[x] = y;
}

vector<vector<int>> b;

bool solve(const vector<vector<int>>& p, vector<int> c) {
    int N = p.size();

    for (int i : c)
        for (int j : c)
            if (p[i][j] == 1)
                unite(i, j);

    for (int i : c)
        for (int j : c)
            if (p[i][j] != 1 + (root(i) != root(j)))
                return 0;

    vector<int> cycle;
    for (int i : c)
        if (UF[i] != -1)
            b[i][UF[i]] = b[UF[i]][i] = 1;
        else
            cycle.push_back(i);

    if (cycle.size() == 2)
        return 0;
    if (cycle.size() > 1) {
        cycle.push_back(cycle[0]);
        for (int i = 1; i < cycle.size(); i++)
            b[cycle[i]][cycle[i-1]] = b[cycle[i-1]][cycle[i]] = 1;
    }

    return 1;
}

int construct(vector<vector<int>> p) {
    int N = p.size();
    for (int i = 0; i < N; i++)
        UF[i] = -1;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            if (p[i][j] == 3)
                return 0;
            else if (p[i][j])
                unite(i, j);

    vector<vector<int>> comps;
    for (int i = 0; i < N; i++)
        if (UF[i] == -1) {
            vector<int> c;
            for (int j = 0; j < N; j++)
                if (root(j) == i)
                    c.push_back(j);
            comps.push_back(c);
        }

    b.resize(N);
    for (int i = 0; i < N; i++) {
        UF[i] = -1;
        b[i].resize(N);
    }

    for (int i = 0; i < comps.size(); i++)
        if (!solve(p, comps[i]))
            return 0;

    build(b);
    return 1;
}

Compilation message

supertrees.cpp: In function 'bool solve(const std::vector<std::vector<int> >&, std::vector<int>)':
supertrees.cpp:48:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |         for (int i = 1; i < cycle.size(); i++)
      |                         ~~^~~~~~~~~~~~~~
supertrees.cpp:25:9: warning: unused variable 'N' [-Wunused-variable]
   25 |     int N = p.size();
      |         ^
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for (int i = 0; i < comps.size(); i++)
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 292 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 296 KB Output is correct
6 Correct 11 ms 1176 KB Output is correct
7 Correct 286 ms 23956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 292 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 296 KB Output is correct
6 Correct 11 ms 1176 KB Output is correct
7 Correct 286 ms 23956 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 12 ms 1228 KB Output is correct
13 Correct 307 ms 24056 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 6 ms 844 KB Output is correct
17 Correct 120 ms 14068 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 73 ms 6212 KB Output is correct
21 Correct 230 ms 23928 KB Output is correct
22 Correct 235 ms 24004 KB Output is correct
23 Correct 242 ms 23928 KB Output is correct
24 Correct 236 ms 24016 KB Output is correct
25 Correct 99 ms 14020 KB Output is correct
26 Correct 97 ms 14064 KB Output is correct
27 Correct 268 ms 23928 KB Output is correct
28 Correct 231 ms 24132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 296 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 10 ms 1196 KB Output is correct
9 Correct 260 ms 23988 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 1228 KB Output is correct
13 Correct 238 ms 24060 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 8 ms 844 KB Output is correct
17 Correct 148 ms 14076 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 57 ms 6228 KB Output is correct
22 Correct 233 ms 23976 KB Output is correct
23 Correct 234 ms 23916 KB Output is correct
24 Correct 239 ms 24004 KB Output is correct
25 Correct 97 ms 14020 KB Output is correct
26 Correct 101 ms 14076 KB Output is correct
27 Correct 229 ms 23928 KB Output is correct
28 Correct 322 ms 23988 KB Output is correct
29 Correct 100 ms 14020 KB Output is correct
# Verdict Execution time Memory 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 58 ms 6248 KB Output is correct
5 Correct 234 ms 23928 KB Output is correct
6 Correct 234 ms 24132 KB Output is correct
7 Correct 246 ms 23920 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 60 ms 6244 KB Output is correct
10 Correct 231 ms 23928 KB Output is correct
11 Correct 228 ms 23992 KB Output is correct
12 Correct 260 ms 24008 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 296 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 60 ms 6176 KB Output is correct
17 Correct 232 ms 24088 KB Output is correct
18 Correct 240 ms 23928 KB Output is correct
19 Correct 234 ms 24088 KB Output is correct
20 Correct 238 ms 23972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 292 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 296 KB Output is correct
6 Correct 11 ms 1176 KB Output is correct
7 Correct 286 ms 23956 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 12 ms 1228 KB Output is correct
13 Correct 307 ms 24056 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 6 ms 844 KB Output is correct
17 Correct 120 ms 14068 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 73 ms 6212 KB Output is correct
21 Correct 230 ms 23928 KB Output is correct
22 Correct 235 ms 24004 KB Output is correct
23 Correct 242 ms 23928 KB Output is correct
24 Correct 236 ms 24016 KB Output is correct
25 Correct 99 ms 14020 KB Output is correct
26 Correct 97 ms 14064 KB Output is correct
27 Correct 268 ms 23928 KB Output is correct
28 Correct 231 ms 24132 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 296 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 296 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 10 ms 1196 KB Output is correct
37 Correct 260 ms 23988 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 1228 KB Output is correct
41 Correct 238 ms 24060 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 8 ms 844 KB Output is correct
45 Correct 148 ms 14076 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 292 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 57 ms 6228 KB Output is correct
50 Correct 233 ms 23976 KB Output is correct
51 Correct 234 ms 23916 KB Output is correct
52 Correct 239 ms 24004 KB Output is correct
53 Correct 97 ms 14020 KB Output is correct
54 Correct 101 ms 14076 KB Output is correct
55 Correct 229 ms 23928 KB Output is correct
56 Correct 322 ms 23988 KB Output is correct
57 Correct 100 ms 14020 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 6 ms 844 KB Output is correct
61 Correct 119 ms 13984 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 65 ms 6216 KB Output is correct
66 Correct 100 ms 14064 KB Output is correct
67 Correct 97 ms 14020 KB Output is correct
68 Correct 119 ms 14064 KB Output is correct
69 Correct 118 ms 14060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 292 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 296 KB Output is correct
6 Correct 11 ms 1176 KB Output is correct
7 Correct 286 ms 23956 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 12 ms 1228 KB Output is correct
13 Correct 307 ms 24056 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 6 ms 844 KB Output is correct
17 Correct 120 ms 14068 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 73 ms 6212 KB Output is correct
21 Correct 230 ms 23928 KB Output is correct
22 Correct 235 ms 24004 KB Output is correct
23 Correct 242 ms 23928 KB Output is correct
24 Correct 236 ms 24016 KB Output is correct
25 Correct 99 ms 14020 KB Output is correct
26 Correct 97 ms 14064 KB Output is correct
27 Correct 268 ms 23928 KB Output is correct
28 Correct 231 ms 24132 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 296 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 296 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 10 ms 1196 KB Output is correct
37 Correct 260 ms 23988 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 1228 KB Output is correct
41 Correct 238 ms 24060 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 8 ms 844 KB Output is correct
45 Correct 148 ms 14076 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 292 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 57 ms 6228 KB Output is correct
50 Correct 233 ms 23976 KB Output is correct
51 Correct 234 ms 23916 KB Output is correct
52 Correct 239 ms 24004 KB Output is correct
53 Correct 97 ms 14020 KB Output is correct
54 Correct 101 ms 14076 KB Output is correct
55 Correct 229 ms 23928 KB Output is correct
56 Correct 322 ms 23988 KB Output is correct
57 Correct 100 ms 14020 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 58 ms 6248 KB Output is correct
62 Correct 234 ms 23928 KB Output is correct
63 Correct 234 ms 24132 KB Output is correct
64 Correct 246 ms 23920 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 60 ms 6244 KB Output is correct
67 Correct 231 ms 23928 KB Output is correct
68 Correct 228 ms 23992 KB Output is correct
69 Correct 260 ms 24008 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 296 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 60 ms 6176 KB Output is correct
74 Correct 232 ms 24088 KB Output is correct
75 Correct 240 ms 23928 KB Output is correct
76 Correct 234 ms 24088 KB Output is correct
77 Correct 238 ms 23972 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 6 ms 844 KB Output is correct
81 Correct 119 ms 13984 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 65 ms 6216 KB Output is correct
86 Correct 100 ms 14064 KB Output is correct
87 Correct 97 ms 14020 KB Output is correct
88 Correct 119 ms 14064 KB Output is correct
89 Correct 118 ms 14060 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 664 KB Output is correct
93 Correct 99 ms 10096 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 24 ms 2628 KB Output is correct
98 Correct 113 ms 10104 KB Output is correct
99 Correct 93 ms 10096 KB Output is correct
100 Correct 93 ms 10052 KB Output is correct
101 Correct 95 ms 10016 KB Output is correct
102 Correct 94 ms 10092 KB Output is correct
103 Correct 93 ms 10112 KB Output is correct
104 Correct 91 ms 10104 KB Output is correct
105 Correct 105 ms 10096 KB Output is correct