Submission #945436

# Submission time Handle Problem Language Result Execution time Memory
945436 2024-03-13T19:19:01 Z galen_colin Spring cleaning (CEOI20_cleaning) C++17
100 / 100
90 ms 38712 KB
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include "bits/stdc++.h"
using namespace std;

/* 
find my code templates at https://github.com/galencolin/cp-templates
also maybe subscribe please thanks 
*/

#define send {ios_base::sync_with_stdio(false);}
#define help {cin.tie(NULL);}
#define f first
#define s second
#define getunique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}

using ll = int;
// using ll = int;
// #pragma warning("int")
//
using vl = vector<ll>;
using pl = pair<ll, ll>;

typedef long double ld;
typedef unsigned long long ull;

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds; 

template <typename num_t>
using ordered_set = tree<num_t, null_type, less<num_t>, rb_tree_tag, tree_order_statistics_node_update>;

// const string PAIR_LEFT = "(";
// const string PAIR_RIGHT = ")";
// const string IT_LEFT = "[";
// const string IT_RIGHT = "]";

const string PAIR_LEFT = "{";
const string PAIR_RIGHT = "}";
const string IT_LEFT = "{";
const string IT_RIGHT = "}";

// benq - print any container + pair
template<typename T, typename = void> struct is_iterable : false_type {};
template<typename T> struct is_iterable<T, void_t<decltype(begin(declval<T>())),decltype(end(declval<T>()))>> : true_type {};
template<typename T> typename enable_if<is_iterable<T>::value&&!is_same<T, string>::value,ostream&>::type operator<<(ostream &cout, T const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << PAIR_LEFT << p.f << ", " << p.s << PAIR_RIGHT; }
template<typename T> typename enable_if<is_iterable<T>::value&&!is_same<T, string>::value,ostream&>::type operator<<(ostream &cout, T const &v) {
    cout << IT_LEFT; 
    for (auto it = v.begin(); it != v.end();) {
        cout << *it;
        if (++it != v.end()) cout << ", ";
    }
    return cout << IT_RIGHT;
}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
    cin >> p.first;
    return cin >> p.second;
}

