Submission #306552

# Submission time Handle Problem Language Result Execution time Memory
306552 2020-09-25T20:22:17 Z andreiomd Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
268 ms 27128 KB
#include "supertrees.h"
#include <vector>

using namespace std;

const int NMAX = 1e3 + 1;

int N, roads[NMAX][NMAX];
bool G[NMAX][NMAX];

vector < int > roots;
vector < int > list[NMAX];

vector < vector < int > > ans;

class DSU
{
    static const int NMAX = 1e3 + 1;
    int T[NMAX], Size[NMAX];

private:
    inline int Find (int Node)
    {
        if(Node == T[Node])
            return Node;

        return (T[Node] = Find(T[Node]));
    }

    inline void my_swap (int &x, int &y)
    {
        x = (x ^ y), y = (x ^ y), x = (x ^ y);

        return;
    }

public:
    inline void Initialize (int N)
    {
        for(int i = 1; i <= N; ++i)
            T[i] = i, Size[i] = 1;

        return;
    }

    inline bool Unify (int X, int Y)
    {
        X = Find(X), Y = Find(Y);

        if(X == Y)
            return 0;

        if(Size[X] < Size[Y])
            my_swap(X, Y);

        T[Y] = X;
        Size[X] += Size[Y], Size[Y] = 0;

        return 1;
    }

    inline int Id (int Node)
    {
        return Find(Node);
    }

    inline int Sz (int Id)
    {
        return Size[Id];
    }
} trees, cycles;

static inline void Add_Edge (int X, int Y)
{
    G[X][Y] = G[Y][X] = 1;

    return;
}

static inline void Build_Trees ()
{
    trees.Initialize(N);

    for(int i = 1; i < N; ++i)
        for(int j = (i + 1); j <= N; ++j)
            if(roads[i][j] == 1)
                trees.Unify(i, j);

    for(int i = 1; i <= N; ++i)
    {
        int father = trees.Id(i);

        if(father == i)
            roots.push_back(i);
        else
            Add_Edge(i, father);
    }

    return;
}

static inline void Build_Cycles ()
{
    cycles.Initialize(N);

    for(int i = 0; i < (int)roots.size() - 1; ++i)
        for(int j = (i + 1); j < (int)roots.size(); ++j)
        {
            int X = roots[i], Y = roots[j];

            if(roads[X][Y] == 2)
                cycles.Unify(X, Y);
        }

    for(auto it : roots)
    {
        int father = cycles.Id(it), i = it;

        if(cycles.Sz(father) > 1)
            list[father].push_back(i);
    }

    return;
}

static inline void Go_to_collect_solution ()
{
    for(int i = 1; i <= N; ++i)
    {
        vector < int > line;

        for(int j = 1; j <= N; ++j)
            line.push_back(G[i][j]);

        ans.push_back(line);
    }

    return;
}

