Submission #737702

# Submission time Handle Problem Language Result Execution time Memory
737702 2023-05-07T14:59:10 Z Joshua_Andersson Village (BOI20_village) C++14
6 / 100
700 ms 392 KB
#undef _GLIBCXX_DEBUG                // disable run-time bound checking, etc
#pragma GCC optimize("Ofast,inline") // Ofast = O3,fast-math,allow-store-data-races,no-protect-parens
#pragma GCC optimize ("unroll-loops")

#pragma GCC target("bmi,bmi2,lzcnt,popcnt")                      // bit manipulation
#pragma GCC target("movbe")                                      // byte swap
#pragma GCC target("aes,pclmul,rdrnd")                           // encryption
#pragma GCC target("avx,avx2,f16c,fma,sse3,ssse3,sse4.1,sse4.2") // SIMD

#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;

#define enablell 1

typedef long long ll;
typedef unsigned long long ull;
#if enablell
#define int ll
#define inf int(1e18)
#define float double
#else
const int inf = int(2e9);
#endif
typedef vector<ull> vull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef pair<int, int> p2;
typedef vector<p2> vp2;
typedef vector<vp2> vvp2;
typedef vector<vvp2> vvvp2;
typedef tuple<int, int, int> p3;
typedef vector<p3> vp3;
typedef vector<vp3> vvp3;
typedef vector<vvp3> vvvp3;
typedef tuple<int, int, int, int> p4;
typedef vector<p4> vp4;

#define PBDS 0
#define _LOCAL _MSC_VER > 0
#if _LOCAL
#define gc() getchar()
#define popcount(x) __popcnt(x)
#define leading_zeros(x) _lzcnt_u32(x)
uint32_t clz(uint32_t x) { return _lzcnt_u32(x); }
uint32_t ctz(uint32_t x) { return _tzcnt_u32(x); }
#define bswap64(x) _byteswap_uint64(x)
#define assert(x) debassert(x)
#else
#define popcount(x) __builtin_popcount(x)
uint32_t clz(uint32_t x) { return __builtin_clz(x); }
uint32_t ctz(uint32_t x) { return __builtin_ctzll(x); }
#define bswap64(x) __builtin_bswap64(x)
#define gc() getchar_unlocked()
#if PBDS
using namespace __gnu_pbds;
// lower_bound is now upper_bound and vice versa (multiset). generally a bit broken
template<typename T> using indexed_multiset = tree<int, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct chash { // large odd number for C
    const uint64_t C = ll(4e18 * acos(0)) | 71;
    ll operator()(ll x) const { return __builtin_bswap64(x * C); }
};

template<typename T, typename U> using fast_map = __gnu_pbds::gp_hash_table<T, U, chash>;
template<typename T> using fast_set = __gnu_pbds::gp_hash_table<T, null_type, chash>;
template<typename T, typename H> using fast_set_h = __gnu_pbds::gp_hash_table<T, null_type, H>;
#endif

#endif

#define FAST_INPUT 0
#define FILE_TC 0
#if FILE_TC && _LOCAL
//ifstream filein("E:\\desktop\\code\\repos\\Comp prog\\x64\\Debug\\in.txt");
ifstream filein("C:\\users\\matis\\source\\repos\\Comp Prog\\x64\\Debug\\1.in");
//ifstream filein("E:\\downloads\\test_data\\test_data\\005-case05.in");
//ifstream filein("E:\\desktop\\po-repos\\swedish-olympiad-2023\\online\\tomtplanering\\data\\secret\\group10\\010-case10.in");

#define cin filein
void fast() {}
#else
inline void fast() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
#endif

#if FAST_INPUT && !FILE_TC
inline void read(int& v) { v = 0; int sign = 1; char c = gc(); if (c == '-') { sign = -1; } else { v += c - '0'; } while ((c = gc()) && c != ' ' && c != '\n') { if (c == EOF) { v = -1; return; } v *= 10; v += c - '0'; } v *= sign; }
inline void read(int& u, int& v) { read(u); read(v); }
inline void read(int& u, int& v, int& k) { read(u); read(v); read(k); }
//inline void read(int& v) { char c; while ((c = getchar()) != EOF && c != ' ' && c != '\n') { v *= 10; v += c - '0'; } }
inline void read(string& s) { char c; while ((c = gc()) != EOF && c != '\n' && c != ' ') { s.push_back(c); } }
inline void readline(string& s) { char c; while ((c = gc()) != EOF && c != '\n') { s.push_back(c); } }
#else
template <typename T> inline void read(T& a) { cin >> a; }
template <typename T> inline void read(T& a, T& b) { cin >> a >> b; }
template <typename T> inline void read(T& a, T& b, T& c) { cin >> a >> b >> c; }
#endif
#define quit cout << endl; _Exit(0);
#define dread(type, a) type a; read(a)
#define dread2(type, a, b) dread(type, a); dread(type, b)
#define dread3(type, a, b, c) dread2(type, a, b); dread(type, c)
#define dread4(type, a, b, c, d) dread3(type, a, b, c); dread(type, d)
#define dread5(type, a, b, c, d, e) dread4(type, a, b, c, d); dread(type, e)
#define readvector(type, name, size) vector<type> name(size); rep(i,size) {dread(type,temp); name[i]=temp;}
#ifdef _DEBUG
#define noop cout << "";
#define deb __debugbreak();
#define debassert(expr) if(!(expr)) deb;
#define debif(expr) if(expr) deb;
#else
#define noop ;
#define deb ;
#define debif(expr) ;
#define debassert(expr) ;
#endif

