Submission #301424

# Submission time Handle Problem Language Result Execution time Memory
301424 2020-09-17T23:19:07 Z cuom1999 Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
275 ms 30204 KB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;


struct Solver {
    int n;
    vector<bool> vs;
    vector<int> lineIndex, cycleIndex;
    vector<vector<int>> p, line, cycle;

    Solver(vector<vector<int>> p): p(p), n(p.size()) {
        vs.resize(n);
        lineIndex.resize(n);
        line.resize(n);
        cycle.resize(n);
        cycleIndex.resize(n);
    }

    void dfs1(int a, int c) {
        if (vs[a]) return;
        vs[a] = 1;
        lineIndex[a] = c;
        line[c].push_back(a);

        for (int i = 0; i < n; i++) {
            if (p[a][i] == 1) dfs1(i, c);
        }
    }

    void dfs2(int a, int c) {
        if (vs[a]) return;
        vs[a] = 1;
        cycle[c].push_back(a);
        cycleIndex[a] = c;
        for (int i = 0; i < n; i++) {
            if (p[a][i] == 2) dfs2(i, c);
        }
    }

    int solve() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (p[i][j] == 3) return 0;
            }
        }

        for (int i = 0; i < n; i++) {
            if (!vs[i]) dfs1(i, i);
        }

        for (int i = 0; i < n; i++) {
            if (lineIndex[i] == i) vs[i] = 0;
        }

        for (int i = 0; i < n; i++) {
            if (!vs[i]) dfs2(i, i);
        }

        // construct res
        vector<vector<int>> res(n, vector<int>(n));

        for (int i = 0; i < n; i++) {
            for (int j = 0; j + 1 < line[i].size(); j++) {
                int x = line[i][j], y = line[i][j + 1];
                res[x][y] = res[y][x] = 1;
            }

            if (cycle[i].size() == 2) return 0;
            if (cycle[i].size() > 1) cycle[i].push_back(cycle[i][0]);
            for (int j = 0; j + 1 < cycle[i].size(); j++) {
                int x = cycle[i][j], y = cycle[i][j + 1];
                res[x][y] = res[y][x] = 1;
            }
        }

        // check satisfaction
        // p[i][j] = 0, p[j][k] = 0, p[i][k] > 0
        // p[i][j] = 1 iff lineIndex[i] = lineIndex[j]
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int a = cycleIndex[lineIndex[i]];
                int b = cycleIndex[lineIndex[j]];
                if (p[i][j] == 0 && a == b) {
                    return 0;
                }
                if (p[i][j] && a != b) {
                    return 0;
                }
                if (p[i][j] == 1 && lineIndex[i] != lineIndex[j]) {
                    return 0;
                }
                if (p[i][j] == 2 && lineIndex[i] == lineIndex[j]) {
                    return 0;
                } 
            }
        }

        build(res);
        return 1;
    }
};


int construct(vector<vector<int>> p) {
    return Solver(p).solve();
}

Compilation message

supertrees.cpp: In constructor 'Solver::Solver(std::vector<std::vector<int> >)':
supertrees.cpp:10:25: warning: 'Solver::p' will be initialized after [-Wreorder]
   10 |     vector<vector<int>> p, line, cycle;
      |                         ^
supertrees.cpp:7:9: warning:   'int Solver::n' [-Wreorder]
    7 |     int n;
      |         ^
