Submission #302996

# Submission time Handle Problem Language Result Execution time Memory
302996 2020-09-19T15:43:54 Z ivan24 Connecting Supertrees (IOI20_supertrees) C++14
96 / 100
261 ms 26236 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

using ll = int;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
#define F first
#define S second

void build(vvi b);

class Solver{
private:
    vvi p;
    ll n;
    vvi ans;
    vi hasSet;
    bool add_edge(ll u, ll v){
        if (ans[u][v] || u == v) return 0;
        ans[u][v] = ans[v][u] = 1;
        return 1;
    }
    vi concat(const vi& v1, const vi& v2, const vi& v3){
        vi res;
        for (ll i = v1.size()-1; i >= 0; i--) res.push_back(v1[i]);
        for (auto i: v2) res.push_back(i);
        for (auto i: v3) res.push_back(i);
        return res;
    }

    bool checkOnes(const vi& v){
        for (auto i: v){
            for (auto j: v){
                if (j != i && p[i][j] != 1) return 0;
            }
        }
        return 1;
    }
    bool checkTwos(const vi& two, const vi& one){
        for (auto i: two){
            for (auto j: one){
                if (p[i][j] != 2) return 0;
            }
            for (auto j: two){
                if (p[i][j] != 2 && i != j) return 0;
            }
        }
        return 1;
    }


    bool processGroup(const vi& cur_set){
        // check if all are connected to each other
        for (auto j: cur_set){
            for (auto k: cur_set){
                if (p[j][k] == 0 && j != k) return 0;
            }
        }
        vi two_set; vvi one_set;
        vi hasCurSet(n,0);
        for (auto j: cur_set){
            if (hasCurSet[j]) continue;
            hasCurSet[j] = 2;

            for (auto k: cur_set){
                if (k != j && p[j][k] == 1) hasCurSet[j] = 1;
            }
            if (hasCurSet[j] == 1){
                ll cur_one = one_set.size();
                one_set.push_back(vi());
                one_set[cur_one].push_back(j);
                for (auto k: cur_set){
                    if (p[j][k] == 1 && j != k){
                        one_set[cur_one].push_back(k);
                        if (hasCurSet[k]) return 0;
                        hasCurSet[k] = 1;
                    }
                }
            }else{
                two_set.push_back(j);
                hasCurSet[j] = 2;
            }
        }

        /*
        cout << "one_set:\n";
        for (auto j: one_set){
            for (auto k: j) cout << k << " ";
            cout << endl;
        }
        cout << "two_set:\n";
        for (auto j: two_set) cout << j << " ";
        cout << endl;
        */
        if (one_set.size() == 1 && two_set.size() == 0){
            for (ll i = 0; one_set[0].size() > i+1; i++){
                add_edge(one_set[0][i],one_set[0][i+1]);
            }
        }else{
            for (auto i: one_set){
                for (auto j: i){
                    for (auto k: i){
                        if (k == j) continue;
                        if (p[j][k] != 1) return 0;
                    }
                }
                for (ll j = 0; i.size() > j+1; j++){
                    add_edge(i[j],i[j+1]);
                }
            }
            vi te;
            for (ll i = 0; one_set.size() > i; i++) te.push_back(one_set[i][0]);
            for (auto i: two_set) te.push_back(i);
            for (ll i = 0; te.size() > i+1 ;i++){
                add_edge(te[i],te[i+1]);
            }

            ii extra_edge = ii(te[0],te[te.size()-1]);
            bool b = add_edge(extra_edge.F, extra_edge.S);
            //cout << extra_edge.F << " " << extra_edge.S << endl;
            if (!b) return 0;
        }

        for (auto i: cur_set){
            for (auto j: cur_set){
                if (j != i) p[i][j] = 0;
            }
        }

        return 1;
    }
public:
    Solver(vvi p): p(p),n(p.size()),ans(n,vi(n,0)), hasSet(n,0){

    }
    bool solve(){
        for (int i = 0; n > i; i++){
            if (hasSet[i]){
                for (ll j = 0; n > j; j++){
                    if (p[i][j] && j != i) return 0;
                }
                continue;
            }
            vi cur_set;
            for (ll j = 0; n > j; j++){
                if (p[i][j] != 0){
                    cur_set.push_back(j);
                    if (hasSet[j]) return 0;
                    else hasSet[j] = 1;
                }
            }
            if (cur_set.size() > 1){
                bool res = processGroup(cur_set);
                if (!res) return 0;
            }
        }
        for (ll i = 0; n > i; i++){
            for (ll j = 0; n > j; j++){
                //if (p[i][j] && i != j) return 0;
            }
        }
        build(ans);
        return 1;
    }
};

int construct(vvi p) {
    Solver mySolver(p);
    return mySolver.solve();
}

