Submission #835958

# Submission time Handle Problem Language Result Execution time Memory
835958 2023-08-24T00:54:49 Z maomao90 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
195 ms 26144 KB
// I can do all things through Christ who strengthens me
// Philippians 4:13

#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

#define REP(i, j, k) for (int i = j; i < (k); i++)
#define RREP(i, j, k) for (int i = j; i >= (k); i--)

template <class T>
inline bool mnto(T &a, const T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T &a, const T b) {return a < b ? a = b, 1 : 0;}

typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int) x.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef tuple<int, int, int> iii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005;
const int MAXN = 1005;

int n;
vector<vi> g;
vector<vi> ans;
bool vis[MAXN];

bool inc[MAXN];
bool solve(vi comp) {
    if (SZ(comp) == 1) {
        return 1;
    }
    vi cyc, line;
    for (int i : comp) {
        bool all2 = 1;
        for (int j : comp) {
            if (i == j) {
                continue;
            }
            if (g[i][j] != 2) {
                all2 = 0;
                break;
            }
        }
        if (all2) {
            cyc.pb(i);
        } else {
            line.pb(i);
        }
    }
    for (int i : line) {
        if (inc[i]) {
            continue;
        }
        vi chain;
        for (int j : line) {
            if (g[i][j] == 1) {
                chain.pb(j);
            }
        }
        for (int j : chain) {
            inc[j] = 1;
            for (int k : line) {
                if (g[i][k] != g[j][k]) {
                    return 0;
                }
            }
        }
        REP (j, 0, SZ(chain) - 1) {
            ans[chain[j]][chain[j + 1]] = 1;
            ans[chain[j + 1]][chain[j]] = 1;
        }
        cyc.pb(chain[0]);
        if (SZ(chain) == SZ(comp)) {
            return 1;
        }
    }
    if (SZ(cyc) <= 2) {
        return 0;
    }
    REP (i, 0, SZ(cyc)) {
        ans[cyc[i]][cyc[(i + 1) % SZ(cyc)]] = 1;
        ans[cyc[(i + 1) % SZ(cyc)]][cyc[i]] = 1;
    }
    return 1;
    /*
    if (SZ(cyc) == 1) {
        return 0;
    }
    REP (i, 0, SZ(line) - 1) {
        ans[line[i]][line[i + 1]] = 1;
        ans[line[i + 1]][line[i]] = 1;
    }
    if (!cyc.empty()) {
        REP (i, 0, SZ(cyc) - 1) {
            ans[cyc[i]][cyc[i + 1]] = 1;
            ans[cyc[i + 1]][cyc[i]] = 1;
        }
        ans[line[0]][cyc[0]] = 1;
        ans[cyc[0]][line[0]] = 1;
        ans[line[0]][cyc.back()] = 1;
        ans[cyc.back()][line[0]] = 1;
    }
    */
    return 1;
}

