Submission #963852

#TimeUsernameProblemLanguageResultExecution timeMemory
963852Mher777슈퍼트리 잇기 (IOI20_supertrees)C++17
100 / 100
188 ms28180 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...