int construct (vector < vector < int > > p)
{
    N = (int)p.size();

    for(int i = 1; i <= N; ++i)
        for(int j = 1; j <= N; ++j)
        {
            roads[i][j] = p[i - 1][j - 1];

            if(roads[i][j] > 2)
                return 0;
        }

    Build_Trees();

    for(int i = 1; i < N; ++i)
        for(int j = (i + 1); j <= N; ++j)
            if(roads[i][j] == 0 && trees.Id(i) == trees.Id(j))
                return 0;

    Build_Cycles();

    for(int i = 1; i <= N; ++i)
    {
        if(list[i].empty())
            continue;

        if((int)list[i].size() == 1 || (int)list[i].size() == 2)
            return 0;

        for(int p1 = 0; p1 < (int)list[i].size() - 1; ++p1)
            for(int p2 = (p1 + 1); p2 < (int)list[i].size(); ++p2)
                if(roads[list[i][p1]][list[i][p2]] != 2)
                    return 0;

        for(int p1 = 0; p1 < (int)list[i].size() - 1; ++p1)
            Add_Edge(list[i][p1], list[i][p1 + 1]);

        Add_Edge(list[i][0], list[i].back());
    }

    Go_to_collect_solution(), build(ans);

    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 12 ms 2304 KB Output is correct
7 Correct 262 ms 27128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 12 ms 2304 KB Output is correct
7 Correct 262 ms 27128 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 11 ms 2048 KB Output is correct
13 Correct 255 ms 25976 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 6 ms 1664 KB Output is correct
17 Correct 123 ms 13048 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 66 ms 8312 KB Output is correct
21 Correct 259 ms 27128 KB Output is correct
22 Correct 257 ms 27128 KB Output is correct
23 Correct 267 ms 27048 KB Output is correct
24 Correct 257 ms 26232 KB Output is correct
25 Correct 118 ms 13176 KB Output is correct
26 Correct 116 ms 13176 KB Output is correct
27 Correct 263 ms 27000 KB Output is correct
28 Correct 256 ms 26232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 14 ms 2048 KB Output is correct
9 Correct 263 ms 26100 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 12 ms 2176 KB Output is correct
13 Correct 266 ms 27000 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 6 ms 1408 KB Output is correct
17 Correct 126 ms 12152 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 66 ms 8316 KB Output is correct
22 Correct 260 ms 27120 KB Output is correct
23 Correct 255 ms 27120 KB Output is correct
24 Correct 266 ms 27000 KB Output is correct
25 Correct 114 ms 12408 KB Output is correct
26 Correct 116 ms 12664 KB Output is correct
27 Correct 256 ms 27000 KB Output is correct
28 Correct 268 ms 26992 KB Output is correct
29 Correct 114 ms 12284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 67 ms 8312 KB Output is correct
5 Correct 259 ms 27000 KB Output is correct
6 Correct 256 ms 26992 KB Output is correct
7 Correct 267 ms 27000 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 66 ms 8312 KB Output is correct
10 Correct 261 ms 27000 KB Output is correct
11 Correct 258 ms 26996 KB Output is correct
12 Correct 267 ms 27000 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 65 ms 8312 KB Output is correct
17 Correct 263 ms 27000 KB Output is correct
18 Correct 261 ms 26988 KB Output is correct
19 Correct 261 ms 27000 KB Output is correct
20 Correct 255 ms 26236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 12 ms 2304 KB Output is correct
7 Correct 262 ms 27128 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 11 ms 2048 KB Output is correct
13 Correct 255 ms 25976 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 6 ms 1664 KB Output is correct
17 Correct 123 ms 13048 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 66 ms 8312 KB Output is correct
21 Correct 259 ms 27128 KB Output is correct
22 Correct 257 ms 27128 KB Output is correct
23 Correct 267 ms 27048 KB Output is correct
24 Correct 257 ms 26232 KB Output is correct
25 Correct 118 ms 13176 KB Output is correct
26 Correct 116 ms 13176 KB Output is correct
27 Correct 263 ms 27000 KB Output is correct
28 Correct 256 ms 26232 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 0 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 0 ms 384 KB Output is correct
36 Correct 14 ms 2048 KB Output is correct
37 Correct 263 ms 26100 KB Output is correct
38 Correct 0 ms 384 KB Output is correct
39 Correct 0 ms 384 KB Output is correct
40 Correct 12 ms 2176 KB Output is correct
41 Correct 266 ms 27000 KB Output is correct
42 Correct 0 ms 384 KB Output is correct
43 Correct 0 ms 384 KB Output is correct
44 Correct 6 ms 1408 KB Output is correct
45 Correct 126 ms 12152 KB Output is correct
46 Correct 0 ms 384 KB Output is correct
47 Correct 0 ms 384 KB Output is correct
48 Correct 0 ms 384 KB Output is correct
49 Correct 66 ms 8316 KB Output is correct
50 Correct 260 ms 27120 KB Output is correct
51 Correct 255 ms 27120 KB Output is correct
52 Correct 266 ms 27000 KB Output is correct
53 Correct 114 ms 12408 KB Output is correct
54 Correct 116 ms 12664 KB Output is correct
55 Correct 256 ms 27000 KB Output is correct
56 Correct 268 ms 26992 KB Output is correct
57 Correct 114 ms 12284 KB Output is correct
58 Correct 0 ms 384 KB Output is correct
59 Correct 0 ms 384 KB Output is correct
60 Correct 6 ms 1664 KB Output is correct
61 Correct 121 ms 13164 KB Output is correct
62 Correct 0 ms 384 KB Output is correct
63 Correct 0 ms 384 KB Output is correct
64 Correct 0 ms 384 KB Output is correct
65 Correct 66 ms 8292 KB Output is correct
66 Correct 119 ms 13048 KB Output is correct
67 Correct 116 ms 13048 KB Output is correct
68 Correct 117 ms 13164 KB Output is correct
69 Correct 119 ms 12400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 12 ms 2304 KB Output is correct
7 Correct 262 ms 27128 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 11 ms 2048 KB Output is correct
13 Correct 255 ms 25976 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 6 ms 1664 KB Output is correct
17 Correct 123 ms 13048 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 66 ms 8312 KB Output is correct
21 Correct 259 ms 27128 KB Output is correct
22 Correct 257 ms 27128 KB Output is correct
23 Correct 267 ms 27048 KB Output is correct
24 Correct 257 ms 26232 KB Output is correct
25 Correct 118 ms 13176 KB Output is correct
26 Correct 116 ms 13176 KB Output is correct
27 Correct 263 ms 27000 KB Output is correct
28 Correct 256 ms 26232 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 0 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 0 ms 384 KB Output is correct
36 Correct 14 ms 2048 KB Output is correct
37 Correct 263 ms 26100 KB Output is correct
38 Correct 0 ms 384 KB Output is correct
39 Correct 0 ms 384 KB Output is correct
40 Correct 12 ms 2176 KB Output is correct
41 Correct 266 ms 27000 KB Output is correct
42 Correct 0 ms 384 KB Output is correct
43 Correct 0 ms 384 KB Output is correct
44 Correct 6 ms 1408 KB Output is correct
45 Correct 126 ms 12152 KB Output is correct
46 Correct 0 ms 384 KB Output is correct
47 Correct 0 ms 384 KB Output is correct
48 Correct 0 ms 384 KB Output is correct
49 Correct 66 ms 8316 KB Output is correct
50 Correct 260 ms 27120 KB Output is correct
51 Correct 255 ms 27120 KB Output is correct
52 Correct 266 ms 27000 KB Output is correct
53 Correct 114 ms 12408 KB Output is correct
54 Correct 116 ms 12664 KB Output is correct
55 Correct 256 ms 27000 KB Output is correct
56 Correct 268 ms 26992 KB Output is correct
57 Correct 114 ms 12284 KB Output is correct
58 Correct 0 ms 384 KB Output is correct
59 Correct 0 ms 384 KB Output is correct
60 Correct 0 ms 384 KB Output is correct
61 Correct 67 ms 8312 KB Output is correct
62 Correct 259 ms 27000 KB Output is correct
63 Correct 256 ms 26992 KB Output is correct
64 Correct 267 ms 27000 KB Output is correct
65 Correct 0 ms 384 KB Output is correct
66 Correct 66 ms 8312 KB Output is correct
67 Correct 261 ms 27000 KB Output is correct
68 Correct 258 ms 26996 KB Output is correct
69 Correct 267 ms 27000 KB Output is correct
70 Correct 0 ms 384 KB Output is correct
71 Correct 0 ms 384 KB Output is correct
72 Correct 0 ms 384 KB Output is correct
73 Correct 65 ms 8312 KB Output is correct
74 Correct 263 ms 27000 KB Output is correct
75 Correct 261 ms 26988 KB Output is correct
76 Correct 261 ms 27000 KB Output is correct
77 Correct 255 ms 26236 KB Output is correct
78 Correct 0 ms 384 KB Output is correct
79 Correct 0 ms 384 KB Output is correct
80 Correct 6 ms 1664 KB Output is correct
81 Correct 121 ms 13164 KB Output is correct
82 Correct 0 ms 384 KB Output is correct
83 Correct 0 ms 384 KB Output is correct
84 Correct 0 ms 384 KB Output is correct
85 Correct 66 ms 8292 KB Output is correct
86 Correct 119 ms 13048 KB Output is correct
87 Correct 116 ms 13048 KB Output is correct
88 Correct 117 ms 13164 KB Output is correct
89 Correct 119 ms 12400 KB Output is correct
90 Correct 0 ms 384 KB Output is correct
91 Correct 1 ms 384 KB Output is correct
92 Correct 5 ms 640 KB Output is correct
93 Correct 115 ms 8176 KB Output is correct
94 Correct 0 ms 384 KB Output is correct
95 Correct 0 ms 384 KB Output is correct
96 Correct 0 ms 384 KB Output is correct
97 Correct 28 ms 2424 KB Output is correct
98 Correct 116 ms 8440 KB Output is correct
99 Correct 111 ms 8824 KB Output is correct
100 Correct 108 ms 8568 KB Output is correct
101 Correct 110 ms 9596 KB Output is correct
102 Correct 113 ms 8184 KB Output is correct
103 Correct 116 ms 8184 KB Output is correct
104 Correct 117 ms 8184 KB Output is correct
105 Correct 115 ms 8172 KB Output is correct