int construct(vector<vi> p) {
    n = SZ(p);
    ans = vector<vi>(n, vi(n, 0));
    g = p;
    REP (i, 0, n) {
        REP (j, 0, n) {
            if (g[i][j] == 3) {
                return 0;
            }
        }
    }
    REP (i, 0, n) {
        if (vis[i]) {
            continue;
        }
        vi comp;
        REP (j, 0, n) {
            if (g[i][j]) {
                comp.pb(j);
            }
        }
        for (int j : comp) {
            vis[j] = 1;
            REP (k, 0, n) {
                if ((g[j][k] > 0) != (g[i][k] > 0)) {
                    return 0;
                }
            }
        }
        if (!solve(comp)) {
            return 0;
        }
    }
    build(ans);
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1364 KB Output is correct
7 Correct 166 ms 25932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1364 KB Output is correct
7 Correct 166 ms 25932 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 158 ms 26004 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 75 ms 16064 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 39 ms 6732 KB Output is correct
21 Correct 155 ms 25992 KB Output is correct
22 Correct 154 ms 25996 KB Output is correct
23 Correct 165 ms 25992 KB Output is correct
24 Correct 156 ms 25884 KB Output is correct
25 Correct 70 ms 16076 KB Output is correct
26 Correct 62 ms 16076 KB Output is correct
27 Correct 164 ms 25936 KB Output is correct
28 Correct 156 ms 25936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 9 ms 1236 KB Output is correct
9 Correct 158 ms 25948 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 167 ms 25992 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 260 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 70 ms 16008 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 41 ms 6824 KB Output is correct
22 Correct 162 ms 25932 KB Output is correct
23 Correct 157 ms 25916 KB Output is correct
24 Correct 166 ms 26000 KB Output is correct
25 Correct 61 ms 16004 KB Output is correct
26 Correct 91 ms 16008 KB Output is correct
27 Correct 155 ms 25932 KB Output is correct
28 Correct 195 ms 25896 KB Output is correct
29 Correct 66 ms 16076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 43 ms 6800 KB Output is correct
5 Correct 157 ms 25912 KB Output is correct
6 Correct 178 ms 25892 KB Output is correct
7 Correct 167 ms 25932 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 39 ms 6728 KB Output is correct
10 Correct 156 ms 25932 KB Output is correct
11 Correct 156 ms 25932 KB Output is correct
12 Correct 166 ms 25948 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 50 ms 6648 KB Output is correct
17 Correct 160 ms 26144 KB Output is correct
18 Correct 159 ms 26092 KB Output is correct
19 Correct 193 ms 25932 KB Output is correct
20 Correct 177 ms 25992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1364 KB Output is correct
7 Correct 166 ms 25932 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 158 ms 26004 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 75 ms 16064 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 39 ms 6732 KB Output is correct
21 Correct 155 ms 25992 KB Output is correct
22 Correct 154 ms 25996 KB Output is correct
23 Correct 165 ms 25992 KB Output is correct
24 Correct 156 ms 25884 KB Output is correct
25 Correct 70 ms 16076 KB Output is correct
26 Correct 62 ms 16076 KB Output is correct
27 Correct 164 ms 25936 KB Output is correct
28 Correct 156 ms 25936 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 9 ms 1236 KB Output is correct
37 Correct 158 ms 25948 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1364 KB Output is correct
41 Correct 167 ms 25992 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 260 KB Output is correct
44 Correct 3 ms 852 KB Output is correct
45 Correct 70 ms 16008 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 41 ms 6824 KB Output is correct
50 Correct 162 ms 25932 KB Output is correct
51 Correct 157 ms 25916 KB Output is correct
52 Correct 166 ms 26000 KB Output is correct
53 Correct 61 ms 16004 KB Output is correct
54 Correct 91 ms 16008 KB Output is correct
55 Correct 155 ms 25932 KB Output is correct
56 Correct 195 ms 25896 KB Output is correct
57 Correct 66 ms 16076 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 3 ms 852 KB Output is correct
61 Correct 74 ms 16012 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 39 ms 6676 KB Output is correct
66 Correct 66 ms 17996 KB Output is correct
67 Correct 64 ms 18040 KB Output is correct
68 Correct 66 ms 18040 KB Output is correct
69 Correct 64 ms 18036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1364 KB Output is correct
7 Correct 166 ms 25932 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 158 ms 26004 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 75 ms 16064 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 39 ms 6732 KB Output is correct
21 Correct 155 ms 25992 KB Output is correct
22 Correct 154 ms 25996 KB Output is correct
23 Correct 165 ms 25992 KB Output is correct
24 Correct 156 ms 25884 KB Output is correct
25 Correct 70 ms 16076 KB Output is correct
26 Correct 62 ms 16076 KB Output is correct
27 Correct 164 ms 25936 KB Output is correct
28 Correct 156 ms 25936 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 9 ms 1236 KB Output is correct
37 Correct 158 ms 25948 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1364 KB Output is correct
41 Correct 167 ms 25992 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 260 KB Output is correct
44 Correct 3 ms 852 KB Output is correct
45 Correct 70 ms 16008 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 41 ms 6824 KB Output is correct
50 Correct 162 ms 25932 KB Output is correct
51 Correct 157 ms 25916 KB Output is correct
52 Correct 166 ms 26000 KB Output is correct
53 Correct 61 ms 16004 KB Output is correct
54 Correct 91 ms 16008 KB Output is correct
55 Correct 155 ms 25932 KB Output is correct
56 Correct 195 ms 25896 KB Output is correct
57 Correct 66 ms 16076 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 43 ms 6800 KB Output is correct
62 Correct 157 ms 25912 KB Output is correct
63 Correct 178 ms 25892 KB Output is correct
64 Correct 167 ms 25932 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 39 ms 6728 KB Output is correct
67 Correct 156 ms 25932 KB Output is correct
68 Correct 156 ms 25932 KB Output is correct
69 Correct 166 ms 25948 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 50 ms 6648 KB Output is correct
74 Correct 160 ms 26144 KB Output is correct
75 Correct 159 ms 26092 KB Output is correct
76 Correct 193 ms 25932 KB Output is correct
77 Correct 177 ms 25992 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 3 ms 852 KB Output is correct
81 Correct 74 ms 16012 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 39 ms 6676 KB Output is correct
86 Correct 66 ms 17996 KB Output is correct
87 Correct 64 ms 18040 KB Output is correct
88 Correct 66 ms 18040 KB Output is correct
89 Correct 64 ms 18036 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 3 ms 980 KB Output is correct
93 Correct 72 ms 17924 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 304 KB Output is correct
97 Correct 16 ms 4692 KB Output is correct
98 Correct 64 ms 18040 KB Output is correct
99 Correct 63 ms 17964 KB Output is correct
100 Correct 62 ms 17976 KB Output is correct
101 Correct 62 ms 17996 KB Output is correct
102 Correct 83 ms 18036 KB Output is correct
103 Correct 71 ms 17996 KB Output is correct
104 Correct 78 ms 18124 KB Output is correct
105 Correct 70 ms 17996 KB Output is correct