Submission #934139

# Submission time Handle Problem Language Result Execution time Memory
934139 2024-02-26T21:05:22 Z raul2008487 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
186 ms 23636 KB
#include<bits/stdc++.h>
#include "supertrees.h"
#define ll int
#define in insert
#define pb push_back
#define vl vector<ll>
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
struct DSU{
    vl par;
    vector<vector<ll>> e;
    void init(ll n){
        e.resize(n + 1);
        par.resize(n + 1);
        ll i;
        for(i = 0; i < n; i++){
            e[i].pb(i);
            par[i] = i;
        }
    }
    ll get(ll x){
        if(par[x] == x){
            return x;
        }
        return par[x] = get(par[x]);
    }
    bool connected(ll x, ll y){
        return (get(x) == get(y));
    }
    bool unite(ll a, ll b){
        a = get(a);
        b = get(b);
        if(a == b){return false;}
        if(e[a].size() < e[b].size()){
            swap(a, b);
        }
        par[b] = a;
        for(auto x: e[b]){
            e[a].pb(x);
            par[x] = a;
        }
        e[b].clear();
        return true;
    }
};
int construct(vector<vector<int>> p) {
	DSU dsu1, dsu2;
    ll n = p.size(), i, j;
    dsu1.init(n);
    dsu2.init(n);
    vector<vector<ll>> ans(n, vector<ll> (n, 0));
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(p[i][j] == 1){
                dsu1.unite(i, j);
            }
            if(p[i][j] == 3){
                //cout << "nisiryoz signal" << endl;
                return 0;
            }
        }
    }
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(p[i][j] != 1 && dsu1.connected(i, j)){
                //cout << "1" << endl;
                return 0;
            }
        }
    }
