Submission #437513

# Submission time Handle Problem Language Result Execution time Memory
437513 2021-06-26T12:07:59 Z beep_boop Connecting Supertrees (IOI20_supertrees) C++17
11 / 100
235 ms 26108 KB
/*
 * Created at 4:30 PM on 26 Jun, 2021
 */

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")

#include <iostream>
#include <cmath>
#include <utility>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <numeric>
#include <cassert>

using namespace std;

#define rep(i, a, b) for(auto (i)=a;(i)<(b);(i)++)
#define list(i, N) for(auto (i)=0;(i)<(N);(i)++)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define SZ(x) (int)(x).size()
#define vt vector
#define error(x) cout << #x << " = " << (x) << '\n'
#define trav(a, x) for(auto& (a): (x))
#define UNIQUE(x) (x).resize(distance((x).begin(),unique(ALL(x))))
#define DO if(true)

//typedef long long ll;
//typedef vector<ll> vi;
//typedef pair<ll,ll> pi;
#define mp make_pair
#define pb push_back
#define eb emplace_back

//#define int ll

typedef vector<int> vi;
typedef pair<int, int> pi;

//INF value
#ifndef int
#define INF int(2e9) + 15
#else
#define INF int(1e18) + 15
#endif

//All four are primes
#define mod 1000000007
#define hash_mod 23333333377
#define MT 1004006003999
#define hash_mod2 911382323

void setIO(const string &name = "") {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    if (name.length()) {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}

template<typename T>
void read(vector<T> &a) {
    for (auto &x: a) cin >> x;
}

template<typename T>
void read(vector<T> &a, int n) {
    a.resize(n);
    for (auto &x: a) cin >> x;
}

template<class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &v) {
    out << "(";
    out << v.first << "," << v.second;
    return out << ")";
}

template<class T>
ostream &operator<<(ostream &out, const vector<T> &v) {
    out << "[";
    list(i, SZ(v)) {
        if (i) out << ", ";
        out << v[i];
    }
    return out << "]";
}

template<typename T>
void print(vector<T> &a) {
    for (const auto &x: a) cout << x << ' ';
    cout << '\n';
}

void MOD(int &x, int m = mod) {
    x %= m;
    if (x < 0) x += m;
}

void MOD(int64_t &x, int m = mod) {
    x %= m;
    if (x < 0) x += m;
}

#define trace(...) dbg(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void dbg(const char *name, T &&arg1) {
    cout << name << " : " << arg1 << '\n';
}

template<typename T, typename... U>
void dbg(const char *names, T &&arg1, U &&... args) {
    const char *comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " : " << arg1 << " | ";
    dbg(comma + 1, args...);
}

template<class T>
void read(T &x) {
    cin >> x;
}

template<class T, class... U>
void read(T &t, U &... u) {
    read(t);
    read(u...);
}

int gcd(int a, int b) { return !a ? b : gcd(b % a, a); }

void build(vt<vi> b);
//void build(vt<vi> b){cout << b << '\n';}

struct DSU {
    int size = 1;
    vi link, sz;

    explicit DSU(int n){
        size = n;
        link.resize(n);
        iota(ALL(link), 0);
        sz.assign(n, 1);
    }

    int find(int x){
        while (x != link[x]){
            x = link[x];
        }
        return x;
    }

    int same(int x, int y){
        return find(x) == find(y);
    }

    void unite(int x, int y){
        x = find(x), y = find(y);

        if(x == y){
            return;
        }

        if(sz[x] < sz[y]){
            swap(x, y);
        }

        sz[x] += sz[y];
        link[y] = x;
    }
};

using ll = int64_t;

#define N 1010
int n;
vt<vi> p;
vt<bool> has2;

int construct(vt<vi> P){
    DO {
        p = P;
        n = SZ(p);
    }

    DSU dsu(n);

    has2.assign(n, false);

    list(i, n){
        rep(j, i + 1, n){
            if(p[i][j]){
                dsu.unite(i, j);
            }
        }
        list(j, n){
            if(p[i][j] == 2){
                has2[i] = true;
            }
        }
    }

    vt<vi> comp(n);
    list(i, n){
        comp[dsu.find(i)].eb(i);
    }

    vt<vi> ans(n, vi(n, 0));
    vi way(n, 1);

    int x, y;
    list(i, n){
        if(SZ(comp[i]) <= 1) continue;

        sort(ALL(comp[i]), [](const int& lhs, const int& rhs){
            if(has2[lhs] == has2[rhs] and has2[lhs]){
                return p[0][lhs] < p[0][rhs];
            }
            return has2[lhs] < has2[rhs];
        });

        rep(j, 1, SZ(comp[i])){
            x = comp[i][j];
            y = comp[i][j - 1];

            ans[x][y] = 1;
            ans[y][x] = 1;
        }

        pi join = {-1, -1};
        list(j, SZ(comp[i])){
            if(p[comp[i][j]][comp[i].back()] == 2){
                join = {comp[i][j], comp[i].back()};
                break;
            }
        }

        if(join.first != -1){
            way[i] = 2;
            ans[join.first][join.second] = 1;
            ans[join.second][join.first] = 1;
        }
    }

    bool possible = true;
    list(i, n){
        if(SZ(comp[i]) <= 1) continue;

//        list(j, SZ(comp[i])){
//            rep(k, j + 1, SZ(comp[i])){
//                possible &= (p[comp[i][j]][comp[i][k]] == way[i]);
//            }
//        }

//        if(way[i] == 2){
//            possible &= (SZ(comp[i]) > 2);
//        }
    }

    if(!possible){
        return 0;
    }

    build(ans);
    return 1;
}

