Submission #934135

# Submission time Handle Problem Language Result Execution time Memory
934135 2024-02-26T21:01:48 Z raul2008487 Connecting Supertrees (IOI20_supertrees) C++17
96 / 100
199 ms 24404 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);
            }
        }
    }
    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:98:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |             for(j = 0;j < dsu2.e[x].size() - 1; j++){
      |                       ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:109:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |             for(j = 0;j < dsu1.e[x].size() - 1; j++){
      |                       ~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1184 KB Output is correct
7 Correct 167 ms 24076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1184 KB Output is correct
7 Correct 167 ms 24076 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 592 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 157 ms 24148 KB Output is correct
14 Correct 0 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 81 ms 14332 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 41 ms 6252 KB Output is correct
21 Correct 161 ms 24072 KB Output is correct
22 Correct 164 ms 24400 KB Output is correct
23 Correct 166 ms 24144 KB Output is correct
24 Correct 165 ms 24148 KB Output is correct
25 Correct 64 ms 14344 KB Output is correct
26 Correct 65 ms 14164 KB Output is correct
27 Correct 166 ms 24204 KB Output is correct
28 Correct 158 ms 24144 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 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 7 ms 1316 KB Output is correct
9 Correct 157 ms 22156 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 1116 KB Output is correct
13 Correct 173 ms 22288 KB Output is correct
14 Correct 0 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 76 ms 12296 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 40 ms 5796 KB Output is correct
22 Correct 159 ms 22104 KB Output is correct
23 Correct 157 ms 22100 KB Output is correct
24 Correct 170 ms 22200 KB Output is correct
25 Correct 67 ms 12372 KB Output is correct
26 Correct 68 ms 12372 KB Output is correct
27 Correct 159 ms 22112 KB Output is correct
28 Correct 199 ms 22096 KB Output is correct
29 Correct 69 ms 12372 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 344 KB Output is correct
4 Correct 40 ms 6400 KB Output is correct
5 Correct 176 ms 24148 KB Output is correct
6 Correct 187 ms 24064 KB Output is correct
7 Correct 166 ms 24036 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 42 ms 6388 KB Output is correct
10 Correct 174 ms 24240 KB Output is correct
11 Correct 159 ms 24148 KB Output is correct
12 Correct 168 ms 24256 KB Output is correct
13 Correct 1 ms 348 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 6312 KB Output is correct
17 Correct 160 ms 24288 KB Output is correct
18 Correct 167 ms 24188 KB Output is correct
19 Correct 169 ms 24404 KB Output is correct
20 Correct 158 ms 24152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1184 KB Output is correct
7 Correct 167 ms 24076 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 592 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 157 ms 24148 KB Output is correct
14 Correct 0 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 81 ms 14332 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 41 ms 6252 KB Output is correct
21 Correct 161 ms 24072 KB Output is correct
22 Correct 164 ms 24400 KB Output is correct
23 Correct 166 ms 24144 KB Output is correct
24 Correct 165 ms 24148 KB Output is correct
25 Correct 64 ms 14344 KB Output is correct
26 Correct 65 ms 14164 KB Output is correct
27 Correct 166 ms 24204 KB Output is correct
28 Correct 158 ms 24144 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 7 ms 1316 KB Output is correct
37 Correct 157 ms 22156 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 7 ms 1116 KB Output is correct
41 Correct 173 ms 22288 KB Output is correct
42 Correct 0 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 76 ms 12296 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 344 KB Output is correct
49 Correct 40 ms 5796 KB Output is correct
50 Correct 159 ms 22104 KB Output is correct
51 Correct 157 ms 22100 KB Output is correct
52 Correct 170 ms 22200 KB Output is correct
53 Correct 67 ms 12372 KB Output is correct
54 Correct 68 ms 12372 KB Output is correct
55 Correct 159 ms 22112 KB Output is correct
56 Correct 199 ms 22096 KB Output is correct
57 Correct 69 ms 12372 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 4 ms 860 KB Output is correct
61 Correct 71 ms 14212 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 40 ms 6376 KB Output is correct
66 Correct 68 ms 14260 KB Output is correct
67 Correct 71 ms 14588 KB Output is correct
68 Correct 68 ms 14336 KB Output is correct
69 Correct 72 ms 14164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1184 KB Output is correct
7 Correct 167 ms 24076 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 592 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 157 ms 24148 KB Output is correct
14 Correct 0 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 81 ms 14332 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 41 ms 6252 KB Output is correct
21 Correct 161 ms 24072 KB Output is correct
22 Correct 164 ms 24400 KB Output is correct
23 Correct 166 ms 24144 KB Output is correct
24 Correct 165 ms 24148 KB Output is correct
25 Correct 64 ms 14344 KB Output is correct
26 Correct 65 ms 14164 KB Output is correct
27 Correct 166 ms 24204 KB Output is correct
28 Correct 158 ms 24144 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 7 ms 1316 KB Output is correct
37 Correct 157 ms 22156 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 7 ms 1116 KB Output is correct
41 Correct 173 ms 22288 KB Output is correct
42 Correct 0 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 76 ms 12296 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 344 KB Output is correct
49 Correct 40 ms 5796 KB Output is correct
50 Correct 159 ms 22104 KB Output is correct
51 Correct 157 ms 22100 KB Output is correct
52 Correct 170 ms 22200 KB Output is correct
53 Correct 67 ms 12372 KB Output is correct
54 Correct 68 ms 12372 KB Output is correct
55 Correct 159 ms 22112 KB Output is correct
56 Correct 199 ms 22096 KB Output is correct
57 Correct 69 ms 12372 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 344 KB Output is correct
61 Correct 40 ms 6400 KB Output is correct
62 Correct 176 ms 24148 KB Output is correct
63 Correct 187 ms 24064 KB Output is correct
64 Correct 166 ms 24036 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 42 ms 6388 KB Output is correct
67 Correct 174 ms 24240 KB Output is correct
68 Correct 159 ms 24148 KB Output is correct
69 Correct 168 ms 24256 KB Output is correct
70 Correct 1 ms 348 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 6312 KB Output is correct
74 Correct 160 ms 24288 KB Output is correct
75 Correct 167 ms 24188 KB Output is correct
76 Correct 169 ms 24404 KB Output is correct
77 Correct 158 ms 24152 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 4 ms 860 KB Output is correct
81 Correct 71 ms 14212 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 1 ms 348 KB Output is correct
84 Correct 1 ms 348 KB Output is correct
85 Correct 40 ms 6376 KB Output is correct
86 Correct 68 ms 14260 KB Output is correct
87 Correct 71 ms 14588 KB Output is correct
88 Correct 68 ms 14336 KB Output is correct
89 Correct 72 ms 14164 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 1 ms 348 KB Output is correct
92 Correct 3 ms 860 KB Output is correct
93 Correct 75 ms 14464 KB Output is correct
94 Incorrect 1 ms 348 KB Answer gives possible 1 while actual possible 0
95 Halted 0 ms 0 KB -