Submission #301482

# Submission time Handle Problem Language Result Execution time Memory
301482 2020-09-18T02:05:32 Z 12tqian Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
256 ms 22360 KB
#ifdef LOCAL
#else
#include "supertrees.h"
#endif
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#ifdef LOCAL
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__);
#else
#define dbg(...) 17;
#endif

template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; }
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) { bool f = true; os << "{"; for (const auto &x : c) { if (!f) os << ", "; f = false; os << x; } return os << "}"; }
template<typename T> void debug(string s, T x) { cerr << s << " = " << x << "\n"; }
template<typename T, typename... Args> void debug(string s, T x, Args... args) { cerr << s.substr(0, s.find(',')) << " = " << x << " | "; debug(s.substr(s.find(',') + 2), args...); }
#ifdef LOCAL
bool build(vector<vector<int>> p) {
    return true;
}
#else
#endif
int construct(vector<vector<int>> p) {
    int n = p.size();
    vector<vector<int>> res(n, vector<int>(n, 0));
    vector<bool> vis(n);
    auto ae = [&](int u, int v) {
        res[u][v] = 1;
        res[v][u] = 1;
    };
    auto make_cycle = [&](vector<int>& v) {
        for (int i = 0; i < v.size(); i++) {
            ae(v[i], v[(i + 1) % v.size()]);
        }
    };
    auto make_triple = [&](vector<int>& v) {
        make_cycle(v);
        ae(v[0], v[2]);
    };
    auto make_chain = [&](vector<int>& v) {
        for (int i = 0; i < v.size() - 1; i++) {
            ae(v[i], v[i + 1]);
        }
    };
    for (int i = 0; i < n; i++) {
        if (vis[i]) continue;
        vector<int> comp;
        function<void(int)> dfs_comp = [&](int src) {
            vis[src] = true;
            comp.push_back(src);
            for (int nxt = 0; nxt < n; nxt++) {
                if (p[src][nxt] == 0 || vis[nxt]) continue;
                dfs_comp(nxt);
            }
        };
        dfs_comp(i);
        int sz = comp.size();
        vector<int> overall(4);
        vector<int> one;
        vector<int> other;
        for (int x: comp) {
            vector<int> cnt(4);
            for (int y: comp) {
                if (x == y) continue;
                cnt[p[x][y]]++;
                overall[p[x][y]]++;
            }
            if (cnt[0]) {
                return 0;
            }
            if (cnt[1] == 0) {
                other.push_back(x);
            } else {
                one.push_back(x);
            }
        }
        if (overall[0] || overall[2] && overall[3]) {
            return 0;
        }
        if (overall[2] == 0 && overall[3] == 0) {
            vector<int> big;
            for (int x: one) big.push_back(x);
            for (int x: other) big.push_back(x);
            make_chain(big);
            continue;
        }
        vector<vector<int>> chains;
        for (int x: one) {
            bool done = false;
            for(auto& y: chains) {
                bool ok = false;
                for (int z: y) {
                    if (p[x][z] != 1) {
                        ok = true;
                        break;
                    }
                }
                if (ok == false) {
                    y.push_back(x);
                    done = true;
                    break;
                }
            }
            if (done == false) {
                chains.push_back(vector<int>(0));
                chains.back().push_back(x);
            }
        }
        for (auto& c: chains) {
            for (int x: c) {
                for (int y: c) {
                    if (p[x][y] != 1) {
                        return 0;
                    }
                }
            }
        }
        for (int j = 0; j < chains.size(); j++) {
            for (int k = j + 1; k < chains.size(); k++) {
                for (int x: chains[j]) {
                    for (int y: chains[k]) {
                        if (p[x][y] == 1) {
                            return 0;
                        }
                    }
                }
            }
        }
        int cycle = chains.size() + other.size();
        if (overall[2]) {
            if (cycle < 3) {
                return 0;
            }
            for (auto x: chains) {
                other.push_back(x[0]);
                make_chain(x);
            }
            make_cycle(other);
        } else if (overall[3]) {
            return 0;
        } else {
            vector<int> big;
            for (int x: one) big.push_back(x);
            for (int x: other) big.push_back(x);
            make_chain(big);
        }
    }
    build(res);
    return 1;
}
#ifdef LOCAL
int main() {
    freopen("file.in", "r", stdin);
    string line; cin >> line;
    int n; cin >> n;
    vector<vector<int>> p(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> p[i][j];
        }
    }
    cout << construct(p) << '\n';
    return 0;
}
#else
#endif

Compilation message

