Submission #651681

# Submission time Handle Problem Language Result Execution time Memory
651681 2022-10-19T17:35:14 Z rafatoa Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
216 ms 24048 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#pragma GCC optimize ("Ofast")

#define double ld
const int inf = 2e9;
const int INF = 4e18;

#define F first
#define S second
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define vpi vector<pi>
#define vb vector<bool>
#define vvb vector<vb>
#define pb push_back
#define read(a) for(auto &x:a) cin >> x;
#define print(a) for(auto x:a) cout << x << " "; cout << "\n";
#define rs resize
#define as assign
#define vc vector<char>
#define vvc vector<vc>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

#define ppb pop_back
#define MP make_pair

// #define int ll

void build(vvi b);

struct DSU{
    int n;
    vi link, sz;
    void init(int x){
        n = x;
        link.clear(); sz.clear();
        link.resize(n); sz.resize(n);
        for(int i=0; i<n; i++)
            link[i] = i, sz[i] = 1;
    }
    int find(int x){
        if(x == link[x]) return x;
        return link[x] = find(link[x]);
    }
    bool same(int a, int b){
        return find(a) == find(b);
    }
    void unite(int a, int b){
        a = find(a), b = find(b);
        if(a == b) return;
        if(sz[a] < sz[b]) swap(a, b);
        sz[a] += sz[b];
        link[b] = a;
    }
};

int construct(vvi p){
    int n = p.size();
    for(int i=0; i<n; i++)
    for(int j=i+1; j<n; j++)
        if(p[i][j] == 3) return 0;

    vvi b(n, vi(n, 0));
    
    DSU dsu;
    dsu.init(n);
    for(int i=0; i<n; i++)
    for(int j=i+1; j<n; j++)
        if(p[i][j] > 0) dsu.unite(i, j);

    vvi comp(n);
    for(int i=0; i<n; i++)
        comp[dsu.find(i)].pb(i);
    
    for(int i=0; i<n; i++){
        if(comp[i].size() <= 1) continue;
        DSU dsu2;
        dsu2.init(n);
        for(int j=0; j<comp[i].size(); j++)
        for(int k=j+1; k<comp[i].size(); k++)
            if(p[comp[i][j]][comp[i][k]] == 0) return 0;
            else if(p[comp[i][j]][comp[i][k]] == 1) dsu2.unite(comp[i][j], comp[i][k]);
        
        for(int j=0; j<comp[i].size(); j++)
        for(int k=j+1; k<comp[i].size(); k++)
            if((dsu2.same(comp[i][j], comp[i][k]) && p[comp[i][j]][comp[i][k]] == 2) ||
                (!dsu2.same(comp[i][j], comp[i][k]) && p[comp[i][j]][comp[i][k]] == 1)) return 0;
        
        vvi sect(n); vi lims;
        for(int j=0; j<comp[i].size(); j++)
            sect[dsu2.find(comp[i][j])].pb(comp[i][j]);
        
        for(int j=0; j<n; j++){
            if(sect[j].empty()) continue;
            for(int k=0; k<sect[j].size()-1; k++)
                b[sect[j][k]][sect[j][k+1]] = b[sect[j][k+1]][sect[j][k]] = 1;
            lims.pb(sect[j].back());
        }

        if(lims.size() == 2) return 0;
        
        for(int j=0; j<lims.size()-1; j++)
            b[lims[j]][lims[j+1]] = b[lims[j+1]][lims[j]] = 1;
        if(lims.size() > 1)  b[lims.front()][lims.back()] = b[lims.back()][lims.front()] = 1;
    }

    build(b);
    return 1;
}

Compilation message

supertrees.cpp:9:17: warning: overflow in conversion from 'double' to 'int' changes value from '4.0e+18' to '2147483647' [-Woverflow]
    9 | const int INF = 4e18;
      |                 ^~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:84:23: 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=0; j<comp[i].size(); j++)
      |                      ~^~~~~~~~~~~~~~~
supertrees.cpp:85:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for(int k=j+1; k<comp[i].size(); k++)
      |                        ~^~~~~~~~~~~~~~~
supertrees.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for(int j=0; j<comp[i].size(); j++)
      |                      ~^~~~~~~~~~~~~~~
supertrees.cpp:90:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         for(int k=j+1; k<comp[i].size(); k++)
      |                        ~^~~~~~~~~~~~~~~
supertrees.cpp:95:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |         for(int j=0; j<comp[i].size(); j++)
      |                      ~^~~~~~~~~~~~~~~
supertrees.cpp:100:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |             for(int k=0; k<sect[j].size()-1; k++)
      |                          ~^~~~~~~~~~~~~~~~~
supertrees.cpp:107:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |         for(int j=0; j<lims.size()-1; j++)
      |                      ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 214 ms 22092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 214 ms 22092 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 10 ms 1160 KB Output is correct