//int main(){
//    cout << construct(vt<vi>{vi{1, 0}, vi{0, 1}}) << '\n';
//}

Compilation message

supertrees.cpp: In function 'std::ostream& operator<<(std::ostream&, const std::vector<T>&)':
supertrees.cpp:23:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   23 | #define list(i, N) for(auto (i)=0;(i)<(N);(i)++)
      |                             ^
supertrees.cpp:90:5: note: in expansion of macro 'list'
   90 |     list(i, SZ(v)) {
      |     ^~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:23:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   23 | #define list(i, N) for(auto (i)=0;(i)<(N);(i)++)
      |                             ^
supertrees.cpp:198:5: note: in expansion of macro 'list'
  198 |     list(i, n){
      |     ^~~~
supertrees.cpp:22:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   22 | #define rep(i, a, b) for(auto (i)=a;(i)<(b);(i)++)
      |                               ^
supertrees.cpp:199:9: note: in expansion of macro 'rep'
  199 |         rep(j, i + 1, n){
      |         ^~~
supertrees.cpp:23:29: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   23 | #define list(i, N) for(auto (i)=0;(i)<(N);(i)++)
      |                             ^
supertrees.cpp:204:9: note: in expansion of macro 'list'
  204 |         list(j, n){
      |         ^~~~
supertrees.cpp:23:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   23 | #define list(i, N) for(auto (i)=0;(i)<(N);(i)++)
      |                             ^
supertrees.cpp:212:5: note: in expansion of macro 'list'
  212 |     list(i, n){
      |     ^~~~
supertrees.cpp:23:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   23 | #define list(i, N) for(auto (i)=0;(i)<(N);(i)++)
      |                             ^
supertrees.cpp:220:5: note: in expansion of macro 'list'
  220 |     list(i, n){
      |     ^~~~
supertrees.cpp:22:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   22 | #define rep(i, a, b) for(auto (i)=a;(i)<(b);(i)++)
      |                               ^
supertrees.cpp:230:9: note: in expansion of macro 'rep'
  230 |         rep(j, 1, SZ(comp[i])){
      |         ^~~
supertrees.cpp:23:29: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   23 | #define list(i, N) for(auto (i)=0;(i)<(N);(i)++)
      |                             ^
supertrees.cpp:239:9: note: in expansion of macro 'list'
  239 |         list(j, SZ(comp[i])){
      |         ^~~~
supertrees.cpp:23:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   23 | #define list(i, N) for(auto (i)=0;(i)<(N);(i)++)
      |                             ^
supertrees.cpp:254:5: note: in expansion of macro 'list'
  254 |     list(i, n){
      |     ^~~~
supertrees.cpp: In function 'void setIO(const string&)':
supertrees.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         freopen((name + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1356 KB Output is correct
7 Correct 227 ms 25976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1356 KB Output is correct
7 Correct 227 ms 25976 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 10 ms 1356 KB Output is correct
13 Correct 228 ms 26036 KB Output is correct
14 Incorrect 1 ms 204 KB Answer gives possible 1 while actual possible 0
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Incorrect 1 ms 204 KB Answer gives possible 1 while actual possible 0
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 61 ms 6788 KB Output is correct
5 Correct 227 ms 25988 KB Output is correct
6 Correct 223 ms 25924 KB Output is correct
7 Correct 230 ms 25984 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 57 ms 6704 KB Output is correct
10 Correct 227 ms 26108 KB Output is correct
11 Correct 235 ms 26052 KB Output is correct
12 Correct 234 ms 25988 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Incorrect 1 ms 204 KB Too many ways to get from 0 to 4, should be 1 found no less than 2
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1356 KB Output is correct
7 Correct 227 ms 25976 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 10 ms 1356 KB Output is correct
13 Correct 228 ms 26036 KB Output is correct
14 Incorrect 1 ms 204 KB Answer gives possible 1 while actual possible 0
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1356 KB Output is correct
7 Correct 227 ms 25976 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 10 ms 1356 KB Output is correct
13 Correct 228 ms 26036 KB Output is correct
14 Incorrect 1 ms 204 KB Answer gives possible 1 while actual possible 0
15 Halted 0 ms 0 KB -