Submission #963852

# Submission time Handle Problem Language Result Execution time Memory
963852 2024-04-15T20:12:09 Z Mher777 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
188 ms 28180 KB
#define _CRT_SECURE_NO_WARNINGS
#include "supertrees.h"
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
using namespace std;
mt19937 rnd(7069);

/* -------------------- Typedefs -------------------- */

typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;

/* -------------------- Usings -------------------- */

using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

/* -------------------- Defines -------------------- */

#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
#define USACO freopen("feast.in", "r", stdin); freopen("feast.out", "w", stdout);

/* -------------------- Constants -------------------- */

const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18) + 5ll;
const ll MOD = ll(998244353);
const ll MOD2 = ll(998244353);

/* -------------------- Functions -------------------- */

void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

void precision(int x) {
    cout.setf(ios::fixed | ios::showpoint);
    cout.precision(x);
}

ll gcd(ll a, ll b) {
    if (a == 0 || b == 0) return(max(a, b));
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

ll range_sum(ll a, ll b) {
    if (a > b) return 0ll;
    ll dif = a - 1, cnt = b - a + 1;
    ll ans = ((b - a + 1) * (b - a + 2)) / 2;
    ans += ((b - a + 1) * dif);
    return ans;
}

string dec_to_bin(ll a) {
    string s = "";
    for (ll i = a; i > 0; ) {
        ll k = i % 2;
        i /= 2;
        char c = k + 48;
        s += c;
    }
    if (a == 0) {
        s = "0";
    }
    reverse(all(s));
    return s;
}

ll bin_to_dec(string s) {
    ll num = 0;
    for (int i = 0; i < s.size(); i++) {
        num *= 2ll;
        num += (s[i] - '0');
    }
    return num;
}

ll factorial_by_mod(ll n, ll mod) {
    ll ans = 1;
    ll num;
    for (ll i = 1; i <= n; ++i) {
        num = i % mod;
        ans *= num;
        ans %= mod;
    }
    return ans;
}

int isPrime(ll a) {
    if (a == 1) return 0;
    for (ll i = 2; i * i <= a; i++) {
        if (a % i == 0) return 0;
    }
    return 1;
}

ll binpow(ll a, ll b) {
    if (!a) return 0;
    ll ans = 1;
    while (b) {
        if (b & 1) {
            ans *= a;
        }
        b >>= 1;
        a *= a;
    }
    return ans;
}

ll binpow_by_mod(ll a, ll b, ll mod) {
    if (!a) return 0;
    ll ans = 1;
    while (b) {
        if (b & 1) {
            ans *= a;
            ans %= mod;
        }
        b >>= 1;
        a *= a;
        a %= mod;
    }
    return ans;
}

/* -------------------- Solution -------------------- */

const int N = 1005;
int used[N], a[N][N], p[N];
vi comp[N], gr[N];
vector<vi> edge;
int n;

void make_set(int x) {
    p[x] = x;
    comp[x] = { x };
}

int find_set(int x) {
    if (x == p[x]) return x;
    return p[x] = find_set(p[x]);
}

void union_sets(int x, int y) {
    x = find_set(x), y = find_set(y);
    if (x == y) return;
    if (comp[x].size() > comp[y].size()) swap(x, y);
    p[x] = y;
    for (auto elem : comp[x]) comp[y].pub(elem);
}

void connect(int x, int y) {
    edge[x][y] = edge[y][x] = 1;
}

int construct(vector<vi> P) {
    n = P[0].size();
    edge.resize(n);
    for (int i = 0; i < n; ++i) {
        edge[i].resize(n);
        make_set(i);
        for (int j = 0; j < n; ++j) {
            a[i][j] = P[i][j];
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (!a[i][j]) continue;
            union_sets(i, j);
        }
    }
    int sz = 0;
    for (int i = 0; i < n; ++i) {
        int ind = find_set(i);
        if (used[ind]) continue;
        used[ind] = 1;
        gr[sz] = comp[ind];
        ++sz;
    }
    for (int i = 0; i < sz; ++i) {
        for (auto elem : gr[i]) {
            make_set(elem);
        }
        for (int i1 = 0; i1 < gr[i].size(); ++i1) {
            for (int i2 = i1 + 1; i2 < gr[i].size(); ++i2) {
                int d = a[gr[i][i1]][gr[i][i2]];
                if (d == 0) {
                    return 0;
                }
                if (d == 1) union_sets(gr[i][i1], gr[i][i2]);
            }
        }
        for (int i1 = 0; i1 < gr[i].size(); ++i1) {
            for (int i2 = i1 + 1; i2 < gr[i].size(); ++i2) {
                int d = a[gr[i][i1]][gr[i][i2]];
                if (!d) return 0;
                if (d == 1) union_sets(gr[i][i1], gr[i][i2]);
            }
        }
        bool f = false;
        for (int i1 = 0; i1 < gr[i].size(); ++i1) {
            for (int i2 = i1 + 1; i2 < gr[i].size(); ++i2) {
                int x = find_set(gr[i][i1]);
                int y = find_set(gr[i][i2]);
                int d = a[gr[i][i1]][gr[i][i2]];
                if (d == 2) f = true;
                if (x == y && d != 1) return 0;
                if (x != y && d != 2) return 0;
            }
        }
        set<int> st;
        for (auto elem : gr[i]) {
            int ind = find_set(elem);
            st.insert(comp[ind][0]);
            for (int j = 1; j < comp[ind].size(); ++j) {
                connect(comp[ind][j - 1], comp[ind][j]);
            }
        }
        if (!f) continue;
        if (st.size() <= 2) return 0;
        vi vec;
        for (auto elem : st) vec.pub(elem);
        for (int j = 1; j < vec.size(); ++j) {
            connect(vec[j], vec[j - 1]);
        }
        connect(vec[0], vec.back());
    }
    build(edge);
    return 1;
}

Compilation message

supertrees.cpp: In function 'll range_sum(ll, ll)':
supertrees.cpp:96:21: warning: unused variable 'cnt' [-Wunused-variable]
   96 |     ll dif = a - 1, cnt = b - a + 1;
      |                     ^~~
supertrees.cpp: In function 'll bin_to_dec(std::string)':
supertrees.cpp:119:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |     for (int i = 0; i < s.size(); i++) {
      |                     ~~^~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:231:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  231 |         for (int i1 = 0; i1 < gr[i].size(); ++i1) {
      |                          ~~~^~~~~~~~~~~~~~
supertrees.cpp:232:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  232 |             for (int i2 = i1 + 1; i2 < gr[i].size(); ++i2) {
      |                                   ~~~^~~~~~~~~~~~~~
supertrees.cpp:240:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  240 |         for (int i1 = 0; i1 < gr[i].size(); ++i1) {
      |                          ~~~^~~~~~~~~~~~~~
supertrees.cpp:241:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  241 |             for (int i2 = i1 + 1; i2 < gr[i].size(); ++i2) {
      |                                   ~~~^~~~~~~~~~~~~~
supertrees.cpp:248:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  248 |         for (int i1 = 0; i1 < gr[i].size(); ++i1) {
      |                          ~~~^~~~~~~~~~~~~~
supertrees.cpp:249:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  249 |             for (int i2 = i1 + 1; i2 < gr[i].size(); ++i2) {
      |                                   ~~~^~~~~~~~~~~~~~
supertrees.cpp:262:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  262 |             for (int j = 1; j < comp[ind].size(); ++j) {
      |                             ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:270:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  270 |         for (int j = 1; j < vec.size(); ++j) {
      |                         ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 3564 KB Output is correct
7 Correct 168 ms 26028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 3564 KB Output is correct
7 Correct 168 ms 26028 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 3420 KB Output is correct
13 Correct 183 ms 26216 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 2908 KB Output is correct
17 Correct 73 ms 16084 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 40 ms 8028 KB Output is correct
21 Correct 160 ms 26184 KB Output is correct
22 Correct 159 ms 26188 KB Output is correct
23 Correct 174 ms 26180 KB Output is correct
24 Correct 158 ms 26180 KB Output is correct
25 Correct 62 ms 16220 KB Output is correct
26 Correct 65 ms 16212 KB Output is correct
27 Correct 171 ms 25992 KB Output is correct
28 Correct 160 ms 26200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 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 344 KB Output is correct
8 Correct 7 ms 3416 KB Output is correct
9 Correct 163 ms 26180 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 3420 KB Output is correct
13 Correct 164 ms 26192 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 3164 KB Output is correct
17 Correct 69 ms 16200 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 40 ms 7976 KB Output is correct
22 Correct 157 ms 26184 KB Output is correct
23 Correct 169 ms 26268 KB Output is correct
24 Correct 161 ms 26108 KB Output is correct
25 Correct 77 ms 16288 KB Output is correct
26 Correct 62 ms 16204 KB Output is correct
27 Correct 157 ms 28180 KB Output is correct
28 Correct 176 ms 28100 KB Output is correct
29 Correct 80 ms 18248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 39 ms 8016 KB Output is correct
5 Correct 157 ms 26012 KB Output is correct
6 Correct 159 ms 26060 KB Output is correct
7 Correct 183 ms 26116 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 46 ms 8136 KB Output is correct
10 Correct 160 ms 26192 KB Output is correct
11 Correct 155 ms 26180 KB Output is correct
12 Correct 166 ms 26196 KB Output is correct
13 Correct 0 ms 352 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 41 ms 8028 KB Output is correct
17 Correct 188 ms 25992 KB Output is correct
18 Correct 181 ms 27500 KB Output is correct
19 Correct 160 ms 27480 KB Output is correct
20 Correct 177 ms 27932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 3564 KB Output is correct
7 Correct 168 ms 26028 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 3420 KB Output is correct
13 Correct 183 ms 26216 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 2908 KB Output is correct
17 Correct 73 ms 16084 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 40 ms 8028 KB Output is correct
21 Correct 160 ms 26184 KB Output is correct
22 Correct 159 ms 26188 KB Output is correct
23 Correct 174 ms 26180 KB Output is correct
24 Correct 158 ms 26180 KB Output is correct
25 Correct 62 ms 16220 KB Output is correct
26 Correct 65 ms 16212 KB Output is correct
27 Correct 171 ms 25992 KB Output is correct
28 Correct 160 ms 26200 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 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 344 KB Output is correct
36 Correct 7 ms 3416 KB Output is correct
37 Correct 163 ms 26180 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 7 ms 3420 KB Output is correct
41 Correct 164 ms 26192 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 3 ms 3164 KB Output is correct
45 Correct 69 ms 16200 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 40 ms 7976 KB Output is correct
50 Correct 157 ms 26184 KB Output is correct
51 Correct 169 ms 26268 KB Output is correct
52 Correct 161 ms 26108 KB Output is correct
53 Correct 77 ms 16288 KB Output is correct
54 Correct 62 ms 16204 KB Output is correct
55 Correct 157 ms 28180 KB Output is correct
56 Correct 176 ms 28100 KB Output is correct
57 Correct 80 ms 18248 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 4 ms 3044 KB Output is correct
61 Correct 76 ms 18260 KB Output is correct
62 Correct 1 ms 344 KB Output is correct
63 Correct 1 ms 344 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 39 ms 8540 KB Output is correct
66 Correct 83 ms 18104 KB Output is correct
67 Correct 66 ms 18248 KB Output is correct
68 Correct 60 ms 18108 KB Output is correct
69 Correct 63 ms 18116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 3564 KB Output is correct
7 Correct 168 ms 26028 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 3420 KB Output is correct
13 Correct 183 ms 26216 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 2908 KB Output is correct
17 Correct 73 ms 16084 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 40 ms 8028 KB Output is correct
21 Correct 160 ms 26184 KB Output is correct
22 Correct 159 ms 26188 KB Output is correct
23 Correct 174 ms 26180 KB Output is correct
24 Correct 158 ms 26180 KB Output is correct
25 Correct 62 ms 16220 KB Output is correct
26 Correct 65 ms 16212 KB Output is correct
27 Correct 171 ms 25992 KB Output is correct
28 Correct 160 ms 26200 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 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 344 KB Output is correct
36 Correct 7 ms 3416 KB Output is correct
37 Correct 163 ms 26180 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 7 ms 3420 KB Output is correct
41 Correct 164 ms 26192 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 3 ms 3164 KB Output is correct
45 Correct 69 ms 16200 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 40 ms 7976 KB Output is correct
50 Correct 157 ms 26184 KB Output is correct
51 Correct 169 ms 26268 KB Output is correct
52 Correct 161 ms 26108 KB Output is correct
53 Correct 77 ms 16288 KB Output is correct
54 Correct 62 ms 16204 KB Output is correct
55 Correct 157 ms 28180 KB Output is correct
56 Correct 176 ms 28100 KB Output is correct
57 Correct 80 ms 18248 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 39 ms 8016 KB Output is correct
62 Correct 157 ms 26012 KB Output is correct
63 Correct 159 ms 26060 KB Output is correct
64 Correct 183 ms 26116 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 46 ms 8136 KB Output is correct
67 Correct 160 ms 26192 KB Output is correct
68 Correct 155 ms 26180 KB Output is correct
69 Correct 166 ms 26196 KB Output is correct
70 Correct 0 ms 352 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 41 ms 8028 KB Output is correct
74 Correct 188 ms 25992 KB Output is correct
75 Correct 181 ms 27500 KB Output is correct
76 Correct 160 ms 27480 KB Output is correct
77 Correct 177 ms 27932 KB Output is correct
78 Correct 1 ms 348 KB Output is correct
79 Correct 1 ms 348 KB Output is correct
80 Correct 4 ms 3044 KB Output is correct
81 Correct 76 ms 18260 KB Output is correct
82 Correct 1 ms 344 KB Output is correct
83 Correct 1 ms 344 KB Output is correct
84 Correct 1 ms 348 KB Output is correct
85 Correct 39 ms 8540 KB Output is correct
86 Correct 83 ms 18104 KB Output is correct
87 Correct 66 ms 18248 KB Output is correct
88 Correct 60 ms 18108 KB Output is correct
89 Correct 63 ms 18116 KB Output is correct
90 Correct 1 ms 348 KB Output is correct
91 Correct 1 ms 348 KB Output is correct
92 Correct 4 ms 3164 KB Output is correct
93 Correct 71 ms 18236 KB Output is correct
94 Correct 1 ms 348 KB Output is correct
95 Correct 0 ms 348 KB Output is correct
96 Correct 1 ms 348 KB Output is correct
97 Correct 17 ms 5980 KB Output is correct
98 Correct 81 ms 18252 KB Output is correct
99 Correct 64 ms 18164 KB Output is correct
100 Correct 62 ms 18116 KB Output is correct
101 Correct 59 ms 18244 KB Output is correct
102 Correct 70 ms 18104 KB Output is correct
103 Correct 64 ms 18112 KB Output is correct
104 Correct 72 ms 18120 KB Output is correct
105 Correct 70 ms 18124 KB Output is correct