Submission #807500

# Submission time Handle Problem Language Result Execution time Memory
807500 2023-08-04T18:18:17 Z Boomyday Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
188 ms 24068 KB
//
// Created by adavy on 8/4/2023.
//
 
 
//
// Created by adavy on 2/12/2023.
//
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!
 
using ii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
 
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vii = vector<ii>;
using vpl = vector<pl>;
using vpd = vector<pd>;
 
#define tcT template<class T
#define tcTU tcT, class U
 
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;
 
// pairs
#define mp make_pair
#define f first
#define s second
 
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
 
#define len(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
 
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const ld PI = acos((ld)-1);
 
 
 
#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>
#include "supertrees.h"
/*
static int n;
static std::vector<std::vector<int>> p;
static std::vector<std::vector<int>> b;
static bool called = false;
 
static void check(bool cond, std::string message) {
    if (!cond) {
        printf("%s\n", message.c_str());
        fclose(stdout);
        exit(0);
    }
}
 
void build(std::vector<std::vector<int>> _b) {
    check(!called, "build is called more than once");
    called = true;
    check((int)_b.size() == n, "Invalid number of rows in b");
    for (int i = 0; i < n; i++) {
        check((int)_b[i].size() == n, "Invalid number of columns in b");
    }
    b = _b;
}
*/
int N;
int construct(std::vector<std::vector<int>> P) {
    N = P.size();
    std::vector<std::vector<int>> answer(N,vi(N,0));
 
    struct DSU {
        vi anc;
 
        void init(int Size) {
            anc.rsz(Size);
            for (int i = 0; i < N; i++) {
                anc[i] = i;
            }
        }
 
        int fRt(int i) {
            return anc[i] == i ? i : anc[i] = fRt(anc[i]);
        }
 
        bool merge(int a, int o) {
            a = fRt(a), o = fRt(o);
            if (a == o) {
                return false;
            }
            anc[a] = o;
            return true;
        }
    };
 
    DSU dsu;
    dsu.init(N);
 
    F0R(i, N) F0R(j,N) if (P[i][j] > 0) dsu.merge(i,j);
    vector<vi> comps;
    vb vis(N,0);
    F0R(i,N){
        if (vis[i]) continue;
        vis[i] = 1;
        vi comp = {i};
        F0R(j,N) {
            if (j==i) continue;
            if (dsu.fRt(i)==dsu.fRt(j)) {
                vis[j] = 1;
                comp.pb(j);
            }
        }
        comps.pb(comp);
    }
 
    //cout << "cmp " << comps.size() << " " << comps[0].size() << endl;
 
 
 
 
    //cout << "foo" << endl;
 
    trav(comp,comps){
        trav(i,comp) trav(j,comp) {
            if (P[i][j]==0 or P[i][j]==3) return 0;
        }
    }
 
 
    //cout << "cmps" <<  comps.size() << endl;
 
    trav(comp,comps) {
 
        DSU treesu;
        vector<vi> treecomps;
        treesu.init(N);
        trav(i,comp) trav(j,comp) if (P[i][j]==1) treesu.merge(i,j);
        vb trvis(N,0);
        trav(i, comp){
            if (trvis[i]) continue;
            trvis[i] = 1;
            vi trcomp = {i};
            trav(j,comp){
                if (i==j) continue;
                if(treesu.fRt(i)==treesu.fRt(j)){
                    trvis[j] = 1;
                    trcomp.pb(j);
                }
            }
            treecomps.pb(trcomp);
        }
        trav(trcmp, treecomps) trav(i, trcmp) trav(j,trcmp) if (P[i][j] == 2) return 0;
        if (treecomps.size()==2) return 0;
        F0R(i, treecomps.size()){
            //connect the cycle node
            answer[treecomps[i][0]][treecomps[(i+1)%treecomps.size()][0]]=1;
            answer[treecomps[(i+1)%treecomps.size()][0]][treecomps[i][0]]=1;
 
            // connect the tree nodes
            F0R(j, treecomps[i].size()-1){
                answer[treecomps[i][j]][treecomps[i][j+1]]=1;
                answer[treecomps[i][j+1]][treecomps[i][j]]=1;
            }
        }
 
 
 
    }
 
    F0R(i, N) answer[i][i] = 0;
 
    build(answer);
    return 1;
}
 