supertrees.cpp: In lambda function:
supertrees.cpp:35:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |         for (int i = 0; i < v.size(); i++) {
      |                         ~~^~~~~~~~~~
supertrees.cpp: In lambda function:
supertrees.cpp:44:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |         for (int i = 0; i < v.size() - 1; i++) {
      |                         ~~^~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:80:38: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   80 |         if (overall[0] || overall[2] && overall[3]) {
supertrees.cpp:121:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |         for (int j = 0; j < chains.size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~
supertrees.cpp:122:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |             for (int k = j + 1; k < chains.size(); k++) {
      |                                 ~~^~~~~~~~~~~~~~~
supertrees.cpp:60:13: warning: unused variable 'sz' [-Wunused-variable]
   60 |         int sz = comp.size();
      |             ^~
supertrees.cpp:39:10: warning: variable 'make_triple' set but not used [-Wunused-but-set-variable]
   39 |     auto make_triple = [&](vector<int>& v) {
      |          ^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 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 0 ms 256 KB Output is correct
6 Correct 10 ms 1152 KB Output is correct
7 Correct 250 ms 22108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 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 0 ms 256 KB Output is correct
6 Correct 10 ms 1152 KB Output is correct
7 Correct 250 ms 22108 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 1 ms 256 KB Output is correct
12 Correct 11 ms 1280 KB Output is correct
13 Correct 240 ms 22136 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 113 ms 12280 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 60 ms 5752 KB Output is correct
21 Correct 248 ms 22136 KB Output is correct
22 Correct 238 ms 22136 KB Output is correct
23 Correct 256 ms 22136 KB Output is correct
24 Correct 238 ms 22136 KB Output is correct
25 Correct 107 ms 12152 KB Output is correct
26 Correct 105 ms 12152 KB Output is correct
27 Correct 252 ms 22136 KB Output is correct
28 Correct 238 ms 22136 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 1 ms 384 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 12 ms 1152 KB Output is correct
9 Correct 235 ms 22136 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 10 ms 1152 KB Output is correct
13 Correct 247 ms 22136 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 113 ms 12284 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 0 ms 256 KB Output is correct
21 Correct 62 ms 5752 KB Output is correct
22 Correct 243 ms 22144 KB Output is correct
23 Correct 238 ms 22136 KB Output is correct
24 Correct 247 ms 22136 KB Output is correct
25 Correct 102 ms 12152 KB Output is correct
26 Correct 101 ms 12152 KB Output is correct
27 Correct 236 ms 22136 KB Output is correct
28 Correct 247 ms 22136 KB Output is correct
29 Correct 98 ms 12156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 288 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 61 ms 5780 KB Output is correct
5 Correct 239 ms 22136 KB Output is correct
6 Correct 234 ms 22140 KB Output is correct
7 Correct 251 ms 22360 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 61 ms 5752 KB Output is correct
10 Correct 243 ms 22136 KB Output is correct
11 Correct 237 ms 22136 KB Output is correct
12 Correct 252 ms 22136 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 59 ms 5752 KB Output is correct
17 Correct 244 ms 22136 KB Output is correct
18 Correct 237 ms 22136 KB Output is correct
19 Correct 245 ms 22136 KB Output is correct
20 Correct 235 ms 22264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 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 0 ms 256 KB Output is correct
6 Correct 10 ms 1152 KB Output is correct
7 Correct 250 ms 22108 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 1 ms 256 KB Output is correct
12 Correct 11 ms 1280 KB Output is correct
13 Correct 240 ms 22136 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 113 ms 12280 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 60 ms 5752 KB Output is correct
21 Correct 248 ms 22136 KB Output is correct
22 Correct 238 ms 22136 KB Output is correct
23 Correct 256 ms 22136 KB Output is correct
24 Correct 238 ms 22136 KB Output is correct
25 Correct 107 ms 12152 KB Output is correct
26 Correct 105 ms 12152 KB Output is correct
27 Correct 252 ms 22136 KB Output is correct
28 Correct 238 ms 22136 KB Output is correct
29 Correct 1 ms 256 KB Output is correct
30 Correct 0 ms 256 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 288 KB Output is correct
33 Correct 1 ms 256 KB Output is correct
34 Correct 1 ms 256 KB Output is correct
35 Correct 1 ms 256 KB Output is correct
36 Correct 12 ms 1152 KB Output is correct
37 Correct 235 ms 22136 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 0 ms 256 KB Output is correct
40 Correct 10 ms 1152 KB Output is correct
41 Correct 247 ms 22136 KB Output is correct
42 Correct 0 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 5 ms 768 KB Output is correct
45 Correct 113 ms 12284 KB Output is correct
46 Correct 0 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 0 ms 256 KB Output is correct
49 Correct 62 ms 5752 KB Output is correct
50 Correct 243 ms 22144 KB Output is correct
51 Correct 238 ms 22136 KB Output is correct
52 Correct 247 ms 22136 KB Output is correct
53 Correct 102 ms 12152 KB Output is correct
54 Correct 101 ms 12152 KB Output is correct
55 Correct 236 ms 22136 KB Output is correct
56 Correct 247 ms 22136 KB Output is correct
57 Correct 98 ms 12156 KB Output is correct
58 Correct 0 ms 256 KB Output is correct
59 Correct 1 ms 256 KB Output is correct
60 Correct 5 ms 768 KB Output is correct
61 Correct 114 ms 12156 KB Output is correct
62 Correct 1 ms 288 KB Output is correct
63 Correct 1 ms 256 KB Output is correct
64 Correct 0 ms 256 KB Output is correct
65 Correct 59 ms 5880 KB Output is correct
66 Correct 104 ms 12152 KB Output is correct
67 Correct 107 ms 12156 KB Output is correct
68 Correct 107 ms 12152 KB Output is correct
69 Correct 100 ms 12152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 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 0 ms 256 KB Output is correct
6 Correct 10 ms 1152 KB Output is correct
7 Correct 250 ms 22108 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 1 ms 256 KB Output is correct
12 Correct 11 ms 1280 KB Output is correct
13 Correct 240 ms 22136 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 113 ms 12280 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 60 ms 5752 KB Output is correct
21 Correct 248 ms 22136 KB Output is correct
22 Correct 238 ms 22136 KB Output is correct
23 Correct 256 ms 22136 KB Output is correct
24 Correct 238 ms 22136 KB Output is correct
25 Correct 107 ms 12152 KB Output is correct
26 Correct 105 ms 12152 KB Output is correct
27 Correct 252 ms 22136 KB Output is correct
28 Correct 238 ms 22136 KB Output is correct
29 Correct 1 ms 256 KB Output is correct
30 Correct 0 ms 256 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 288 KB Output is correct
33 Correct 1 ms 256 KB Output is correct
34 Correct 1 ms 256 KB Output is correct
35 Correct 1 ms 256 KB Output is correct
36 Correct 12 ms 1152 KB Output is correct
37 Correct 235 ms 22136 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 0 ms 256 KB Output is correct
40 Correct 10 ms 1152 KB Output is correct
41 Correct 247 ms 22136 KB Output is correct
42 Correct 0 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 5 ms 768 KB Output is correct
45 Correct 113 ms 12284 KB Output is correct
46 Correct 0 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 0 ms 256 KB Output is correct
49 Correct 62 ms 5752 KB Output is correct
50 Correct 243 ms 22144 KB Output is correct
51 Correct 238 ms 22136 KB Output is correct
52 Correct 247 ms 22136 KB Output is correct
53 Correct 102 ms 12152 KB Output is correct
54 Correct 101 ms 12152 KB Output is correct
55 Correct 236 ms 22136 KB Output is correct
56 Correct 247 ms 22136 KB Output is correct
57 Correct 98 ms 12156 KB Output is correct
58 Correct 1 ms 288 KB Output is correct
59 Correct 0 ms 256 KB Output is correct
60 Correct 0 ms 256 KB Output is correct
61 Correct 61 ms 5780 KB Output is correct
62 Correct 239 ms 22136 KB Output is correct
63 Correct 234 ms 22140 KB Output is correct
64 Correct 251 ms 22360 KB Output is correct
65 Correct 0 ms 256 KB Output is correct
66 Correct 61 ms 5752 KB Output is correct
67 Correct 243 ms 22136 KB Output is correct
68 Correct 237 ms 22136 KB Output is correct
69 Correct 252 ms 22136 KB Output is correct
70 Correct 0 ms 256 KB Output is correct
71 Correct 1 ms 256 KB Output is correct
72 Correct 0 ms 256 KB Output is correct
73 Correct 59 ms 5752 KB Output is correct
74 Correct 244 ms 22136 KB Output is correct
75 Correct 237 ms 22136 KB Output is correct
76 Correct 245 ms 22136 KB Output is correct
77 Correct 235 ms 22264 KB Output is correct
78 Correct 0 ms 256 KB Output is correct
79 Correct 1 ms 256 KB Output is correct
80 Correct 5 ms 768 KB Output is correct
81 Correct 114 ms 12156 KB Output is correct
82 Correct 1 ms 288 KB Output is correct
83 Correct 1 ms 256 KB Output is correct
84 Correct 0 ms 256 KB Output is correct
85 Correct 59 ms 5880 KB Output is correct
86 Correct 104 ms 12152 KB Output is correct
87 Correct 107 ms 12156 KB Output is correct
88 Correct 107 ms 12152 KB Output is correct
89 Correct 100 ms 12152 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 768 KB Output is correct
93 Correct 107 ms 12152 KB Output is correct
94 Correct 1 ms 256 KB Output is correct
95 Correct 0 ms 256 KB Output is correct
96 Correct 0 ms 256 KB Output is correct
97 Correct 25 ms 3328 KB Output is correct
98 Correct 101 ms 12152 KB Output is correct
99 Correct 104 ms 12152 KB Output is correct
100 Correct 106 ms 12156 KB Output is correct
101 Correct 99 ms 12152 KB Output is correct
102 Correct 114 ms 12280 KB Output is correct
103 Correct 113 ms 12376 KB Output is correct
104 Correct 114 ms 12152 KB Output is correct
105 Correct 114 ms 12152 KB Output is correct