#define rep(i, high) for (int i = 0; i < high; i++)
#define repp(i, low, high) for (int i = low; i < high; i++)
#define repe(i, container) for (auto& i : container)
#define per(i, high) for (int i = high-1; i >= 0; i--)
#define perr(i, low, high) for (int i = high-1; i >= low; i--)

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define setcontains(set, x) (set.find(x) != set.end())
#define within(a, b, c, d) (a >= 0 && a < b && c >= 0 && c < d)
#define sz(container) ((int)container.size())
#define mp(a,b) (make_pair(a,b))

#define ceildiv(x,y) ((x + y - 1) / y)

template <typename T, typename U> inline void operator+=(pair<T, U>& l, const pair<T, U>& r) { l = { l.first + r.first,l.second + r.second }; }
template <typename T, typename U> inline pair<T, U> operator+(const pair<T, U> l, const pair<T, U> r) { return { l.first + r.first, l.second + r.second }; }
template <typename T, typename U> inline pair<T, U> operator-(const pair<T, U> l, const pair<T, U> r) { return { l.first - r.first, l.second - r.second }; }
template <typename T, typename U> inline pair<T, U> operator*(const pair<T, U> l, const int m) { return { l.first * m, l.second * m }; }
template <typename Out> inline void split(const string& s, char delim, Out result) { istringstream iss(s); string item; while (getline(iss, item, delim)) { *result++ = item; } }
inline vector<string> split(const string& s, char delim) { vector<string> elems; split(s, delim, back_inserter(elems)); return elems; }
vector<string> split(string s, string d) { size_t k = 0, n, e = d.length(); string t; vector<string> r; while ((n = s.find(d, k)) != string::npos) { t = s.substr(k, n - k); k = n + e; r.push_back(t); } r.push_back(s.substr(k)); return r; }
ll binpow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; }
ll binpow(ll a, ll b, ll m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } // For a < 2^31

#if 1
auto Start = chrono::high_resolution_clock::now();
void resettimer() { Start = chrono::high_resolution_clock::now(); }
int elapsedmillis() { return chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - Start).count(); }
random_device rd;
mt19937 rng(rd());
template<typename T, typename U> inline int randint(T lo, U hi) { return uniform_int_distribution<int>((int)lo, (int)hi)(rng); } // [lo,hi]
template<typename T> inline T randel(vector<T>& v) { return v[uniform_int_distribution<int>(int(0), int(v.size()) - int(1))(rng)]; } // [lo,hi]
#endif
const ll mod = 1e9 + 7;
vp2 dirs = { {0,1},{0,-1},{1,0},{-1,0} };

int dist(int u, int t, int p, vvi& edges)
{
    if (u == t) return 0;

    repe(e, edges[u])
    {
        if (e == p) continue;
        int k = dist(e, t, u, edges);
        if (k != inf) return k + 1;
    }

    return inf;
}