13 Correct 173 ms 22080 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 720 KB Output is correct
17 Correct 93 ms 12160 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 46 ms 5688 KB Output is correct
21 Correct 193 ms 22072 KB Output is correct
22 Correct 203 ms 22028 KB Output is correct
23 Correct 204 ms 22020 KB Output is correct
24 Correct 162 ms 22084 KB Output is correct
25 Correct 72 ms 12176 KB Output is correct
26 Correct 67 ms 12164 KB Output is correct
27 Correct 186 ms 22116 KB Output is correct
28 Correct 161 ms 22016 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 0 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 7 ms 1248 KB Output is correct
9 Correct 187 ms 24048 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 272 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 177 ms 23964 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 86 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 45 ms 6196 KB Output is correct
22 Correct 175 ms 23936 KB Output is correct
23 Correct 167 ms 23932 KB Output is correct
24 Correct 175 ms 23980 KB Output is correct
25 Correct 71 ms 14156 KB Output is correct
26 Correct 70 ms 14068 KB Output is correct
27 Correct 171 ms 23924 KB Output is correct
28 Correct 177 ms 23932 KB Output is correct
29 Correct 84 ms 14076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 43 ms 5736 KB Output is correct
5 Correct 187 ms 22032 KB Output is correct
6 Correct 173 ms 21996 KB Output is correct
7 Correct 185 ms 22024 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 44 ms 5708 KB Output is correct
10 Correct 172 ms 22040 KB Output is correct
11 Correct 169 ms 22080 KB Output is correct
12 Correct 216 ms 22028 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 47 ms 5780 KB Output is correct
17 Correct 172 ms 21976 KB Output is correct
18 Correct 174 ms 22092 KB Output is correct
19 Correct 170 ms 21964 KB Output is correct
20 Correct 174 ms 22148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 214 ms 22092 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 10 ms 1160 KB Output is correct
13 Correct 173 ms 22080 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 720 KB Output is correct
17 Correct 93 ms 12160 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 46 ms 5688 KB Output is correct
21 Correct 193 ms 22072 KB Output is correct
22 Correct 203 ms 22028 KB Output is correct
23 Correct 204 ms 22020 KB Output is correct
24 Correct 162 ms 22084 KB Output is correct
25 Correct 72 ms 12176 KB Output is correct
26 Correct 67 ms 12164 KB Output is correct
27 Correct 186 ms 22116 KB Output is correct
28 Correct 161 ms 22016 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 0 ms 212 KB Output is correct
33 Correct 1 ms 300 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1248 KB Output is correct
37 Correct 187 ms 24048 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 272 KB Output is correct
40 Correct 8 ms 1236 KB Output is correct
41 Correct 177 ms 23964 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 852 KB Output is correct
45 Correct 86 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 45 ms 6196 KB Output is correct
50 Correct 175 ms 23936 KB Output is correct
51 Correct 167 ms 23932 KB Output is correct
52 Correct 175 ms 23980 KB Output is correct
53 Correct 71 ms 14156 KB Output is correct
54 Correct 70 ms 14068 KB Output is correct
55 Correct 171 ms 23924 KB Output is correct
56 Correct 177 ms 23932 KB Output is correct
57 Correct 84 ms 14076 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 4 ms 852 KB Output is correct
61 Correct 80 ms 14080 KB Output is correct
62 Correct 1 ms 300 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 50 ms 6152 KB Output is correct
66 Correct 71 ms 14064 KB Output is correct
67 Correct 73 ms 14064 KB Output is correct
68 Correct 75 ms 14064 KB Output is correct
69 Correct 70 ms 14072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 214 ms 22092 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 10 ms 1160 KB Output is correct
13 Correct 173 ms 22080 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 720 KB Output is correct
17 Correct 93 ms 12160 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 46 ms 5688 KB Output is correct
21 Correct 193 ms 22072 KB Output is correct
22 Correct 203 ms 22028 KB Output is correct
23 Correct 204 ms 22020 KB Output is correct
24 Correct 162 ms 22084 KB Output is correct
25 Correct 72 ms 12176 KB Output is correct
26 Correct 67 ms 12164 KB Output is correct
27 Correct 186 ms 22116 KB Output is correct
28 Correct 161 ms 22016 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 0 ms 212 KB Output is correct
33 Correct 1 ms 300 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1248 KB Output is correct
37 Correct 187 ms 24048 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 272 KB Output is correct
40 Correct 8 ms 1236 KB Output is correct
41 Correct 177 ms 23964 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 852 KB Output is correct
45 Correct 86 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 45 ms 6196 KB Output is correct
50 Correct 175 ms 23936 KB Output is correct
51 Correct 167 ms 23932 KB Output is correct
52 Correct 175 ms 23980 KB Output is correct
53 Correct 71 ms 14156 KB Output is correct
54 Correct 70 ms 14068 KB Output is correct
55 Correct 171 ms 23924 KB Output is correct
56 Correct 177 ms 23932 KB Output is correct
57 Correct 84 ms 14076 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 43 ms 5736 KB Output is correct
62 Correct 187 ms 22032 KB Output is correct
63 Correct 173 ms 21996 KB Output is correct
64 Correct 185 ms 22024 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 44 ms 5708 KB Output is correct
67 Correct 172 ms 22040 KB Output is correct
68 Correct 169 ms 22080 KB Output is correct
69 Correct 216 ms 22028 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 47 ms 5780 KB Output is correct
74 Correct 172 ms 21976 KB Output is correct
75 Correct 174 ms 22092 KB Output is correct
76 Correct 170 ms 21964 KB Output is correct
77 Correct 174 ms 22148 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 4 ms 852 KB Output is correct
81 Correct 80 ms 14080 KB Output is correct
82 Correct 1 ms 300 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 340 KB Output is correct
85 Correct 50 ms 6152 KB Output is correct
86 Correct 71 ms 14064 KB Output is correct
87 Correct 73 ms 14064 KB Output is correct
88 Correct 75 ms 14064 KB Output is correct
89 Correct 70 ms 14072 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 5 ms 596 KB Output is correct
93 Correct 73 ms 10116 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 300 KB Output is correct
97 Correct 18 ms 2684 KB Output is correct
98 Correct 74 ms 10104 KB Output is correct
99 Correct 69 ms 10116 KB Output is correct
100 Correct 68 ms 10096 KB Output is correct
101 Correct 67 ms 10112 KB Output is correct
102 Correct 71 ms 10116 KB Output is correct
103 Correct 67 ms 10116 KB Output is correct
104 Correct 71 ms 10096 KB Output is correct
105 Correct 81 ms 10096 KB Output is correct