Submission #397293

# Submission time Handle Problem Language Result Execution time Memory
397293 2021-05-01T20:35:13 Z ruadhan Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
307 ms 30644 KB
#include "supertrees.h"
#include <bits/stdc++.h>
#define sz(x) (int)x.size()
using namespace std;

// void build(vector<vector<int>> b)
// {
//     for (auto r : b)
//     {
//         for (auto c : r)
//         cerr << c << " ";
//         cerr << '\n';
//     }
//     cerr << '\n';
// }

const int MX = 1005;
int N;
vector<int> adj[MX];

vector<int> component, vis(MX);
void dfs(int u)
{
    // cerr << "dfs on " << u << '\n';
    vis[u] = 1;
    component.push_back(u);
    for (int v : adj[u])
        if (!vis[v])
            dfs(v);
}

vector<int> tree[MX];
vector<int> vis2(MX), outside_cycle(MX);
vector<bool> in_tree(MX);
void dfs2(int u)
{
    // cerr << "doing dfs2 on " << u << '\n';
    vis2[u] = 1;
    outside_cycle.push_back(u);
    for (int v : tree[u])
        if (!vis2[v])
            dfs2(v);
}

int construct(vector<vector<int>> p)
{
    N = p.size();
    // cerr << "N = " << N << '\n';
    for (int i = 0; i < N; i++)
    {
        for (int j = i + 1; j < N; j++)
        {
            if (p[i][j] == 3)
                return 0;
            if (p[i][j] > 0)
            {
                adj[i].push_back(j);
                adj[j].push_back(i);
            }
        }
    }

    // cerr << "created adjacency list " << '\n';

    vector<vector<int>> ans(N, vector<int>(N));

    for (int node = 0; node < N; node++)
    {
        // cerr << "N = " << N << '\n';
        // cerr << "node = " << node << '\n';

        if (!vis[node])
        {
            component.clear();
            dfs(node);

            int n = sz(component);
            // cerr << "size of component = n = " << n << '\n';
            for (int i = 0; i < n; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    int u = component[i], v = component[j];
                    // cerr << "i = " << i << ", j = " << j << '\n';
                    if (p[u][v] == 0)
                        return 0;
                    if (p[u][v] == 1)
                    {
                        tree[u].push_back(v);
                        tree[v].push_back(u);
                    }
                }
            }
            // cerr << "tree counting finished\n";
            vector<int> cycle;
            for (int idx : component)
                if (!vis2[idx])
                {
                    outside_cycle.clear();
                    dfs2(idx);
                    // cerr << "finished doing dfs2\n";
                    for (int u : outside_cycle)
                        in_tree[u] = true;
                    for (int u : outside_cycle)
                    {
                        for (int v : component)
                        {
                            if (in_tree[v] && p[u][v] != 1)
                            {
                                // cerr << "returning 0" << '\n';
                                return 0;
                            }
                            if (!in_tree[v] && p[u][v] != 2)
                            {
                                // cerr << "returning 0" << '\n';
                                return 0;
                            }
                        }
                    }
                    for (int i = 0; i < sz(outside_cycle) - 1; i++)
                    {
                        int u = outside_cycle[i], v = outside_cycle[i + 1];
                        ans[u][v] = ans[v][u] = 1;
                    }
                    cycle.push_back(outside_cycle[0]);
                    for (int u : outside_cycle)
                        in_tree[u] = false;
                }
            // cerr << "sz(cycle) = " << sz(cycle) << '\n';
            if (sz(cycle) == 2)
                return 0;
            if (sz(cycle) > 2)
            {
                for (int i = 0; i < sz(cycle); i++)
                {
                    int u = cycle[i], v = cycle[(i + 1 == sz(cycle) ? 0 : i + 1)];
                    ans[u][v] = ans[v][u] = 1;
                }
            }
        }
    }
    build(ans);
    return 1;
}

