Submission #800102

# Submission time Handle Problem Language Result Execution time Memory
800102 2023-08-01T10:14:40 Z becaido Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
166 ms 24132 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "supertrees.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

#ifdef WAIMAI
void build(vector<vector<int>> b);
#endif

const int SIZE = 1005;

int n, mx;
int g[SIZE];
bool vs[SIZE];
vector<int> all[SIZE];
vector<vector<int>> ans;

int construct(vector<vector<int>> p) {
	n = p.size();
	mx = 0;
	ans.clear();
	FOR (i, 0, n - 1) {
        g[i] = -1;
        vs[i] = 0;
        all[i].clear();
        ans.pb(vector<int>(n));
        mx = max(mx, *max_element(p[i].begin(), p[i].end()));
	}
	if (mx == 3) return 0;
	FOR (i, 0, n - 1) if (!vs[i]) {
        vector<int> v;
        queue<int> q;
        vs[i] = 1, q.push(i);
        while (q.size()) {
            int pos = q.front();
            q.pop();
            v.pb(pos);
            FOR (np, 0, n - 1) if (p[pos][np] == 1 && !vs[np]) {
                q.push(np);
                vs[np] = 1;
            }
        }
        for (int x : v) for (int y : v) if (p[x][y] != 1) return 0;
        FOR (j, 1, v.size() - 1) ans[v[j]][v[j - 1]] = ans[v[j - 1]][v[j]] = 1;
        for (int x : v) {
            all[v[0]].pb(x);
            g[x] = v[0];
        }
	}
	fill(vs, vs + n, 0);
	FOR (i, 0, n - 1) if (!vs[g[i]]) {
        vector<int> v;
        queue<int> q;
        vs[g[i]] = 1, q.push(g[i]);
        while (q.size()) {
            int pos = q.front();
            q.pop();
            v.pb(pos);
            for (int x : all[pos]) {
                FOR (y, 0, n - 1) if (p[x][y] == 2 && !vs[g[y]]) {
                    int np = g[y];
                    vs[np] = 1;
                    q.push(np);
                }
            }
        }
        if (v.size() == 1) continue;
        if (v.size() == 2) return 0;
        for (int x : v) for (int y : v) if (x != y) {
            for (int xi : all[x]) for (int yi : all[y]) {
                if (p[xi][yi] != 2) return 0;
            }
        }
        FOR (j, 1, v.size() - 1) ans[v[j]][v[j - 1]] = ans[v[j - 1]][v[j]] = 1;
        ans[v[0]][v.back()] = ans[v.back()][v[0]] = 1;
	}
	build(ans);
	return 1;
}

/*
in1
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
out1
1
0 1 0 0
1 0 1 1
0 1 0 1
0 1 1 0

in2
2
1 0
0 1
out2
1
0 0
0 0

in3
2
1 3
3 1
out3
0
*/

#ifdef WAIMAI
static int nn;
static vector<vector<int>> pp;
static vector<vector<int>> bb;
static bool called = false;

static void check(bool cond, string message) {
    if (!cond) {
        printf("%s\n", message.c_str());
        fclose(stdout);
        exit(0);
    }
}

void build(vector<vector<int>> _b) {
    check(!called, "build is called more than once");
    called = true;
    check((int)_b.size() == nn, "Invalid number of rows in b");
    for (int i = 0; i < nn; i++) {
        check((int)_b[i].size() == nn, "Invalid number of columns in b");
    }
    bb = _b;
}