//    for(i = 0; i < n; i++){cout << dsu1.get(i) << ' ';}cout << endl;
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(p[i][j] == 2){
                if(dsu2.unite(dsu1.get(i), dsu1.get(j))){
//                    cout << dsu1.get(i) <<  ' ' << dsu1.get(j) << endl;
                }
            }
        }
    }
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(i == j){continue;}
            if(p[i][j] == 0 && (dsu1.connected(i, j) || dsu2.connected(i, j))){
                //cout << "2" << endl;
                return 0;
            }
            if(dsu1.connected(i, j) && dsu2.connected(i, j)){
                //cout << "signal 3" << endl;
                return 0;
            }
        }
    }
    vector<bool> used(n, 0), vis(n, 0);
    for(i = 0; i < n; i++){
        ll x = dsu2.get(i);
        if(dsu2.e[x].size() == 2){/*cout << "3" << endl;*/return 0;}
        if(dsu2.e[x].size() > 2 && !used[x]){
            for(j = 0;j < dsu2.e[x].size() - 1; j++){
                ans[dsu2.e[x][j]][dsu2.e[x][j + 1]] = 1;
                ans[dsu2.e[x][j + 1]][dsu2.e[x][j]] = 1;
            }
            //cout << "#" << endl;
            ans[dsu2.e[x][0]][dsu2.e[x].back()] = ans[dsu2.e[x].back()][dsu2.e[x][0]] = 1;
            used[x] = 1;
        }
        x = dsu1.get(i);
        if(dsu1.e[x].size() < 2){continue;}
        if(!vis[x]){
            for(j = 0;j < dsu1.e[x].size() - 1; j++){
//                cout << dsu1.e[x][j] << ' ';
                ans[dsu1.e[x][j]][dsu1.e[x][j + 1]] = 1;
                ans[dsu1.e[x][j + 1]][dsu1.e[x][j]] = 1;
            }
//            cout << dsu1.e[x][j] << endl;
            vis[x] = 1;
        }
    }

    build(ans);
    return 1;
}
/*
4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

*/

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:102:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |             for(j = 0;j < dsu2.e[x].size() - 1; j++){
      |                       ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:113:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |             for(j = 0;j < dsu1.e[x].size() - 1; j++){
      |                       ~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1624 KB Output is correct
7 Correct 186 ms 23484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1624 KB Output is correct
7 Correct 186 ms 23484 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 165 ms 23376 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 904 KB Output is correct
17 Correct 72 ms 13532 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 43 ms 6336 KB Output is correct
21 Correct 170 ms 23336 KB Output is correct
22 Correct 171 ms 23492 KB Output is correct
23 Correct 174 ms 23556 KB Output is correct
24 Correct 161 ms 23464 KB Output is correct
25 Correct 63 ms 13648 KB Output is correct
26 Correct 58 ms 13568 KB Output is correct
27 Correct 172 ms 23380 KB Output is correct
28 Correct 161 ms 23516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 440 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 7 ms 1372 KB Output is correct
9 Correct 167 ms 23464 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1184 KB Output is correct
13 Correct 162 ms 23440 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 78 ms 13572 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 40 ms 6400 KB Output is correct
22 Correct 162 ms 23376 KB Output is correct
23 Correct 161 ms 23380 KB Output is correct
24 Correct 168 ms 23380 KB Output is correct
25 Correct 71 ms 13648 KB Output is correct
26 Correct 67 ms 13812 KB Output is correct
27 Correct 161 ms 23364 KB Output is correct
28 Correct 167 ms 23380 KB Output is correct
29 Correct 70 ms 13508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 41 ms 6276 KB Output is correct
5 Correct 164 ms 23380 KB Output is correct
6 Correct 164 ms 23380 KB Output is correct
7 Correct 174 ms 23556 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 46 ms 6508 KB Output is correct
10 Correct 182 ms 23504 KB Output is correct
11 Correct 161 ms 23380 KB Output is correct
12 Correct 167 ms 23376 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 41 ms 6448 KB Output is correct
17 Correct 164 ms 23636 KB Output is correct
18 Correct 183 ms 23368 KB Output is correct
19 Correct 166 ms 23380 KB Output is correct
20 Correct 161 ms 23468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1624 KB Output is correct
7 Correct 186 ms 23484 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 165 ms 23376 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 904 KB Output is correct
17 Correct 72 ms 13532 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 43 ms 6336 KB Output is correct
21 Correct 170 ms 23336 KB Output is correct
22 Correct 171 ms 23492 KB Output is correct
23 Correct 174 ms 23556 KB Output is correct
24 Correct 161 ms 23464 KB Output is correct
25 Correct 63 ms 13648 KB Output is correct
26 Correct 58 ms 13568 KB Output is correct
27 Correct 172 ms 23380 KB Output is correct
28 Correct 161 ms 23516 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 440 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 7 ms 1372 KB Output is correct
37 Correct 167 ms 23464 KB Output is correct
38 Correct 1 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 7 ms 1184 KB Output is correct
41 Correct 162 ms 23440 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 860 KB Output is correct
45 Correct 78 ms 13572 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 344 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 40 ms 6400 KB Output is correct
50 Correct 162 ms 23376 KB Output is correct
51 Correct 161 ms 23380 KB Output is correct
52 Correct 168 ms 23380 KB Output is correct
53 Correct 71 ms 13648 KB Output is correct
54 Correct 67 ms 13812 KB Output is correct
55 Correct 161 ms 23364 KB Output is correct
56 Correct 167 ms 23380 KB Output is correct
57 Correct 70 ms 13508 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 344 KB Output is correct
60 Correct 3 ms 860 KB Output is correct
61 Correct 75 ms 13564 KB Output is correct
62 Correct 0 ms 344 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 41 ms 6296 KB Output is correct
66 Correct 65 ms 13480 KB Output is correct
67 Correct 61 ms 13468 KB Output is correct
68 Correct 68 ms 13708 KB Output is correct
69 Correct 72 ms 13524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1624 KB Output is correct
7 Correct 186 ms 23484 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 165 ms 23376 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 904 KB Output is correct
17 Correct 72 ms 13532 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 43 ms 6336 KB Output is correct
21 Correct 170 ms 23336 KB Output is correct
22 Correct 171 ms 23492 KB Output is correct
23 Correct 174 ms 23556 KB Output is correct
24 Correct 161 ms 23464 KB Output is correct
25 Correct 63 ms 13648 KB Output is correct
26 Correct 58 ms 13568 KB Output is correct
27 Correct 172 ms 23380 KB Output is correct
28 Correct 161 ms 23516 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 440 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 7 ms 1372 KB Output is correct
37 Correct 167 ms 23464 KB Output is correct
38 Correct 1 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 7 ms 1184 KB Output is correct
41 Correct 162 ms 23440 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 860 KB Output is correct
45 Correct 78 ms 13572 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 344 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 40 ms 6400 KB Output is correct
50 Correct 162 ms 23376 KB Output is correct
51 Correct 161 ms 23380 KB Output is correct
52 Correct 168 ms 23380 KB Output is correct
53 Correct 71 ms 13648 KB Output is correct
54 Correct 67 ms 13812 KB Output is correct
55 Correct 161 ms 23364 KB Output is correct
56 Correct 167 ms 23380 KB Output is correct
57 Correct 70 ms 13508 KB Output is correct
58 Correct 0 ms 344 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 41 ms 6276 KB Output is correct
62 Correct 164 ms 23380 KB Output is correct
63 Correct 164 ms 23380 KB Output is correct
64 Correct 174 ms 23556 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 46 ms 6508 KB Output is correct
67 Correct 182 ms 23504 KB Output is correct
68 Correct 161 ms 23380 KB Output is correct
69 Correct 167 ms 23376 KB Output is correct
70 Correct 1 ms 344 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 1 ms 348 KB Output is correct
73 Correct 41 ms 6448 KB Output is correct
74 Correct 164 ms 23636 KB Output is correct
75 Correct 183 ms 23368 KB Output is correct
76 Correct 166 ms 23380 KB Output is correct
77 Correct 161 ms 23468 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 344 KB Output is correct
80 Correct 3 ms 860 KB Output is correct
81 Correct 75 ms 13564 KB Output is correct
82 Correct 0 ms 344 KB Output is correct
83 Correct 1 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 41 ms 6296 KB Output is correct
86 Correct 65 ms 13480 KB Output is correct
87 Correct 61 ms 13468 KB Output is correct
88 Correct 68 ms 13708 KB Output is correct
89 Correct 72 ms 13524 KB Output is correct
90 Correct 1 ms 344 KB Output is correct
91 Correct 0 ms 344 KB Output is correct
92 Correct 3 ms 860 KB Output is correct
93 Correct 64 ms 13656 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 1 ms 344 KB Output is correct
96 Correct 0 ms 348 KB Output is correct
97 Correct 15 ms 3932 KB Output is correct
98 Correct 63 ms 14204 KB Output is correct
99 Correct 60 ms 14328 KB Output is correct
100 Correct 59 ms 14160 KB Output is correct
101 Correct 59 ms 14216 KB Output is correct
102 Correct 60 ms 14164 KB Output is correct
103 Correct 60 ms 14160 KB Output is correct
104 Correct 61 ms 14164 KB Output is correct
105 Correct 63 ms 14160 KB Output is correct