supertrees.cpp:12:5: warning:   when initialized here [-Wreorder]
   12 |     Solver(vector<vector<int>> p): p(p), n(p.size()) {
      |     ^~~~~~
supertrees.cpp: In member function 'int Solver::solve()':
supertrees.cpp:64:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |             for (int j = 0; j + 1 < line[i].size(); j++) {
      |                             ~~~~~~^~~~~~~~~~~~~~~~
supertrees.cpp:71:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |             for (int j = 0; j + 1 < cycle[i].size(); j++) {
      |                             ~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 11 ms 1536 KB Output is correct
7 Correct 252 ms 30072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 11 ms 1536 KB Output is correct
7 Correct 252 ms 30072 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 11 ms 1536 KB Output is correct
13 Correct 243 ms 30200 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 6 ms 1152 KB Output is correct
17 Correct 122 ms 20220 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 63 ms 7800 KB Output is correct
21 Correct 248 ms 30072 KB Output is correct
22 Correct 251 ms 30072 KB Output is correct
23 Correct 261 ms 30200 KB Output is correct
24 Correct 244 ms 30072 KB Output is correct
25 Correct 108 ms 20088 KB Output is correct
26 Correct 105 ms 20088 KB Output is correct
27 Correct 257 ms 30072 KB Output is correct
28 Correct 242 ms 30036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 10 ms 1536 KB Output is correct
9 Correct 242 ms 30200 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 11 ms 1536 KB Output is correct
13 Correct 261 ms 30072 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 7 ms 1152 KB Output is correct
17 Correct 125 ms 20216 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 62 ms 7800 KB Output is correct
22 Correct 246 ms 30072 KB Output is correct
23 Correct 246 ms 30200 KB Output is correct
24 Correct 259 ms 30072 KB Output is correct
25 Correct 108 ms 20216 KB Output is correct
26 Correct 109 ms 20216 KB Output is correct
27 Correct 245 ms 30072 KB Output is correct
28 Correct 257 ms 29980 KB Output is correct
29 Correct 105 ms 20216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 62 ms 7800 KB Output is correct
5 Correct 249 ms 30200 KB Output is correct
6 Correct 242 ms 30072 KB Output is correct
7 Correct 262 ms 30072 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 62 ms 7800 KB Output is correct
10 Correct 251 ms 30204 KB Output is correct
11 Correct 243 ms 30200 KB Output is correct
12 Correct 258 ms 30076 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 67 ms 7800 KB Output is correct
17 Correct 269 ms 30200 KB Output is correct
18 Correct 245 ms 30072 KB Output is correct
19 Correct 275 ms 30064 KB Output is correct
20 Correct 255 ms 30200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 11 ms 1536 KB Output is correct
7 Correct 252 ms 30072 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 11 ms 1536 KB Output is correct
13 Correct 243 ms 30200 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 6 ms 1152 KB Output is correct
17 Correct 122 ms 20220 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 63 ms 7800 KB Output is correct
21 Correct 248 ms 30072 KB Output is correct
22 Correct 251 ms 30072 KB Output is correct
23 Correct 261 ms 30200 KB Output is correct
24 Correct 244 ms 30072 KB Output is correct
25 Correct 108 ms 20088 KB Output is correct
26 Correct 105 ms 20088 KB Output is correct
27 Correct 257 ms 30072 KB Output is correct
28 Correct 242 ms 30036 KB Output is correct
29 Correct 0 ms 256 KB Output is correct
30 Correct 0 ms 256 KB Output is correct
31 Correct 0 ms 256 KB Output is correct
32 Correct 0 ms 256 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 256 KB Output is correct
35 Correct 0 ms 256 KB Output is correct
36 Correct 10 ms 1536 KB Output is correct
37 Correct 242 ms 30200 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 0 ms 256 KB Output is correct
40 Correct 11 ms 1536 KB Output is correct
41 Correct 261 ms 30072 KB Output is correct
42 Correct 1 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 7 ms 1152 KB Output is correct
45 Correct 125 ms 20216 KB Output is correct
46 Correct 1 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 62 ms 7800 KB Output is correct
50 Correct 246 ms 30072 KB Output is correct
51 Correct 246 ms 30200 KB Output is correct
52 Correct 259 ms 30072 KB Output is correct
53 Correct 108 ms 20216 KB Output is correct
54 Correct 109 ms 20216 KB Output is correct
55 Correct 245 ms 30072 KB Output is correct
56 Correct 257 ms 29980 KB Output is correct
57 Correct 105 ms 20216 KB Output is correct
58 Correct 0 ms 288 KB Output is correct
59 Correct 1 ms 256 KB Output is correct
60 Correct 7 ms 1280 KB Output is correct
61 Correct 131 ms 20176 KB Output is correct
62 Correct 1 ms 256 KB Output is correct
63 Correct 1 ms 256 KB Output is correct
64 Correct 1 ms 256 KB Output is correct
65 Correct 61 ms 7928 KB Output is correct
66 Correct 108 ms 20216 KB Output is correct
67 Correct 111 ms 20120 KB Output is correct
68 Correct 107 ms 20216 KB Output is correct
69 Correct 106 ms 20216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 11 ms 1536 KB Output is correct
7 Correct 252 ms 30072 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 11 ms 1536 KB Output is correct
13 Correct 243 ms 30200 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 6 ms 1152 KB Output is correct
17 Correct 122 ms 20220 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 63 ms 7800 KB Output is correct
21 Correct 248 ms 30072 KB Output is correct
22 Correct 251 ms 30072 KB Output is correct
23 Correct 261 ms 30200 KB Output is correct
24 Correct 244 ms 30072 KB Output is correct
25 Correct 108 ms 20088 KB Output is correct
26 Correct 105 ms 20088 KB Output is correct
27 Correct 257 ms 30072 KB Output is correct
28 Correct 242 ms 30036 KB Output is correct
29 Correct 0 ms 256 KB Output is correct
30 Correct 0 ms 256 KB Output is correct
31 Correct 0 ms 256 KB Output is correct
32 Correct 0 ms 256 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 256 KB Output is correct
35 Correct 0 ms 256 KB Output is correct
36 Correct 10 ms 1536 KB Output is correct
37 Correct 242 ms 30200 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 0 ms 256 KB Output is correct
40 Correct 11 ms 1536 KB Output is correct
41 Correct 261 ms 30072 KB Output is correct
42 Correct 1 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 7 ms 1152 KB Output is correct
45 Correct 125 ms 20216 KB Output is correct
46 Correct 1 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 62 ms 7800 KB Output is correct
50 Correct 246 ms 30072 KB Output is correct
51 Correct 246 ms 30200 KB Output is correct
52 Correct 259 ms 30072 KB Output is correct
53 Correct 108 ms 20216 KB Output is correct
54 Correct 109 ms 20216 KB Output is correct
55 Correct 245 ms 30072 KB Output is correct
56 Correct 257 ms 29980 KB Output is correct
57 Correct 105 ms 20216 KB Output is correct
58 Correct 1 ms 256 KB Output is correct
59 Correct 0 ms 256 KB Output is correct
60 Correct 0 ms 256 KB Output is correct
61 Correct 62 ms 7800 KB Output is correct
62 Correct 249 ms 30200 KB Output is correct
63 Correct 242 ms 30072 KB Output is correct
64 Correct 262 ms 30072 KB Output is correct
65 Correct 0 ms 256 KB Output is correct
66 Correct 62 ms 7800 KB Output is correct
67 Correct 251 ms 30204 KB Output is correct
68 Correct 243 ms 30200 KB Output is correct
69 Correct 258 ms 30076 KB Output is correct
70 Correct 1 ms 256 KB Output is correct
71 Correct 1 ms 256 KB Output is correct
72 Correct 1 ms 384 KB Output is correct
73 Correct 67 ms 7800 KB Output is correct
74 Correct 269 ms 30200 KB Output is correct
75 Correct 245 ms 30072 KB Output is correct
76 Correct 275 ms 30064 KB Output is correct
77 Correct 255 ms 30200 KB Output is correct
78 Correct 0 ms 288 KB Output is correct
79 Correct 1 ms 256 KB Output is correct
80 Correct 7 ms 1280 KB Output is correct
81 Correct 131 ms 20176 KB Output is correct
82 Correct 1 ms 256 KB Output is correct
83 Correct 1 ms 256 KB Output is correct
84 Correct 1 ms 256 KB Output is correct
85 Correct 61 ms 7928 KB Output is correct
86 Correct 108 ms 20216 KB Output is correct
87 Correct 111 ms 20120 KB Output is correct
88 Correct 107 ms 20216 KB Output is correct
89 Correct 106 ms 20216 KB Output is correct
90 Correct 0 ms 256 KB Output is correct
91 Correct 0 ms 256 KB Output is correct
92 Correct 5 ms 1024 KB Output is correct
93 Correct 111 ms 16248 KB Output is correct
94 Correct 1 ms 256 KB Output is correct
95 Correct 0 ms 256 KB Output is correct
96 Correct 1 ms 256 KB Output is correct
97 Correct 26 ms 4352 KB Output is correct
98 Correct 102 ms 16160 KB Output is correct
99 Correct 102 ms 16120 KB Output is correct
100 Correct 101 ms 16120 KB Output is correct
101 Correct 99 ms 16124 KB Output is correct
102 Correct 109 ms 16124 KB Output is correct
103 Correct 110 ms 16212 KB Output is correct
104 Correct 109 ms 16120 KB Output is correct
105 Correct 108 ms 16120 KB Output is correct