template<typename T> void debug(string s, T x) {cerr << "\033[1;34m" << s << "\033[0;32m = \033[35m" << x << "\033[0m\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
        if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;34m" << s.substr(0, i) << "\033[0;32m = \033[35m" << x << "\033[31m | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
template<typename T> void debug_nameless(T x) {cerr << "\033[35m" << x << "\033[0m\n";}
template<typename T, typename... Args> void debug_nameless(T x, Args... args) {cerr << "\033[35m" << x << "\033[31m | "; debug_nameless(args...);}

#ifdef galen_colin_local
#define pr(...) debug(#__VA_ARGS__, __VA_ARGS__)
#define prs(...) debug_nameless(__VA_ARGS__)
const bool local_ = true;
#else
#define pr(...) 135
#define prs(...) 135
const bool local_ = false;
#endif

mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 rng(61378913);
/* usage - just do rng() */

void usaco(string filename) {
// #pragma message("be careful, freopen may be wrong")
    freopen((filename + ".in").c_str(), "r", stdin);
    freopen((filename + ".out").c_str(), "w", stdout);
}

// #include <atcoder/all>
// using namespace atcoder;

// const ld pi = 3.14159265358979323846;
// const ll mod = 1000000007;
// const ll mod = 998244353;
// ll mod;



ll n, m, q, k, l, r, x, y, z;
const ll template_array_size = 1e6 + 206171;
ll a[template_array_size];
ll b[template_array_size];
ll c[template_array_size];
string s, t;

vl adj[100005];
vl ch[100005];
ll sz[100005];
ll top[100005];
vl at[100005];
ll lab[100005];
ll par[100005];
ll T = -1;

ll cl[100005];

ll path[100005][30];
ll psz[100005];
ll vis[100005];

ll ps[100005];

ll dsz(ll v, ll p) {
    sz[v] = 1;
    par[v] = p;
    for (ll x: adj[v]) if (x != p) {
        sz[v] += dsz(x, v);
        ch[v].push_back(x);
    }
    return sz[v];
}

void dlab(ll v, ll p) {
    lab[v] = ++T;

    top[v] = v;
    if (p != -1 && lab[p] + 1 == lab[v]) top[v] = top[p];

    for (ll i = 1; i < ch[v].size(); i++) if (sz[ch[v][i]] > sz[ch[v][0]]) swap(ch[v][0], ch[v][i]);

    for (ll x: ch[v]) dlab(x, v);
}

void dl(ll v, ll p) {
    if (ch[v].size() == 0) cl[lab[v]] = 1;

    for (ll x: ch[v]) {
        dl(x, v);
        cl[lab[v]] ^= cl[lab[x]];
    }
}

inline ll gc(ll l, ll r, ll f) {
    ll v = ps[r];
    if (l > 0) v -= ps[l - 1];
    if (!f) v = (r - l + 1) - v;
    return v;
}

ll pc[100005];

inline ll gd(ll l, ll r, ll f) {
    ll v = ps[r];
    if (l > 0) v -= ps[l - 1];
    if (!f) v = (r - l + 1) - v;
    return 2 * v - (r - l + 1);
}

const bool run = local_ ? 0 : 1;
void solve(int tc = 0) {
    cin >> n >> q;

    for (ll i = 1; i < n; i++) {
        cin >> x >> y;
        --x; --y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    ll r = 0;
    while (adj[r].size() == 1) ++r;

    dsz(r, -1);
    dlab(r, -1);
    dl(r, -1);

    for (ll i = 0; i < n; i++) {
        ll t = i;
        while (t != -1) {
            path[i][psz[i]++] = t;
            t = par[top[t]];
        }
    }

    for (ll i = 0; i < n; i++) vis[i] = -1;

    {
        ll r = 0;
        for (ll i = 0; i < n; i++) {
            r += cl[i];
            ps[i] = r;
        }
    }

    for (ll i = 0; i < n; i++) {
        ll t = lab[top[i]], l = lab[i];
        pc[l] = gd(t, l, 0);
    }

    ll ra = n - 2 + gc(0, n - 1, 0);

    for (ll i = 0; i < q; i++) {
        cin >> m;
        vl v(m);
        for (ll &x: v) cin >> x, --x;

        vl nv;
        nv.reserve(m);
        for (ll x: v) {
            if (vis[x] != i && adj[x].size() == 1) {
                vis[x] = i;
                continue;
            }
            nv.push_back(x);
        }
        v = nv;

        sort(v.begin(), v.end(), [&](ll a, ll b) {
            return lab[a] < lab[b];
        });

        nv.clear();
        for (ll x: v) {
            if (nv.size() && nv.back() == x) nv.pop_back();
            else nv.push_back(x);
        }
        v = nv;

        for (ll x: v) {
            for (ll i = 0; i < psz[x]; i++) {
                ll y = path[x][i];
                at[lab[top[y]]].push_back(lab[y]);
            }
        }

        ll ans = ra + m;

        for (ll x: v) {
            for (ll i = 0; i < psz[x]; i++) {
                ll y = path[x][i];
                ll c = lab[top[y]];
                if (at[c].size()) {
                    ll l = 0, r = at[c].size() - 1;

                    ll t = at[c].size() % 2;

                    while (l <= r) {
                        if (at[c][l] <= at[c][r]) {
                            ans += (t ? -1 : 1) * pc[at[c][l]];
                            ++l;
                        } else {
                            ans += (t ? -1 : 1) * pc[at[c][r]];
                            --r;
                        }
                        
                        t ^= 1;
                    }

                    at[c].clear();
                }
            }
        }

        if ((cl[lab[r]] + v.size()) % 2 == 1) {
            cout << -1 << '\n';
        } else {
            cout << ans << '\n';
        }
    }
}

int main() {
    #ifdef galen_colin_local
        auto begin = std::chrono::high_resolution_clock::now();
    #endif
    
    send help

    #ifndef galen_colin_local
        // usaco("cbs");
    #endif
    
    // usaco("cowland");
    
    // freopen("tc.txt", "r", stdin);
    // freopen("tc.txt", "w", stdout);
    // freopen("tc2.cpp", "w", stdout);
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
        
    cout << setprecision(15) << fixed;
    cerr << setprecision(4) << fixed;


            
    int tc = 1;
    // if (local_)
    // if (!run)
    // cin >> tc;
    for (int t = 0; t < tc; t++) {
        pr(t); prs(string(50, '-'));
        solve(t);
        prs(string(50, '-') + "\n");
    }
    
    #ifdef galen_colin_local
        auto end = std::chrono::high_resolution_clock::now();
        cerr << setprecision(4) << fixed;
        cerr << "Execution time: " << std::chrono::duration_cast<std::chrono::duration<double>>(end - begin).count() << " seconds" << endl;
    #endif
}

Compilation message

cleaning.cpp: In function 'void dlab(ll, ll)':
cleaning.cpp:141:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |     for (ll i = 1; i < ch[v].size(); i++) if (sz[ch[v][i]] > sz[ch[v][0]]) swap(ch[v][0], ch[v][i]);
      |                    ~~^~~~~~~~~~~~~~
cleaning.cpp: In function 'int main()':
cleaning.cpp:76:17: warning: statement has no effect [-Wunused-value]
   76 | #define pr(...) 135
      |                 ^~~
cleaning.cpp:313:9: note: in expansion of macro 'pr'
  313 |         pr(t); prs(string(50, '-'));
      |         ^~
cleaning.cpp:77:18: warning: statement has no effect [-Wunused-value]
   77 | #define prs(...) 135
      |                  ^~~
cleaning.cpp:313:16: note: in expansion of macro 'prs'
  313 |         pr(t); prs(string(50, '-'));
      |                ^~~
cleaning.cpp:77:18: warning: statement has no effect [-Wunused-value]
   77 | #define prs(...) 135
      |                  ^~~
cleaning.cpp:315:9: note: in expansion of macro 'prs'
  315 |         prs(string(50, '-') + "\n");
      |         ^~~
cleaning.cpp: In function 'void usaco(std::string)':
cleaning.cpp:87:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |     freopen((filename + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:88:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |     freopen((filename + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 24 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 11868 KB Output is correct
2 Correct 12 ms 11916 KB Output is correct
3 Correct 27 ms 27076 KB Output is correct
4 Correct 29 ms 24392 KB Output is correct
5 Correct 36 ms 28232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 12768 KB Output is correct
2 Correct 14 ms 12636 KB Output is correct
3 Correct 41 ms 38712 KB Output is correct
4 Correct 52 ms 38228 KB Output is correct
5 Correct 35 ms 36944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 15708 KB Output is correct
2 Correct 14 ms 14940 KB Output is correct
3 Correct 9 ms 14684 KB Output is correct
4 Correct 10 ms 15704 KB Output is correct
5 Correct 10 ms 15452 KB Output is correct
6 Correct 16 ms 15452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 24448 KB Output is correct
2 Correct 47 ms 24240 KB Output is correct
3 Correct 31 ms 18008 KB Output is correct
4 Correct 48 ms 24144 KB Output is correct
5 Correct 47 ms 24156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 29208 KB Output is correct
2 Correct 90 ms 31528 KB Output is correct
3 Correct 81 ms 32852 KB Output is correct
4 Correct 79 ms 32080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 24 ms 14940 KB Output is correct
3 Correct 12 ms 11868 KB Output is correct
4 Correct 12 ms 11916 KB Output is correct
5 Correct 27 ms 27076 KB Output is correct
6 Correct 29 ms 24392 KB Output is correct
7 Correct 36 ms 28232 KB Output is correct
8 Correct 14 ms 12768 KB Output is correct
9 Correct 14 ms 12636 KB Output is correct
10 Correct 41 ms 38712 KB Output is correct
11 Correct 52 ms 38228 KB Output is correct
12 Correct 35 ms 36944 KB Output is correct
13 Correct 21 ms 15708 KB Output is correct
14 Correct 14 ms 14940 KB Output is correct
15 Correct 9 ms 14684 KB Output is correct
16 Correct 10 ms 15704 KB Output is correct
17 Correct 10 ms 15452 KB Output is correct
18 Correct 16 ms 15452 KB Output is correct
19 Correct 49 ms 24448 KB Output is correct
20 Correct 47 ms 24240 KB Output is correct
21 Correct 31 ms 18008 KB Output is correct
22 Correct 48 ms 24144 KB Output is correct
23 Correct 47 ms 24156 KB Output is correct
24 Correct 77 ms 29208 KB Output is correct
25 Correct 90 ms 31528 KB Output is correct
26 Correct 81 ms 32852 KB Output is correct
27 Correct 79 ms 32080 KB Output is correct
28 Correct 43 ms 23844 KB Output is correct
29 Correct 59 ms 32340 KB Output is correct
30 Correct 38 ms 28244 KB Output is correct
31 Correct 50 ms 38228 KB Output is correct
32 Correct 48 ms 24032 KB Output is correct
33 Correct 51 ms 30548 KB Output is correct
34 Correct 61 ms 32852 KB Output is correct
35 Correct 60 ms 32616 KB Output is correct