Compilation message

supertrees.cpp: In member function 'bool Solver::processGroup(const vi&)':
supertrees.cpp:100:46: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'int'} [-Wsign-compare]
  100 |             for (ll i = 0; one_set[0].size() > i+1; i++){
      |                            ~~~~~~~~~~~~~~~~~~^~~~~
supertrees.cpp:111:41: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'int'} [-Wsign-compare]
  111 |                 for (ll j = 0; i.size() > j+1; j++){
      |                                ~~~~~~~~~^~~~~
supertrees.cpp:116:43: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'll' {aka 'int'} [-Wsign-compare]
  116 |             for (ll i = 0; one_set.size() > i; i++) te.push_back(one_set[i][0]);
      |                            ~~~~~~~~~~~~~~~^~~
supertrees.cpp:118:38: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'int'} [-Wsign-compare]
  118 |             for (ll i = 0; te.size() > i+1 ;i++){
      |                            ~~~~~~~~~~^~~~~
# 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 1 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 11 ms 1408 KB Output is correct
7 Correct 246 ms 25976 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 1 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 11 ms 1408 KB Output is correct
7 Correct 246 ms 25976 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 10 ms 1408 KB Output is correct
13 Correct 245 ms 26072 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 5 ms 1152 KB Output is correct
17 Correct 115 ms 20088 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 6780 KB Output is correct
21 Correct 243 ms 25976 KB Output is correct
22 Correct 242 ms 26104 KB Output is correct
23 Correct 261 ms 26104 KB Output is correct
24 Correct 246 ms 26024 KB Output is correct
25 Correct 110 ms 20088 KB Output is correct
26 Correct 111 ms 20052 KB Output is correct
27 Correct 251 ms 26112 KB Output is correct
28 Correct 243 ms 26104 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 1 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 13 ms 1408 KB Output is correct
9 Correct 249 ms 26108 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 1408 KB Output is correct
13 Correct 248 ms 26104 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 1152 KB Output is correct
17 Correct 113 ms 20088 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 1 ms 256 KB Output is correct
21 Correct 60 ms 6776 KB Output is correct
22 Correct 242 ms 26104 KB Output is correct
23 Correct 237 ms 26072 KB Output is correct
24 Correct 254 ms 26072 KB Output is correct
25 Correct 109 ms 20088 KB Output is correct
26 Correct 110 ms 20088 KB Output is correct
27 Correct 246 ms 26236 KB Output is correct
28 Correct 248 ms 25976 KB Output is correct
29 Correct 106 ms 20088 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 1 ms 256 KB Output is correct
4 Correct 62 ms 6856 KB Output is correct
5 Correct 246 ms 25976 KB Output is correct
6 Correct 246 ms 26104 KB Output is correct
7 Correct 247 ms 25976 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 63 ms 6776 KB Output is correct
10 Correct 241 ms 25976 KB Output is correct
11 Correct 239 ms 26104 KB Output is correct
12 Correct 249 ms 26104 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 62 ms 6780 KB Output is correct
17 Correct 240 ms 25976 KB Output is correct
18 Correct 238 ms 25976 KB Output is correct
19 Correct 239 ms 25976 KB Output is correct
20 Correct 244 ms 25976 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 1 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 11 ms 1408 KB Output is correct
7 Correct 246 ms 25976 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 10 ms 1408 KB Output is correct
13 Correct 245 ms 26072 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 5 ms 1152 KB Output is correct
17 Correct 115 ms 20088 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 6780 KB Output is correct
21 Correct 243 ms 25976 KB Output is correct
22 Correct 242 ms 26104 KB Output is correct
23 Correct 261 ms 26104 KB Output is correct
24 Correct 246 ms 26024 KB Output is correct
25 Correct 110 ms 20088 KB Output is correct
26 Correct 111 ms 20052 KB Output is correct
27 Correct 251 ms 26112 KB Output is correct
28 Correct 243 ms 26104 KB Output is correct
29 Correct 0 ms 256 KB Output is correct
30 Correct 1 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 0 ms 256 KB Output is correct
34 Correct 1 ms 256 KB Output is correct
35 Correct 0 ms 256 KB Output is correct
36 Correct 13 ms 1408 KB Output is correct
37 Correct 249 ms 26108 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 1 ms 256 KB Output is correct
40 Correct 11 ms 1408 KB Output is correct
41 Correct 248 ms 26104 KB Output is correct
42 Correct 1 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 5 ms 1152 KB Output is correct
45 Correct 113 ms 20088 KB Output is correct
46 Correct 0 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 1 ms 256 KB Output is correct
49 Correct 60 ms 6776 KB Output is correct
50 Correct 242 ms 26104 KB Output is correct
51 Correct 237 ms 26072 KB Output is correct
52 Correct 254 ms 26072 KB Output is correct
53 Correct 109 ms 20088 KB Output is correct
54 Correct 110 ms 20088 KB Output is correct
55 Correct 246 ms 26236 KB Output is correct
56 Correct 248 ms 25976 KB Output is correct
57 Correct 106 ms 20088 KB Output is correct
58 Correct 0 ms 256 KB Output is correct
59 Correct 0 ms 256 KB Output is correct
60 Correct 5 ms 1152 KB Output is correct
61 Correct 114 ms 20056 KB Output is correct
62 Correct 1 ms 256 KB Output is correct
63 Correct 0 ms 256 KB Output is correct
64 Correct 0 ms 256 KB Output is correct
65 Correct 62 ms 6864 KB Output is correct
66 Correct 114 ms 20088 KB Output is correct
67 Correct 109 ms 20052 KB Output is correct
68 Correct 107 ms 20076 KB Output is correct
69 Correct 107 ms 20088 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 1 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 11 ms 1408 KB Output is correct
7 Correct 246 ms 25976 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 10 ms 1408 KB Output is correct
13 Correct 245 ms 26072 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 5 ms 1152 KB Output is correct
17 Correct 115 ms 20088 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 6780 KB Output is correct
21 Correct 243 ms 25976 KB Output is correct
22 Correct 242 ms 26104 KB Output is correct
23 Correct 261 ms 26104 KB Output is correct
24 Correct 246 ms 26024 KB Output is correct
25 Correct 110 ms 20088 KB Output is correct
26 Correct 111 ms 20052 KB Output is correct
27 Correct 251 ms 26112 KB Output is correct
28 Correct 243 ms 26104 KB Output is correct
29 Correct 0 ms 256 KB Output is correct
30 Correct 1 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 0 ms 256 KB Output is correct
34 Correct 1 ms 256 KB Output is correct
35 Correct 0 ms 256 KB Output is correct
36 Correct 13 ms 1408 KB Output is correct
37 Correct 249 ms 26108 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 1 ms 256 KB Output is correct
40 Correct 11 ms 1408 KB Output is correct
41 Correct 248 ms 26104 KB Output is correct
42 Correct 1 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 5 ms 1152 KB Output is correct
45 Correct 113 ms 20088 KB Output is correct
46 Correct 0 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 1 ms 256 KB Output is correct
49 Correct 60 ms 6776 KB Output is correct
50 Correct 242 ms 26104 KB Output is correct
51 Correct 237 ms 26072 KB Output is correct
52 Correct 254 ms 26072 KB Output is correct
53 Correct 109 ms 20088 KB Output is correct
54 Correct 110 ms 20088 KB Output is correct
55 Correct 246 ms 26236 KB Output is correct
56 Correct 248 ms 25976 KB Output is correct
57 Correct 106 ms 20088 KB Output is correct
58 Correct 0 ms 256 KB Output is correct
59 Correct 0 ms 256 KB Output is correct
60 Correct 1 ms 256 KB Output is correct
61 Correct 62 ms 6856 KB Output is correct
62 Correct 246 ms 25976 KB Output is correct
63 Correct 246 ms 26104 KB Output is correct
64 Correct 247 ms 25976 KB Output is correct
65 Correct 0 ms 256 KB Output is correct
66 Correct 63 ms 6776 KB Output is correct
67 Correct 241 ms 25976 KB Output is correct
68 Correct 239 ms 26104 KB Output is correct
69 Correct 249 ms 26104 KB Output is correct
70 Correct 1 ms 256 KB Output is correct
71 Correct 0 ms 256 KB Output is correct
72 Correct 0 ms 256 KB Output is correct
73 Correct 62 ms 6780 KB Output is correct
74 Correct 240 ms 25976 KB Output is correct
75 Correct 238 ms 25976 KB Output is correct
76 Correct 239 ms 25976 KB Output is correct
77 Correct 244 ms 25976 KB Output is correct
78 Correct 0 ms 256 KB Output is correct
79 Correct 0 ms 256 KB Output is correct
80 Correct 5 ms 1152 KB Output is correct
81 Correct 114 ms 20056 KB Output is correct
82 Correct 1 ms 256 KB Output is correct
83 Correct 0 ms 256 KB Output is correct
84 Correct 0 ms 256 KB Output is correct
85 Correct 62 ms 6864 KB Output is correct
86 Correct 114 ms 20088 KB Output is correct
87 Correct 109 ms 20052 KB Output is correct
88 Correct 107 ms 20076 KB Output is correct
89 Correct 107 ms 20088 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 1152 KB Output is correct
93 Correct 109 ms 20084 KB Output is correct
94 Incorrect 0 ms 256 KB Answer gives possible 1 while actual possible 0
95 Halted 0 ms 0 KB -