Submission #305434

# Submission time Handle Problem Language Result Execution time Memory
305434 2020-09-23T05:49:06 Z Jarif_Rahman Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
276 ms 27428 KB
#include "supertrees.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
vector<vector<int>> v;
vector <vector<int>> dsu1, dsu11;
vector <int> dsu2, dsu12;
void unite(int a, int b){
    if(dsu2[a] == dsu2[b]) return;
    int x = dsu2[a], y = dsu2[b];
    if(dsu1[x].size() < dsu1.size()){
        while(!dsu1[x].empty()){
            int ss = dsu1[x].back();
            dsu1[x].pop_back();
            dsu2[ss] = y;
            dsu1[y].pb(ss);
        }
    }
    else{
        while(!dsu1[y].empty()){
            int ss = dsu1[y].back();
            dsu1[y].pop_back();
            dsu2[ss] = x;
            dsu1[x].pb(ss);
        }
    }
}
void unite1(int a, int b){
    if(dsu12[a] == dsu12[b]) return;
    int x = dsu12[a], y = dsu12[b];
    if(dsu11[x].size() < dsu11.size()){
        while(!dsu11[x].empty()){
            int ss = dsu11[x].back();
            dsu11[x].pop_back();
            dsu12[ss] = y;
            dsu11[y].pb(ss);
        }
    }
    else{
        while(!dsu1[y].empty()){
            int ss = dsu1[y].back();
            dsu1[y].pop_back();
            dsu2[ss] = x;
            dsu1[x].pb(ss);
        }
    }
}
int construct(vector<vector<int>> p){
    v.clear();
    dsu1.clear();
    dsu2.clear();
    dsu11.clear();
    dsu12.clear();
    int n = p.size();
    v = vector<vector<int>>(n, vector<int>(n, 0));
    dsu1 = vector<vector<int>>(n);
    dsu2 = vector<int>(n);
    for(int i = 0; i < n ; i++) dsu1[i].pb(i);
    for(int i = 0; i < n; i++) dsu2[i] = i;
    dsu11 = dsu1;
    dsu12 =  dsu2;
    vector <int> zot(4, 0);
    for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(i != j) zot[p[i][j]]++;
    if(zot[3] > 0) return 0;
    for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(p[i][j] != 0) unite(i, j);
    bool bl = 1;
    for(auto sth: dsu1){
        if(sth.size() < 2) continue;
        if(sth.size() == 2){
            int x = sth[0], y = sth[1];
            if(p[x][y] == 2){
                bl = 0;
                break;
            }
            else continue;
        }
        for(int i = 0; i < sth.size(); i++) for(int j = 0; j < sth.size(); j++){
            if(i == j) continue;
            if(p[sth[i]][sth[j]] == 0){
                bl = 0;
                break;
            }
        }
    }
    if(!bl) return 0;
    for(auto sth: dsu1){
        if(sth.size() < 2) continue;
        vector <int> two, one;
        for(int x: sth) for(int y: sth){
            if(x == y) continue;
            if(p[x][y] == 1) unite1(x, y);
        }
        for(int x: sth){
            int o = 0,tw = 0;
            for(int k = 0; k < n; k++){
              	if(k == x) continue;
                if(p[x][k] == 1) o++;
            }
            if(o == 0) two.pb(x);
            else one.pb(x);
        }
        for(int a: sth){
            if(dsu11[a].size() < 2) continue;
            for(int x: dsu11[a]){
                int o = 0, tw = 0;
                for(int y: dsu11[a]) if(p[x][y] != 1) return 0;
                for(int y: p[x]) if(y == 1) o++;
                if(o > dsu11[a].size()) return 0;
            }
            for(int i = 0; i < (int)dsu11[a].size() - 1; i++){
                int x = dsu11[a][i], y = dsu11[a][i+1];
                v[x][y] = 1;
                v[y][x] = 1;
            }
        }
        vector <int> oo;
        for(int a: sth) if(dsu11[a].size() > 0) oo.pb(dsu11[a][0]);
        for(int i = 1; i < oo.size(); i++){
            int x = oo[i], y = oo[i-1];
            v[x][y] = 1;
            v[y][x] = 1;
        }
        if(oo.size() > 1){
            int x = oo[0], y = oo.back();
            v[x][y] = 1;
            v[y][x] = 1;
        }
    }
    if(!bl) return 0;
    build(v);
    return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:81:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for(int i = 0; i < sth.size(); i++) for(int j = 0; j < sth.size(); j++){
      |                        ~~^~~~~~~~~~~~
supertrees.cpp:81:62: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for(int i = 0; i < sth.size(); i++) for(int j = 0; j < sth.size(); j++){
      |                                                            ~~^~~~~~~~~~~~
supertrees.cpp:98:23: warning: unused variable 'tw' [-Wunused-variable]
   98 |             int o = 0,tw = 0;
      |                       ^~
supertrees.cpp:112:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |                 if(o > dsu11[a].size()) return 0;
      |                    ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:109:28: warning: unused variable 'tw' [-Wunused-variable]
  109 |                 int o = 0, tw = 0;
      |                            ^~
supertrees.cpp:122:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |         for(int i = 1; i < oo.size(); i++){
      |                        ~~^~~~~~~~~~~
# 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 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 12 ms 1408 KB Output is correct
7 Correct 271 ms 27428 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 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 12 ms 1408 KB Output is correct
7 Correct 271 ms 27428 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 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 270 ms 22312 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 7 ms 896 KB Output is correct
17 Correct 127 ms 14968 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 256 KB Output is correct
20 Correct 64 ms 5880 KB Output is correct
21 Correct 251 ms 22776 KB Output is correct
22 Correct 249 ms 22136 KB Output is correct
23 Correct 271 ms 24824 KB Output is correct
24 Correct 240 ms 22268 KB Output is correct
25 Correct 116 ms 12664 KB Output is correct
26 Correct 114 ms 12280 KB Output is correct
27 Correct 276 ms 24952 KB Output is correct
28 Correct 271 ms 22264 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 256 KB Output is correct
4 Correct 1 ms 256 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 384 KB Output is correct
8 Correct 12 ms 1280 KB Output is correct
9 Correct 252 ms 22264 KB Output is correct
10 Correct 1 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 268 ms 24788 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 6 ms 896 KB Output is correct
17 Correct 128 ms 14936 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 256 KB Output is correct
20 Correct 1 ms 256 KB Output is correct
21 Correct 64 ms 5880 KB Output is correct
22 Correct 270 ms 22520 KB Output is correct
23 Correct 248 ms 22228 KB Output is correct
24 Correct 261 ms 23544 KB Output is correct
25 Correct 117 ms 12280 KB Output is correct
26 Correct 120 ms 12536 KB Output is correct
27 Correct 250 ms 22264 KB Output is correct
28 Correct 265 ms 23544 KB Output is correct
29 Correct 107 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 65 ms 5964 KB Output is correct
5 Correct 252 ms 22860 KB Output is correct
6 Correct 249 ms 22264 KB Output is correct
7 Correct 275 ms 24824 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 69 ms 5880 KB Output is correct
10 Correct 254 ms 22520 KB Output is correct
11 Correct 247 ms 22264 KB Output is correct
12 Correct 271 ms 23512 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 65 ms 5884 KB Output is correct
17 Correct 250 ms 22520 KB Output is correct
18 Correct 252 ms 22520 KB Output is correct
19 Correct 258 ms 22392 KB Output is correct
20 Correct 262 ms 22228 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 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 12 ms 1408 KB Output is correct
7 Correct 271 ms 27428 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 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 270 ms 22312 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 7 ms 896 KB Output is correct
17 Correct 127 ms 14968 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 256 KB Output is correct
20 Correct 64 ms 5880 KB Output is correct
21 Correct 251 ms 22776 KB Output is correct
22 Correct 249 ms 22136 KB Output is correct
23 Correct 271 ms 24824 KB Output is correct
24 Correct 240 ms 22268 KB Output is correct
25 Correct 116 ms 12664 KB Output is correct
26 Correct 114 ms 12280 KB Output is correct
27 Correct 276 ms 24952 KB Output is correct
28 Correct 271 ms 22264 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 256 KB Output is correct
32 Correct 1 ms 256 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 384 KB Output is correct
36 Correct 12 ms 1280 KB Output is correct
37 Correct 252 ms 22264 KB Output is correct
38 Correct 1 ms 256 KB Output is correct
39 Correct 1 ms 256 KB Output is correct
40 Correct 11 ms 1280 KB Output is correct
41 Correct 268 ms 24788 KB Output is correct
42 Correct 1 ms 256 KB Output is correct
43 Correct 1 ms 256 KB Output is correct
44 Correct 6 ms 896 KB Output is correct
45 Correct 128 ms 14936 KB Output is correct
46 Correct 1 ms 256 KB Output is correct
47 Correct 1 ms 256 KB Output is correct
48 Correct 1 ms 256 KB Output is correct
49 Correct 64 ms 5880 KB Output is correct
50 Correct 270 ms 22520 KB Output is correct
51 Correct 248 ms 22228 KB Output is correct
52 Correct 261 ms 23544 KB Output is correct
53 Correct 117 ms 12280 KB Output is correct
54 Correct 120 ms 12536 KB Output is correct
55 Correct 250 ms 22264 KB Output is correct
56 Correct 265 ms 23544 KB Output is correct
57 Correct 107 ms 12280 KB Output is correct
58 Correct 1 ms 256 KB Output is correct
59 Correct 0 ms 256 KB Output is correct
60 Correct 6 ms 896 KB Output is correct
61 Correct 125 ms 14968 KB Output is correct
62 Correct 1 ms 384 KB Output is correct
63 Correct 1 ms 256 KB Output is correct
64 Correct 0 ms 256 KB Output is correct
65 Correct 66 ms 5880 KB Output is correct
66 Correct 114 ms 12504 KB Output is correct
67 Correct 116 ms 12504 KB Output is correct
68 Correct 112 ms 12408 KB Output is correct
69 Correct 108 ms 12280 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 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 12 ms 1408 KB Output is correct
7 Correct 271 ms 27428 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 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 270 ms 22312 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 7 ms 896 KB Output is correct
17 Correct 127 ms 14968 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 256 KB Output is correct
20 Correct 64 ms 5880 KB Output is correct
21 Correct 251 ms 22776 KB Output is correct
22 Correct 249 ms 22136 KB Output is correct
23 Correct 271 ms 24824 KB Output is correct
24 Correct 240 ms 22268 KB Output is correct
25 Correct 116 ms 12664 KB Output is correct
26 Correct 114 ms 12280 KB Output is correct
27 Correct 276 ms 24952 KB Output is correct
28 Correct 271 ms 22264 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 256 KB Output is correct
32 Correct 1 ms 256 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 384 KB Output is correct
36 Correct 12 ms 1280 KB Output is correct
37 Correct 252 ms 22264 KB Output is correct
38 Correct 1 ms 256 KB Output is correct
39 Correct 1 ms 256 KB Output is correct
40 Correct 11 ms 1280 KB Output is correct
41 Correct 268 ms 24788 KB Output is correct
42 Correct 1 ms 256 KB Output is correct
43 Correct 1 ms 256 KB Output is correct
44 Correct 6 ms 896 KB Output is correct
45 Correct 128 ms 14936 KB Output is correct
46 Correct 1 ms 256 KB Output is correct
47 Correct 1 ms 256 KB Output is correct
48 Correct 1 ms 256 KB Output is correct
49 Correct 64 ms 5880 KB Output is correct
50 Correct 270 ms 22520 KB Output is correct
51 Correct 248 ms 22228 KB Output is correct
52 Correct 261 ms 23544 KB Output is correct
53 Correct 117 ms 12280 KB Output is correct
54 Correct 120 ms 12536 KB Output is correct
55 Correct 250 ms 22264 KB Output is correct
56 Correct 265 ms 23544 KB Output is correct
57 Correct 107 ms 12280 KB Output is correct
58 Correct 1 ms 256 KB Output is correct
59 Correct 1 ms 256 KB Output is correct
60 Correct 1 ms 256 KB Output is correct
61 Correct 65 ms 5964 KB Output is correct
62 Correct 252 ms 22860 KB Output is correct
63 Correct 249 ms 22264 KB Output is correct
64 Correct 275 ms 24824 KB Output is correct
65 Correct 1 ms 256 KB Output is correct
66 Correct 69 ms 5880 KB Output is correct
67 Correct 254 ms 22520 KB Output is correct
68 Correct 247 ms 22264 KB Output is correct
69 Correct 271 ms 23512 KB Output is correct
70 Correct 1 ms 256 KB Output is correct
71 Correct 0 ms 256 KB Output is correct
72 Correct 1 ms 256 KB Output is correct
73 Correct 65 ms 5884 KB Output is correct
74 Correct 250 ms 22520 KB Output is correct
75 Correct 252 ms 22520 KB Output is correct
76 Correct 258 ms 22392 KB Output is correct
77 Correct 262 ms 22228 KB Output is correct
78 Correct 1 ms 256 KB Output is correct
79 Correct 0 ms 256 KB Output is correct
80 Correct 6 ms 896 KB Output is correct
81 Correct 125 ms 14968 KB Output is correct
82 Correct 1 ms 384 KB Output is correct
83 Correct 1 ms 256 KB Output is correct
84 Correct 0 ms 256 KB Output is correct
85 Correct 66 ms 5880 KB Output is correct
86 Correct 114 ms 12504 KB Output is correct
87 Correct 116 ms 12504 KB Output is correct
88 Correct 112 ms 12408 KB Output is correct
89 Correct 108 ms 12280 KB Output is correct
90 Correct 1 ms 256 KB Output is correct
91 Correct 1 ms 256 KB Output is correct
92 Correct 5 ms 768 KB Output is correct
93 Correct 148 ms 12280 KB Output is correct
94 Correct 1 ms 256 KB Output is correct
95 Correct 1 ms 256 KB Output is correct
96 Correct 1 ms 256 KB Output is correct
97 Correct 29 ms 3324 KB Output is correct
98 Correct 107 ms 12280 KB Output is correct
99 Correct 112 ms 12280 KB Output is correct
100 Correct 106 ms 12280 KB Output is correct
101 Correct 109 ms 12280 KB Output is correct
102 Correct 112 ms 12248 KB Output is correct
103 Correct 113 ms 12280 KB Output is correct
104 Correct 111 ms 12280 KB Output is correct
105 Correct 115 ms 12280 KB Output is correct