int main() {
    assert(scanf("%d", &nn) == 1);

    pp.resize(nn);
    for (int i = 0; i < nn; i++) {
        pp[i].resize(nn);
    }

    for (int i = 0; i < nn; i++) {
        for (int j = 0; j < nn; j++) {
            assert(scanf("%d", &pp[i][j]) == 1);
        }
    }
    fclose(stdin);

    int possible = construct(pp);

    check(possible == 0 || possible == 1, "Invalid return value of construct");
    if (possible == 1) {
        check(called, "construct returned 1 without calling build");
    } else {
        check(!called, "construct called build but returned 0");
    }

    printf("%d\n", possible);
    if (possible == 1) {
        for (int i = 0; i < nn; i++) {
            for (int j = 0; j < nn; j++) {
                if (j) {
                    printf(" ");
                }
                printf("%d", bb[i][j]);
            }
            printf("\n");
        }
    }
    fclose(stdout);
}
#endif
# 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 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 6 ms 1108 KB Output is correct
7 Correct 152 ms 22048 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 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 6 ms 1108 KB Output is correct
7 Correct 152 ms 22048 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 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 6 ms 1236 KB Output is correct
13 Correct 157 ms 22048 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 76 ms 12076 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 5684 KB Output is correct
21 Correct 155 ms 22056 KB Output is correct
22 Correct 151 ms 22012 KB Output is correct
23 Correct 161 ms 22016 KB Output is correct
24 Correct 153 ms 21984 KB Output is correct
25 Correct 62 ms 12072 KB Output is correct
26 Correct 61 ms 12064 KB Output is correct
27 Correct 157 ms 22300 KB Output is correct
28 Correct 153 ms 22044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 320 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 6 ms 1236 KB Output is correct
9 Correct 152 ms 22064 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 1216 KB Output is correct
13 Correct 156 ms 22052 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 71 ms 12192 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 38 ms 5788 KB Output is correct
22 Correct 150 ms 22060 KB Output is correct
23 Correct 150 ms 22024 KB Output is correct
24 Correct 157 ms 22060 KB Output is correct
25 Correct 60 ms 12188 KB Output is correct
26 Correct 62 ms 12192 KB Output is correct
27 Correct 152 ms 22012 KB Output is correct
28 Correct 156 ms 22092 KB Output is correct
29 Correct 61 ms 12192 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 38 ms 5708 KB Output is correct
5 Correct 160 ms 22044 KB Output is correct
6 Correct 153 ms 22044 KB Output is correct
7 Correct 156 ms 22032 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 38 ms 5800 KB Output is correct
10 Correct 152 ms 22052 KB Output is correct
11 Correct 152 ms 22092 KB Output is correct
12 Correct 166 ms 21996 KB Output is correct
13 Correct 0 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 38 ms 6284 KB Output is correct
17 Correct 153 ms 24060 KB Output is correct
18 Correct 154 ms 24044 KB Output is correct
19 Correct 152 ms 23952 KB Output is correct
20 Correct 158 ms 24132 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 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 6 ms 1108 KB Output is correct
7 Correct 152 ms 22048 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 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 6 ms 1236 KB Output is correct
13 Correct 157 ms 22048 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 76 ms 12076 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 5684 KB Output is correct
21 Correct 155 ms 22056 KB Output is correct
22 Correct 151 ms 22012 KB Output is correct
23 Correct 161 ms 22016 KB Output is correct
24 Correct 153 ms 21984 KB Output is correct
25 Correct 62 ms 12072 KB Output is correct
26 Correct 61 ms 12064 KB Output is correct
27 Correct 157 ms 22300 KB Output is correct
28 Correct 153 ms 22044 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 320 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 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 6 ms 1236 KB Output is correct
37 Correct 152 ms 22064 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1216 KB Output is correct
41 Correct 156 ms 22052 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 724 KB Output is correct
45 Correct 71 ms 12192 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 38 ms 5788 KB Output is correct
50 Correct 150 ms 22060 KB Output is correct
51 Correct 150 ms 22024 KB Output is correct
52 Correct 157 ms 22060 KB Output is correct
53 Correct 60 ms 12188 KB Output is correct
54 Correct 62 ms 12192 KB Output is correct
55 Correct 152 ms 22012 KB Output is correct
56 Correct 156 ms 22092 KB Output is correct
57 Correct 61 ms 12192 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 3 ms 724 KB Output is correct
61 Correct 68 ms 12068 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 328 KB Output is correct
65 Correct 39 ms 6208 KB Output is correct
66 Correct 62 ms 14108 KB Output is correct
67 Correct 68 ms 14096 KB Output is correct
68 Correct 61 ms 14024 KB Output is correct
69 Correct 61 ms 14108 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 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 6 ms 1108 KB Output is correct
7 Correct 152 ms 22048 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 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 6 ms 1236 KB Output is correct
13 Correct 157 ms 22048 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 76 ms 12076 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 5684 KB Output is correct
21 Correct 155 ms 22056 KB Output is correct
22 Correct 151 ms 22012 KB Output is correct
23 Correct 161 ms 22016 KB Output is correct
24 Correct 153 ms 21984 KB Output is correct
25 Correct 62 ms 12072 KB Output is correct
26 Correct 61 ms 12064 KB Output is correct
27 Correct 157 ms 22300 KB Output is correct
28 Correct 153 ms 22044 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 320 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 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 6 ms 1236 KB Output is correct
37 Correct 152 ms 22064 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1216 KB Output is correct
41 Correct 156 ms 22052 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 724 KB Output is correct
45 Correct 71 ms 12192 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 38 ms 5788 KB Output is correct
50 Correct 150 ms 22060 KB Output is correct
51 Correct 150 ms 22024 KB Output is correct
52 Correct 157 ms 22060 KB Output is correct
53 Correct 60 ms 12188 KB Output is correct
54 Correct 62 ms 12192 KB Output is correct
55 Correct 152 ms 22012 KB Output is correct
56 Correct 156 ms 22092 KB Output is correct
57 Correct 61 ms 12192 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 38 ms 5708 KB Output is correct
62 Correct 160 ms 22044 KB Output is correct
63 Correct 153 ms 22044 KB Output is correct
64 Correct 156 ms 22032 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 38 ms 5800 KB Output is correct
67 Correct 152 ms 22052 KB Output is correct
68 Correct 152 ms 22092 KB Output is correct
69 Correct 166 ms 21996 KB Output is correct
70 Correct 0 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 38 ms 6284 KB Output is correct
74 Correct 153 ms 24060 KB Output is correct
75 Correct 154 ms 24044 KB Output is correct
76 Correct 152 ms 23952 KB Output is correct
77 Correct 158 ms 24132 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 3 ms 724 KB Output is correct
81 Correct 68 ms 12068 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct
84 Correct 1 ms 328 KB Output is correct
85 Correct 39 ms 6208 KB Output is correct
86 Correct 62 ms 14108 KB Output is correct
87 Correct 68 ms 14096 KB Output is correct
88 Correct 61 ms 14024 KB Output is correct
89 Correct 61 ms 14108 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 3 ms 852 KB Output is correct
93 Correct 62 ms 14100 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 0 ms 212 KB Output is correct
97 Correct 17 ms 3736 KB Output is correct
98 Correct 60 ms 14108 KB Output is correct
99 Correct 61 ms 14048 KB Output is correct
100 Correct 62 ms 14108 KB Output is correct
101 Correct 60 ms 14104 KB Output is correct
102 Correct 61 ms 14108 KB Output is correct
103 Correct 66 ms 14052 KB Output is correct
104 Correct 62 ms 14096 KB Output is correct
105 Correct 66 ms 14112 KB Output is correct