// int main()
// {
//     int n;
//     cin >> n;
//     vector<vector<int>> in(n, vector<int>(n));
//     for (auto &r : in)
//     {
//         for (auto &c : r)
//             cin >> c;
//     }
//     for (auto x : in)
//     {
//         for (auto y : x)
//             // cerr << y << " ";
//         // cerr << '\n';
//     }
//     construct(in);
//     return 0;
// }
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 13 ms 1696 KB Output is correct
7 Correct 292 ms 30644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 13 ms 1696 KB Output is correct
7 Correct 292 ms 30644 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 356 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 1236 KB Output is correct
13 Correct 260 ms 24072 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 5 ms 972 KB Output is correct
17 Correct 114 ms 16944 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 272 KB Output is correct
20 Correct 81 ms 6316 KB Output is correct
21 Correct 246 ms 25028 KB Output is correct
22 Correct 256 ms 24136 KB Output is correct
23 Correct 301 ms 28116 KB Output is correct
24 Correct 250 ms 24048 KB Output is correct
25 Correct 104 ms 14908 KB Output is correct
26 Correct 101 ms 14120 KB Output is correct
27 Correct 261 ms 30256 KB Output is correct
28 Correct 262 ms 23992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 356 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 14 ms 1228 KB Output is correct
9 Correct 261 ms 23936 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 1484 KB Output is correct
13 Correct 253 ms 28216 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 1112 KB Output is correct
17 Correct 126 ms 16944 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 352 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 74 ms 6300 KB Output is correct
22 Correct 254 ms 24560 KB Output is correct
23 Correct 261 ms 24084 KB Output is correct
24 Correct 269 ms 27096 KB Output is correct
25 Correct 112 ms 14132 KB Output is correct
26 Correct 114 ms 14624 KB Output is correct
27 Correct 253 ms 24008 KB Output is correct
28 Correct 305 ms 26124 KB Output is correct
29 Correct 116 ms 14120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 65 ms 6316 KB Output is correct
5 Correct 261 ms 25036 KB Output is correct
6 Correct 307 ms 24052 KB Output is correct
7 Correct 267 ms 28100 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 66 ms 6320 KB Output is correct
10 Correct 254 ms 24496 KB Output is correct
11 Correct 250 ms 24124 KB Output is correct
12 Correct 269 ms 27164 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 63 ms 6328 KB Output is correct
17 Correct 250 ms 24548 KB Output is correct
18 Correct 252 ms 24644 KB Output is correct
19 Correct 248 ms 24260 KB Output is correct
20 Correct 260 ms 23960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 13 ms 1696 KB Output is correct
7 Correct 292 ms 30644 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 356 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 1236 KB Output is correct
13 Correct 260 ms 24072 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 5 ms 972 KB Output is correct
17 Correct 114 ms 16944 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 272 KB Output is correct
20 Correct 81 ms 6316 KB Output is correct
21 Correct 246 ms 25028 KB Output is correct
22 Correct 256 ms 24136 KB Output is correct
23 Correct 301 ms 28116 KB Output is correct
24 Correct 250 ms 24048 KB Output is correct
25 Correct 104 ms 14908 KB Output is correct
26 Correct 101 ms 14120 KB Output is correct
27 Correct 261 ms 30256 KB Output is correct
28 Correct 262 ms 23992 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 356 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 352 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 14 ms 1228 KB Output is correct
37 Correct 261 ms 23936 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 11 ms 1484 KB Output is correct
41 Correct 253 ms 28216 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 6 ms 1112 KB Output is correct
45 Correct 126 ms 16944 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 352 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 74 ms 6300 KB Output is correct
50 Correct 254 ms 24560 KB Output is correct
51 Correct 261 ms 24084 KB Output is correct
52 Correct 269 ms 27096 KB Output is correct
53 Correct 112 ms 14132 KB Output is correct
54 Correct 114 ms 14624 KB Output is correct
55 Correct 253 ms 24008 KB Output is correct
56 Correct 305 ms 26124 KB Output is correct
57 Correct 116 ms 14120 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 6 ms 1100 KB Output is correct
61 Correct 112 ms 18656 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 66 ms 6312 KB Output is correct
66 Correct 111 ms 14780 KB Output is correct
67 Correct 104 ms 14648 KB Output is correct
68 Correct 101 ms 14380 KB Output is correct
69 Correct 102 ms 14132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 13 ms 1696 KB Output is correct
7 Correct 292 ms 30644 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 356 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 1236 KB Output is correct
13 Correct 260 ms 24072 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 5 ms 972 KB Output is correct
17 Correct 114 ms 16944 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 272 KB Output is correct
20 Correct 81 ms 6316 KB Output is correct
21 Correct 246 ms 25028 KB Output is correct
22 Correct 256 ms 24136 KB Output is correct
23 Correct 301 ms 28116 KB Output is correct
24 Correct 250 ms 24048 KB Output is correct
25 Correct 104 ms 14908 KB Output is correct
26 Correct 101 ms 14120 KB Output is correct
27 Correct 261 ms 30256 KB Output is correct
28 Correct 262 ms 23992 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 356 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 352 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 14 ms 1228 KB Output is correct
37 Correct 261 ms 23936 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 11 ms 1484 KB Output is correct
41 Correct 253 ms 28216 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 6 ms 1112 KB Output is correct
45 Correct 126 ms 16944 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 352 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 74 ms 6300 KB Output is correct
50 Correct 254 ms 24560 KB Output is correct
51 Correct 261 ms 24084 KB Output is correct
52 Correct 269 ms 27096 KB Output is correct
53 Correct 112 ms 14132 KB Output is correct
54 Correct 114 ms 14624 KB Output is correct
55 Correct 253 ms 24008 KB Output is correct
56 Correct 305 ms 26124 KB Output is correct
57 Correct 116 ms 14120 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 65 ms 6316 KB Output is correct
62 Correct 261 ms 25036 KB Output is correct
63 Correct 307 ms 24052 KB Output is correct
64 Correct 267 ms 28100 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 66 ms 6320 KB Output is correct
67 Correct 254 ms 24496 KB Output is correct
68 Correct 250 ms 24124 KB Output is correct
69 Correct 269 ms 27164 KB Output is correct
70 Correct 1 ms 332 KB Output is correct
71 Correct 1 ms 332 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 63 ms 6328 KB Output is correct
74 Correct 250 ms 24548 KB Output is correct
75 Correct 252 ms 24644 KB Output is correct
76 Correct 248 ms 24260 KB Output is correct
77 Correct 260 ms 23960 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 6 ms 1100 KB Output is correct
81 Correct 112 ms 18656 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 66 ms 6312 KB Output is correct
86 Correct 111 ms 14780 KB Output is correct
87 Correct 104 ms 14648 KB Output is correct
88 Correct 101 ms 14380 KB Output is correct
89 Correct 102 ms 14132 KB Output is correct
90 Correct 1 ms 332 KB Output is correct
91 Correct 1 ms 332 KB Output is correct
92 Correct 5 ms 716 KB Output is correct
93 Correct 99 ms 10152 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 332 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 29 ms 2740 KB Output is correct
98 Correct 98 ms 10156 KB Output is correct
99 Correct 99 ms 10280 KB Output is correct
100 Correct 115 ms 10180 KB Output is correct
101 Correct 106 ms 10164 KB Output is correct
102 Correct 89 ms 10156 KB Output is correct
103 Correct 88 ms 10180 KB Output is correct
104 Correct 89 ms 10168 KB Output is correct
105 Correct 101 ms 10172 KB Output is correct