p2 solvesmarter(vvi& edges, vi& largest, vi& smallest)
{
    int n = edges.size();
    vi deg(n);
    rep(i, n) deg[i] = edges[i].size();
    int taken = 0;

    auto findroot = [&]()
    {
        int best = 0;
        rep(i, n)
        {
            if (deg[i] > deg[best]) best = i;
        }

        return best;
    };

    vector<set<int>> realedges(n);
    rep(i, n) repe(e, edges[i]) realedges[i].insert(e);
    vi leafs;
    rep(i, n) if (edges[i].size() == 1) leafs.push_back(i);
    int ans = 0;
    int t = n;
    while (leafs.size())
    {
        if (t == 3) break;
        t -= 2;
        p2 bestp = p2(-1, -1);
        int bestv = -1;
        rep(i, leafs.size())
        {
            rep(j, leafs.size())
            {
                int d = dist(leafs[i], leafs[j], leafs[i], edges);
                if (d>bestv)
                {
                    bestv = d;
                    bestp = { i,j };
                }
            }
        }

        ans += bestv*2;
        int a, b;
        tie(a, b) = bestp;
        a = leafs[a];
        b = leafs[b];
        largest[a] = b;
        largest[b] = a;
        set<int>& s = realedges[*begin(realedges[a])];
        s.erase(a);
        if (s.size() == 1)
        {
            leafs.push_back(*begin(realedges[a]));
        }
        
        if (realedges[b].size())
        {
            set<int>& s2 = realedges[*begin(realedges[b])];
            s2.erase(b);
            if (s2.size() == 1)
            {
                leafs.push_back(*begin(realedges[b]));
            }
        }
       

        vi newleafs;
        rep(i, leafs.size())
        {
            if (i != bestp.first && i != bestp.second) newleafs.push_back(leafs[i]);
        }
        leafs = newleafs;
    }

    if (t==3)
    {
        ans += 4;
        int m = *begin(realedges[leafs[0]]);

        largest[m] = leafs[0];
        largest[leafs[0]] = leafs[1];
        largest[leafs[1]] = m;
    }

    return { ans,0};
}

void finddepth(int u, int p, int d, vector<set<int>>& edges, vi& depth)
{
    depth[u] = d;
    repe(e, edges[u]) if (e != p) finddepth(e, u, d + 1, edges, depth);
}

p2 solvesmartest(vvi& edges, vi& largest, vi& smallest)
{
    int n = edges.size();

    int taken = 0;

    vector<set<int>> realedges(n);
    auto findroot = [&]()
    {
        int best = 0;
        rep(i, n)
        {
            if (realedges[i].size() > realedges[best].size()) best = i;
        }

        return best;
    };

    rep(i, n) repe(e, edges[i]) realedges[i].insert(e);

    priority_queue<p2> pq;

    int ans = 0;
    int t = n;
    int root = -1;
    vi depth(n, inf);
    while (t)
    {
        if (t == 3) break;
        t -= 2;

        if (root==-1||realedges[root].size()==1)
        {
            root = findroot();
            priority_queue<p2> newpq;
            finddepth(root, root, 0, realedges, depth);
            while (pq.size())
            {
                p2 a = pq.top();
                pq.pop();
                newpq.emplace(depth[a.second], a.second);
            }
            pq = newpq;
        }

        p2 a = pq.top();
        pq.pop();
        p2 b = pq.top();
        pq.pop();

        ans += (a.first+b.first)*2;
        
        largest[a.second] = b.second;
        largest[b.second] = a.second;
        set<int>& s = realedges[*begin(realedges[a.second])];
        s.erase(a.second);
        if (s.size() == 1)
        {
            int l = *begin(realedges[a.second]);
            pq.emplace(depth[l], l);
        }
        
        if (realedges[b.second].size())
        {
            set<int>& s2 = realedges[*begin(realedges[b.second])];
            s2.erase(b.second);
            if (s2.size() == 1)
            {
                int l = *begin(realedges[b.second]);
                pq.emplace(depth[l], l);
            }
        }
    }

    if (t==3)
    {
        ans += 4;
        int a = pq.top().second;
        pq.pop();
        int b = pq.top().second;
        int m = *begin(realedges[a]);

        largest[m] = a;
        largest[a] = b;
        largest[b] = m;
    }

    return { ans,0};
}



p2 solvestupid(vvi& edges)
{
    int n = edges.size();

    vi perm;
    rep(i, n) perm.emplace_back(i);

    p2 ans = p2(0, inf);
    do
    {
        int c = 0;
        bool ok = 1;
        rep(i, n) ok &= perm[i] != i;
        if (!ok) continue;
        rep(i, n) c += dist(i, perm[i], i, edges);
        ans.first = max(ans.first, c);
        ans.second = min(ans.second, c);
    } while (next_permutation(all(perm)));
    return ans;
}

struct UF
{
    vi par;
    UF(int n) : par(n)
    {
        rep(i, n) par[i] = i;
    }

    int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }

    void merge(int a, int b)
    {
        a = find(a); b = find(b);
        if (a == b) return;
        par[a] = b;
    }
};

