Submission #621317

# Submission time Handle Problem Language Result Execution time Memory
621317 2022-08-03T17:14:04 Z Mr_Husanboy Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
228 ms 24068 KB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;


struct DSU{
    vector<int> parent, sz;
    void init(int n){
        parent.resize(n), sz.resize(n);
        for(int i = 0; i < n; i ++) parent[i] = i, sz[i] = 1;
    }

    int get(int a){
        return parent[a] == a ? a : parent[a] = get(parent[a]);
    }

    void unite(int a, int b){
        a = get(a); b = get(b);
        if(a==b) return;
        if(sz[a] < sz[b]) swap(a,b);
        sz[a] += sz[b];
        parent[b] = a;
    }
};


int construct(vector<vector<int>> p) {
    bool ok = 0;
    bool yes1 = 0, yes2 = 0;
	int n = p.size();DSU gr; gr.init(n);
	vector<vector<int>> b(n,vector<int> (n));
    DSU t; t.init(n);
    for(int j = 0; j < n; j ++){
        for(int i = 0; i < n; i ++){
            if(p[i][j] == 1) t.unite(i,j),gr.unite(i,j);
            if(p[i][j] == 3) ok = 1;
            if(p[i][j] == 2) yes2 = 1;
        }
    }

    if(ok) return 0;
    for(int i = 0; i < n; i ++){
        for(int j=0; j < n; j ++){
            if(gr.get(i) == gr.get(j) && p[i][j] != 1) return 0;
        }
    }



    vector<vector<int>> par(n);

    for(int i = 0; i < n; i ++){
        par[t.get(i)].push_back(i);
    }

    vector<int> comp;
    for(int i = 0; i < n; i ++){
        if(par[i].empty()) continue;
        for(int j = 1; j < par[i].size(); j ++){
            int u = par[i][j-1], v = par[i][j];
            b[u][v] = b[v][u] = 1; gr.unite(v,u);
        }
        comp.push_back(par[i][0]);
    }
    t.init(n);
    for(int i = 0; i < comp.size(); i ++){
        for(int j = i + 1; j < comp.size(); j++){
            int u = comp[i], v = comp[j];
            if(p[u][v] == 2){
                t.unite(u,v); //cout << u << ' ' << v << endl;
            }
        }
    }



    for(int i = 0; i < n; i ++) par[i].clear();
    for(int i = 0; i < n; i ++){
        par[t.get(i)].push_back(i);
    }
    for(int i = 0; i < n; i ++){
        if(par[i].size() == 2) return 0;
        for(int j = 1; j < par[i].size(); j ++){
            int u = par[i][j-1], v = par[i][j];
            b[u][v] = b[v][u] = 1; gr.unite(v,u);
        }
        if(par[i].size() > 1){
            int u = par[i][0], v = par[i].back();
            b[u][v] = b[v][u] = 1; gr.unite(u,v);
        }
    }

    for(int i = 0; i < n; i ++){
        for(int j=0; j < n; j ++){
            if(gr.get(i) == gr.get(j) && p[i][j] == 0) return 0;
        }
    }

    build(b);
    return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:60:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |         for(int j = 1; j < par[i].size(); j ++){
      |                        ~~^~~~~~~~~~~~~~~
supertrees.cpp:67:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for(int i = 0; i < comp.size(); i ++){
      |                    ~~^~~~~~~~~~~~~
supertrees.cpp:68:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |         for(int j = i + 1; j < comp.size(); j++){
      |                            ~~^~~~~~~~~~~~~
supertrees.cpp:84:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |         for(int j = 1; j < par[i].size(); j ++){
      |                        ~~^~~~~~~~~~~~~~~
supertrees.cpp:30:10: warning: unused variable 'yes1' [-Wunused-variable]
   30 |     bool yes1 = 0, yes2 = 0;
      |          ^~~~
supertrees.cpp:30:20: warning: variable 'yes2' set but not used [-Wunused-but-set-variable]
   30 |     bool yes1 = 0, yes2 = 0;
      |                    ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 209 ms 22164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 209 ms 22164 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 11 ms 1108 KB Output is correct
13 Correct 199 ms 22108 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 98 ms 12032 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 57 ms 5708 KB Output is correct
21 Correct 199 ms 22152 KB Output is correct
22 Correct 198 ms 22092 KB Output is correct
23 Correct 226 ms 22220 KB Output is correct
24 Correct 208 ms 22096 KB Output is correct
25 Correct 80 ms 12040 KB Output is correct
26 Correct 85 ms 12108 KB Output is correct
27 Correct 222 ms 22032 KB Output is correct
28 Correct 218 ms 22116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 9 ms 1184 KB Output is correct
9 Correct 180 ms 22028 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 10 ms 1176 KB Output is correct
13 Correct 225 ms 22068 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 4 ms 800 KB Output is correct
17 Correct 122 ms 14120 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 49 ms 6264 KB Output is correct
22 Correct 214 ms 24000 KB Output is correct
23 Correct 206 ms 23980 KB Output is correct
24 Correct 216 ms 24068 KB Output is correct
25 Correct 89 ms 14072 KB Output is correct
26 Correct 87 ms 14064 KB Output is correct
27 Correct 201 ms 24032 KB Output is correct
28 Correct 222 ms 23992 KB Output is correct
29 Correct 79 ms 14156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 52 ms 5756 KB Output is correct
5 Correct 228 ms 22084 KB Output is correct
6 Correct 194 ms 22092 KB Output is correct
7 Correct 222 ms 22092 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 56 ms 5764 KB Output is correct
10 Correct 193 ms 22040 KB Output is correct
11 Correct 206 ms 22104 KB Output is correct
12 Correct 214 ms 22204 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 56 ms 5708 KB Output is correct
17 Correct 207 ms 22088 KB Output is correct
18 Correct 199 ms 22020 KB Output is correct
19 Correct 194 ms 22020 KB Output is correct
20 Correct 181 ms 22036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 209 ms 22164 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 11 ms 1108 KB Output is correct
13 Correct 199 ms 22108 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 98 ms 12032 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 57 ms 5708 KB Output is correct
21 Correct 199 ms 22152 KB Output is correct
22 Correct 198 ms 22092 KB Output is correct
23 Correct 226 ms 22220 KB Output is correct
24 Correct 208 ms 22096 KB Output is correct
25 Correct 80 ms 12040 KB Output is correct
26 Correct 85 ms 12108 KB Output is correct
27 Correct 222 ms 22032 KB Output is correct
28 Correct 218 ms 22116 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 9 ms 1184 KB Output is correct
37 Correct 180 ms 22028 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 10 ms 1176 KB Output is correct
41 Correct 225 ms 22068 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 296 KB Output is correct
44 Correct 4 ms 800 KB Output is correct
45 Correct 122 ms 14120 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 49 ms 6264 KB Output is correct
50 Correct 214 ms 24000 KB Output is correct
51 Correct 206 ms 23980 KB Output is correct
52 Correct 216 ms 24068 KB Output is correct
53 Correct 89 ms 14072 KB Output is correct
54 Correct 87 ms 14064 KB Output is correct
55 Correct 201 ms 24032 KB Output is correct
56 Correct 222 ms 23992 KB Output is correct
57 Correct 79 ms 14156 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 4 ms 820 KB Output is correct
61 Correct 93 ms 14096 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 60 ms 6220 KB Output is correct
66 Correct 85 ms 14064 KB Output is correct
67 Correct 80 ms 13988 KB Output is correct
68 Correct 86 ms 14144 KB Output is correct
69 Correct 106 ms 14068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 209 ms 22164 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 11 ms 1108 KB Output is correct
13 Correct 199 ms 22108 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 98 ms 12032 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 57 ms 5708 KB Output is correct
21 Correct 199 ms 22152 KB Output is correct
22 Correct 198 ms 22092 KB Output is correct
23 Correct 226 ms 22220 KB Output is correct
24 Correct 208 ms 22096 KB Output is correct
25 Correct 80 ms 12040 KB Output is correct
26 Correct 85 ms 12108 KB Output is correct
27 Correct 222 ms 22032 KB Output is correct
28 Correct 218 ms 22116 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 9 ms 1184 KB Output is correct
37 Correct 180 ms 22028 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 10 ms 1176 KB Output is correct
41 Correct 225 ms 22068 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 296 KB Output is correct
44 Correct 4 ms 800 KB Output is correct
45 Correct 122 ms 14120 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 49 ms 6264 KB Output is correct
50 Correct 214 ms 24000 KB Output is correct
51 Correct 206 ms 23980 KB Output is correct
52 Correct 216 ms 24068 KB Output is correct
53 Correct 89 ms 14072 KB Output is correct
54 Correct 87 ms 14064 KB Output is correct
55 Correct 201 ms 24032 KB Output is correct
56 Correct 222 ms 23992 KB Output is correct
57 Correct 79 ms 14156 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 52 ms 5756 KB Output is correct
62 Correct 228 ms 22084 KB Output is correct
63 Correct 194 ms 22092 KB Output is correct
64 Correct 222 ms 22092 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 56 ms 5764 KB Output is correct
67 Correct 193 ms 22040 KB Output is correct
68 Correct 206 ms 22104 KB Output is correct
69 Correct 214 ms 22204 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 56 ms 5708 KB Output is correct
74 Correct 207 ms 22088 KB Output is correct
75 Correct 199 ms 22020 KB Output is correct
76 Correct 194 ms 22020 KB Output is correct
77 Correct 181 ms 22036 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 4 ms 820 KB Output is correct
81 Correct 93 ms 14096 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 60 ms 6220 KB Output is correct
86 Correct 85 ms 14064 KB Output is correct
87 Correct 80 ms 13988 KB Output is correct
88 Correct 86 ms 14144 KB Output is correct
89 Correct 106 ms 14068 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 4 ms 852 KB Output is correct
93 Correct 92 ms 14068 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 0 ms 296 KB Output is correct
97 Correct 21 ms 3696 KB Output is correct
98 Correct 83 ms 14064 KB Output is correct
99 Correct 83 ms 14056 KB Output is correct
100 Correct 104 ms 14000 KB Output is correct
101 Correct 85 ms 14068 KB Output is correct
102 Correct 81 ms 14068 KB Output is correct
103 Correct 96 ms 14068 KB Output is correct
104 Correct 94 ms 14028 KB Output is correct
105 Correct 93 ms 14068 KB Output is correct