Submission #410061

# Submission time Handle Problem Language Result Execution time Memory
410061 2021-05-21T22:47:13 Z 534351 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
250 ms 26080 KB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int) (x).size())

const int MAXN = 1013;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N;
int grid[MAXN][MAXN];
int dsu[MAXN], cc[MAXN], cc1[MAXN];
vi heads[MAXN];
vector<vi> ans;

int get(int u)
{
    return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
bool merge(int u, int v)
{
    u = get(u);
    v = get(v);
    if (u == v) return false;
    dsu[u] = v;
    return true;
}
void addedge(int i, int j)
{
    if (i == j) return;
    ans[i][j] = 1;
    ans[j][i] = 1;
}

int construct(vector<vi> p)
{
	N = SZ(p);
    FOR(i, 0, N)
    {
        FOR(j, 0, N)
        {
            grid[i][j] = p[i][j];
            if (grid[i][j] == 3)
            {
                return 0;
            }
        }
    }
    ans.resize(N);
    FOR(i, 0, N)
    {
        ans[i].resize(N);
    }
    iota(dsu, dsu + N, 0);
    FOR(i, 0, N)
    {
        FOR(j, 0, N)
        {
            if (grid[i][j] != 0)
            {
                merge(i, j);
            }
        }
    }
    FOR(i, 0, N)
    {
        cc[i] = get(i);
    }
    FOR(i, 0, N)
    {
        FOR(j, 0, N)
        {
            if (cc[i] == cc[j] && grid[i][j] == 0)
            {
                return 0;
            }
        }
    }
    iota(dsu, dsu + N, 0);
    FOR(i, 0, N)
    {
        FOR(j, 0, N)
        {
            if (grid[i][j] == 1)
            {
                merge(i, j);
            }
        }
    }
    FOR(i, 0, N)
    {
        cc1[i] = get(i);
    }
    FOR(i, 0, N)
    {
        FOR(j, 0, N)
        {
            if (cc1[i] == cc1[j] && grid[i][j] != 1)
            {
                return 0;
            }
        }
        addedge(i, cc1[i]);
    }
    FOR(i, 0, N)
    {
        if (cc1[i] == i)
        {
            heads[cc[i]].PB(i);
        }
    }
    FOR(i, 0, N)
    {
        if (cc[i] == i)
        {
            if (SZ(heads[i]) == 2)
            {
                return 0;
            }
            FOR(j, 0, SZ(heads[i]) - 1)
            {
                addedge(heads[i][j], heads[i][j + 1]);
            }
            addedge(heads[i][0], heads[i].back());
        }
    }
    build(ans);
    //first connect all the stuff that are 1's. if you get any 0's in between then bad.
    return 1;
}
# 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 11 ms 1992 KB Output is correct
7 Correct 231 ms 26028 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 11 ms 1992 KB Output is correct
7 Correct 231 ms 26028 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 10 ms 2080 KB Output is correct
13 Correct 231 ms 26024 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 1484 KB Output is correct
17 Correct 115 ms 16040 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 61 ms 7732 KB Output is correct
21 Correct 232 ms 25924 KB Output is correct
22 Correct 235 ms 26052 KB Output is correct
23 Correct 245 ms 25924 KB Output is correct
24 Correct 233 ms 26052 KB Output is correct
25 Correct 94 ms 16068 KB Output is correct
26 Correct 92 ms 16040 KB Output is correct
27 Correct 250 ms 25972 KB Output is correct
28 Correct 223 ms 26056 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 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 10 ms 2028 KB Output is correct
9 Correct 225 ms 26004 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 14 ms 1948 KB Output is correct
13 Correct 230 ms 26008 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 1508 KB Output is correct
17 Correct 107 ms 16036 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 58 ms 7716 KB Output is correct
22 Correct 224 ms 26020 KB Output is correct
23 Correct 226 ms 26012 KB Output is correct
24 Correct 236 ms 26060 KB Output is correct
25 Correct 100 ms 16036 KB Output is correct
26 Correct 95 ms 16028 KB Output is correct
27 Correct 222 ms 26056 KB Output is correct
28 Correct 240 ms 26020 KB Output is correct
29 Correct 94 ms 16156 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 60 ms 7720 KB Output is correct
5 Correct 233 ms 26028 KB Output is correct
6 Correct 235 ms 26052 KB Output is correct
7 Correct 248 ms 26052 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 60 ms 7708 KB Output is correct
10 Correct 223 ms 26016 KB Output is correct
11 Correct 230 ms 26012 KB Output is correct
12 Correct 241 ms 26024 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 59 ms 7756 KB Output is correct
17 Correct 225 ms 26020 KB Output is correct
18 Correct 231 ms 26044 KB Output is correct
19 Correct 239 ms 26080 KB Output is correct
20 Correct 224 ms 26052 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 11 ms 1992 KB Output is correct
7 Correct 231 ms 26028 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 10 ms 2080 KB Output is correct
13 Correct 231 ms 26024 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 1484 KB Output is correct
17 Correct 115 ms 16040 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 61 ms 7732 KB Output is correct
21 Correct 232 ms 25924 KB Output is correct
22 Correct 235 ms 26052 KB Output is correct
23 Correct 245 ms 25924 KB Output is correct
24 Correct 233 ms 26052 KB Output is correct
25 Correct 94 ms 16068 KB Output is correct
26 Correct 92 ms 16040 KB Output is correct
27 Correct 250 ms 25972 KB Output is correct
28 Correct 223 ms 26056 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 10 ms 2028 KB Output is correct
37 Correct 225 ms 26004 KB Output is correct
38 Correct 0 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 14 ms 1948 KB Output is correct
41 Correct 230 ms 26008 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 5 ms 1508 KB Output is correct
45 Correct 107 ms 16036 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 58 ms 7716 KB Output is correct
50 Correct 224 ms 26020 KB Output is correct
51 Correct 226 ms 26012 KB Output is correct
52 Correct 236 ms 26060 KB Output is correct
53 Correct 100 ms 16036 KB Output is correct
54 Correct 95 ms 16028 KB Output is correct
55 Correct 222 ms 26056 KB Output is correct
56 Correct 240 ms 26020 KB Output is correct
57 Correct 94 ms 16156 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 7 ms 1512 KB Output is correct
61 Correct 115 ms 16032 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 57 ms 7708 KB Output is correct
66 Correct 95 ms 16024 KB Output is correct
67 Correct 96 ms 16040 KB Output is correct
68 Correct 102 ms 16024 KB Output is correct
69 Correct 95 ms 16040 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 11 ms 1992 KB Output is correct
7 Correct 231 ms 26028 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 10 ms 2080 KB Output is correct
13 Correct 231 ms 26024 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 1484 KB Output is correct
17 Correct 115 ms 16040 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 61 ms 7732 KB Output is correct
21 Correct 232 ms 25924 KB Output is correct
22 Correct 235 ms 26052 KB Output is correct
23 Correct 245 ms 25924 KB Output is correct
24 Correct 233 ms 26052 KB Output is correct
25 Correct 94 ms 16068 KB Output is correct
26 Correct 92 ms 16040 KB Output is correct
27 Correct 250 ms 25972 KB Output is correct
28 Correct 223 ms 26056 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 10 ms 2028 KB Output is correct
37 Correct 225 ms 26004 KB Output is correct
38 Correct 0 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 14 ms 1948 KB Output is correct
41 Correct 230 ms 26008 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 5 ms 1508 KB Output is correct
45 Correct 107 ms 16036 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 58 ms 7716 KB Output is correct
50 Correct 224 ms 26020 KB Output is correct
51 Correct 226 ms 26012 KB Output is correct
52 Correct 236 ms 26060 KB Output is correct
53 Correct 100 ms 16036 KB Output is correct
54 Correct 95 ms 16028 KB Output is correct
55 Correct 222 ms 26056 KB Output is correct
56 Correct 240 ms 26020 KB Output is correct
57 Correct 94 ms 16156 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 60 ms 7720 KB Output is correct
62 Correct 233 ms 26028 KB Output is correct
63 Correct 235 ms 26052 KB Output is correct
64 Correct 248 ms 26052 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 60 ms 7708 KB Output is correct
67 Correct 223 ms 26016 KB Output is correct
68 Correct 230 ms 26012 KB Output is correct
69 Correct 241 ms 26024 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 59 ms 7756 KB Output is correct
74 Correct 225 ms 26020 KB Output is correct
75 Correct 231 ms 26044 KB Output is correct
76 Correct 239 ms 26080 KB Output is correct
77 Correct 224 ms 26052 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 7 ms 1512 KB Output is correct
81 Correct 115 ms 16032 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 57 ms 7708 KB Output is correct
86 Correct 95 ms 16024 KB Output is correct
87 Correct 96 ms 16040 KB Output is correct
88 Correct 102 ms 16024 KB Output is correct
89 Correct 95 ms 16040 KB Output is correct
90 Correct 1 ms 204 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 4 ms 588 KB Output is correct
93 Correct 91 ms 8092 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 248 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 27 ms 2828 KB Output is correct
98 Correct 93 ms 10308 KB Output is correct
99 Correct 102 ms 10644 KB Output is correct
100 Correct 89 ms 10396 KB Output is correct
101 Correct 90 ms 11408 KB Output is correct
102 Correct 87 ms 10136 KB Output is correct
103 Correct 90 ms 10128 KB Output is correct
104 Correct 91 ms 10144 KB Output is correct
105 Correct 98 ms 10128 KB Output is correct