int32_t main()
{
    fast();

#if 1
    dread(int, n);
    vvi edges(n);

    rep(i, n - 1)
    {
        dread2(int, a, b);
        a--; b--;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }

    vi largest(n, 1);
    vi smallest(n, 1);
    p2 ans = solvesmarter(edges, largest, smallest);

    cout << ans.second << " " << ans.first << "\n";
    repe(e, smallest)
    {
        cout << e+1 << " ";
    }
    cout << "\n";
    repe(e, largest)
    {
        cout << e+1 << " ";
    }
    cout << "\n";

#else

    rep(i, 10000)
    {
        int n = randint(2, 6);
        //if (n % 2 == 1) continue;

        UF uf(n);
        vvi edges(n);
        rep(i, n - 1)
        {
            while (true)
            {
                int a = randint(0, n - 1);
                int b = randint(0, n - 1);
                if (uf.find(a)!=uf.find(b))
                {
                    edges[a].push_back(b);
                    edges[b].push_back(a);
                    uf.merge(a, b);
                    break;
                }
            }
        }

        p2 a = solvestupid(edges);
        vi ig1(n), ig2(n);
        p2 b = solvesmarter(edges, ig1, ig2);
        if (a.first!=b.first)
        {
            deb;
        }
        cout << i << "\n";
    }

#endif

    quit;
}

Compilation message

Village.cpp: In function 'p2 solvesmarter(vvi&, vi&, vi&)':
Village.cpp:122:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 | #define rep(i, high) for (int i = 0; i < high; i++)
......
  203 |         rep(i, leafs.size())
      |             ~~~~~~~~~~~~~~~             
Village.cpp:203:9: note: in expansion of macro 'rep'
  203 |         rep(i, leafs.size())
      |         ^~~
Village.cpp:122:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 | #define rep(i, high) for (int i = 0; i < high; i++)
......
  205 |             rep(j, leafs.size())
      |                 ~~~~~~~~~~~~~~~         
Village.cpp:205:13: note: in expansion of macro 'rep'
  205 |             rep(j, leafs.size())
      |             ^~~
Village.cpp:122:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 | #define rep(i, high) for (int i = 0; i < high; i++)
......
  242 |         rep(i, leafs.size())
      |             ~~~~~~~~~~~~~~~             
Village.cpp:242:9: note: in expansion of macro 'rep'
  242 |         rep(i, leafs.size())
      |         ^~~
Village.cpp:178:9: warning: unused variable 'taken' [-Wunused-variable]
  178 |     int taken = 0;
      |         ^~~~~
Village.cpp:180:10: warning: variable 'findroot' set but not used [-Wunused-but-set-variable]
  180 |     auto findroot = [&]()
      |          ^~~~~~~~
Village.cpp: In function 'p2 solvesmartest(vvi&, vi&, vi&)':
Village.cpp:272:9: warning: unused variable 'taken' [-Wunused-variable]
  272 |     int taken = 0;
      |         ^~~~~
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 212 KB Partially correct
2 Partially correct 1 ms 212 KB Partially correct
3 Partially correct 1 ms 212 KB Partially correct
4 Partially correct 1 ms 212 KB Partially correct
5 Partially correct 1 ms 336 KB Partially correct
6 Partially correct 1 ms 212 KB Partially correct
7 Partially correct 1 ms 212 KB Partially correct
8 Partially correct 1 ms 212 KB Partially correct
9 Partially correct 1 ms 336 KB Partially correct
10 Partially correct 0 ms 212 KB Partially correct
11 Partially correct 1 ms 212 KB Partially correct
12 Partially correct 0 ms 212 KB Partially correct
13 Partially correct 1 ms 332 KB Partially correct
14 Partially correct 1 ms 212 KB Partially correct
15 Partially correct 1 ms 212 KB Partially correct
16 Partially correct 1 ms 212 KB Partially correct
17 Partially correct 1 ms 336 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 671 ms 392 KB Partially correct
2 Execution timed out 1052 ms 340 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 212 KB Partially correct
2 Partially correct 1 ms 212 KB Partially correct
3 Partially correct 1 ms 212 KB Partially correct
4 Partially correct 1 ms 212 KB Partially correct
5 Partially correct 1 ms 336 KB Partially correct
6 Partially correct 1 ms 212 KB Partially correct
7 Partially correct 1 ms 212 KB Partially correct
8 Partially correct 1 ms 212 KB Partially correct
9 Partially correct 1 ms 336 KB Partially correct
10 Partially correct 0 ms 212 KB Partially correct
11 Partially correct 1 ms 212 KB Partially correct
12 Partially correct 0 ms 212 KB Partially correct
13 Partially correct 1 ms 332 KB Partially correct
14 Partially correct 1 ms 212 KB Partially correct
15 Partially correct 1 ms 212 KB Partially correct
16 Partially correct 1 ms 212 KB Partially correct
17 Partially correct 1 ms 336 KB Partially correct
18 Partially correct 671 ms 392 KB Partially correct
19 Execution timed out 1052 ms 340 KB Time limit exceeded
20 Halted 0 ms 0 KB -