/*
int main() {
    assert(scanf("%d", &n) == 1);
 
    p.resize(n);
    for (int i = 0; i < n; i++) {
        p[i].resize(n);
    }
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            assert(scanf("%d", &p[i][j]) == 1);
        }
    }
    fclose(stdin);
 
    int possible = construct(p);
 
    check(possible == 0 || possible == 1, "Invalid return value of construct");
    if (possible == 1) {
        check(called, "construct returned 1 without calling build");
    } else {
        check(!called, "construct called build but returned 0");
    }
 
    printf("%d\n", possible);
    if (possible == 1) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (j) {
                    printf(" ");
                }
                printf("%d", b[i][j]);
            }
            printf("\n");
        }
    }
    fclose(stdout);
}
*/

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:43:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
      |                                        ^
supertrees.cpp:44:18: note: in expansion of macro 'FOR'
   44 | #define F0R(i,a) FOR(i,0,a)
      |                  ^~~
supertrees.cpp:186:9: note: in expansion of macro 'F0R'
  186 |         F0R(i, treecomps.size()){
      |         ^~~
supertrees.cpp:43:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
      |                                        ^
supertrees.cpp:44:18: note: in expansion of macro 'FOR'
   44 | #define F0R(i,a) FOR(i,0,a)
      |                  ^~~
supertrees.cpp:192:13: note: in expansion of macro 'F0R'
  192 |             F0R(j, treecomps[i].size()-1){
      |             ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 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 8 ms 1208 KB Output is correct
7 Correct 163 ms 22828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 8 ms 1208 KB Output is correct
7 Correct 163 ms 22828 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 296 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 6 ms 1236 KB Output is correct
13 Correct 148 ms 22768 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 80 ms 12900 KB Output is correct
18 Correct 1 ms 304 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 37 ms 6044 KB Output is correct
21 Correct 151 ms 22796 KB Output is correct
22 Correct 147 ms 22812 KB Output is correct
23 Correct 188 ms 23024 KB Output is correct
24 Correct 149 ms 22768 KB Output is correct
25 Correct 67 ms 12800 KB Output is correct
26 Correct 68 ms 12852 KB Output is correct
27 Correct 170 ms 22760 KB Output is correct
28 Correct 148 ms 22784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 7 ms 1264 KB Output is correct
9 Correct 148 ms 23208 KB Output is correct
10 Correct 1 ms 288 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1180 KB Output is correct
13 Correct 162 ms 23104 KB Output is correct
14 Correct 1 ms 244 KB Output is correct
15 Correct 0 ms 300 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 79 ms 13988 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 300 KB Output is correct
21 Correct 36 ms 6168 KB Output is correct
22 Correct 146 ms 23116 KB Output is correct
23 Correct 145 ms 23196 KB Output is correct
24 Correct 165 ms 23116 KB Output is correct
25 Correct 65 ms 14068 KB Output is correct
26 Correct 62 ms 13996 KB Output is correct
27 Correct 144 ms 23196 KB Output is correct
28 Correct 157 ms 23200 KB Output is correct
29 Correct 65 ms 14072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 36 ms 6264 KB Output is correct
5 Correct 148 ms 23188 KB Output is correct
6 Correct 146 ms 23116 KB Output is correct
7 Correct 184 ms 23344 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 37 ms 6220 KB Output is correct
10 Correct 149 ms 23116 KB Output is correct
11 Correct 153 ms 23176 KB Output is correct
12 Correct 161 ms 23120 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 37 ms 6220 KB Output is correct
17 Correct 147 ms 23984 KB Output is correct
18 Correct 149 ms 23928 KB Output is correct
19 Correct 145 ms 24068 KB Output is correct
20 Correct 150 ms 24012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 8 ms 1208 KB Output is correct
7 Correct 163 ms 22828 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 296 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 6 ms 1236 KB Output is correct
13 Correct 148 ms 22768 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 80 ms 12900 KB Output is correct
18 Correct 1 ms 304 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 37 ms 6044 KB Output is correct
21 Correct 151 ms 22796 KB Output is correct
22 Correct 147 ms 22812 KB Output is correct
23 Correct 188 ms 23024 KB Output is correct
24 Correct 149 ms 22768 KB Output is correct
25 Correct 67 ms 12800 KB Output is correct
26 Correct 68 ms 12852 KB Output is correct
27 Correct 170 ms 22760 KB Output is correct
28 Correct 148 ms 22784 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 300 KB Output is correct
34 Correct 1 ms 296 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 7 ms 1264 KB Output is correct
37 Correct 148 ms 23208 KB Output is correct
38 Correct 1 ms 288 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 7 ms 1180 KB Output is correct
41 Correct 162 ms 23104 KB Output is correct
42 Correct 1 ms 244 KB Output is correct
43 Correct 0 ms 300 KB Output is correct
44 Correct 4 ms 852 KB Output is correct
45 Correct 79 ms 13988 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 300 KB Output is correct
49 Correct 36 ms 6168 KB Output is correct
50 Correct 146 ms 23116 KB Output is correct
51 Correct 145 ms 23196 KB Output is correct
52 Correct 165 ms 23116 KB Output is correct
53 Correct 65 ms 14068 KB Output is correct
54 Correct 62 ms 13996 KB Output is correct
55 Correct 144 ms 23196 KB Output is correct
56 Correct 157 ms 23200 KB Output is correct
57 Correct 65 ms 14072 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 82 ms 14064 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 300 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 36 ms 6188 KB Output is correct
66 Correct 63 ms 14068 KB Output is correct
67 Correct 65 ms 14076 KB Output is correct
68 Correct 61 ms 14072 KB Output is correct
69 Correct 63 ms 14156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 8 ms 1208 KB Output is correct
7 Correct 163 ms 22828 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 296 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 6 ms 1236 KB Output is correct
13 Correct 148 ms 22768 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 80 ms 12900 KB Output is correct
18 Correct 1 ms 304 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 37 ms 6044 KB Output is correct
21 Correct 151 ms 22796 KB Output is correct
22 Correct 147 ms 22812 KB Output is correct
23 Correct 188 ms 23024 KB Output is correct
24 Correct 149 ms 22768 KB Output is correct
25 Correct 67 ms 12800 KB Output is correct
26 Correct 68 ms 12852 KB Output is correct
27 Correct 170 ms 22760 KB Output is correct
28 Correct 148 ms 22784 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 300 KB Output is correct
34 Correct 1 ms 296 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 7 ms 1264 KB Output is correct
37 Correct 148 ms 23208 KB Output is correct
38 Correct 1 ms 288 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 7 ms 1180 KB Output is correct
41 Correct 162 ms 23104 KB Output is correct
42 Correct 1 ms 244 KB Output is correct
43 Correct 0 ms 300 KB Output is correct
44 Correct 4 ms 852 KB Output is correct
45 Correct 79 ms 13988 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 300 KB Output is correct
49 Correct 36 ms 6168 KB Output is correct
50 Correct 146 ms 23116 KB Output is correct
51 Correct 145 ms 23196 KB Output is correct
52 Correct 165 ms 23116 KB Output is correct
53 Correct 65 ms 14068 KB Output is correct
54 Correct 62 ms 13996 KB Output is correct
55 Correct 144 ms 23196 KB Output is correct
56 Correct 157 ms 23200 KB Output is correct
57 Correct 65 ms 14072 KB Output is correct
58 Correct 1 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 36 ms 6264 KB Output is correct
62 Correct 148 ms 23188 KB Output is correct
63 Correct 146 ms 23116 KB Output is correct
64 Correct 184 ms 23344 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 37 ms 6220 KB Output is correct
67 Correct 149 ms 23116 KB Output is correct
68 Correct 153 ms 23176 KB Output is correct
69 Correct 161 ms 23120 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 37 ms 6220 KB Output is correct
74 Correct 147 ms 23984 KB Output is correct
75 Correct 149 ms 23928 KB Output is correct
76 Correct 145 ms 24068 KB Output is correct
77 Correct 150 ms 24012 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 82 ms 14064 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 0 ms 300 KB Output is correct
84 Correct 0 ms 212 KB Output is correct
85 Correct 36 ms 6188 KB Output is correct
86 Correct 63 ms 14068 KB Output is correct
87 Correct 65 ms 14076 KB Output is correct
88 Correct 61 ms 14072 KB Output is correct
89 Correct 63 ms 14156 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 4 ms 852 KB Output is correct
93 Correct 79 ms 13996 KB Output is correct
94 Correct 0 ms 296 KB Output is correct
95 Correct 0 ms 212 KB Output is correct
96 Correct 0 ms 212 KB Output is correct
97 Correct 15 ms 3796 KB Output is correct
98 Correct 62 ms 14100 KB Output is correct
99 Correct 63 ms 14072 KB Output is correct
100 Correct 63 ms 14084 KB Output is correct
101 Correct 64 ms 14032 KB Output is correct
102 Correct 75 ms 14028 KB Output is correct
103 Correct 76 ms 14084 KB Output is correct
104 Correct 76 ms 14084 KB Output is correct
105 Correct 82 ms 14084 KB Output is correct