답안 #945416

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
945416 2024-03-13T18:40:49 Z galen_colin Spring cleaning (CEOI20_cleaning) C++17
100 / 100
103 ms 36816 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 = long long;
// 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];

vl path[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];

    sort(ch[v].begin(), ch[v].end(), [&](ll a, ll b) {
        return sz[a] > sz[b];
    });

    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]];
    }
}

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;
}

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].push_back(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 < q; i++) {
        cin >> m;
        vl v(m);
        for (ll &x: v) cin >> x, --x;

        vl nv;
        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];
        });

        for (ll x: v) {
            for (ll y: path[x]) {
                at[lab[top[y]]].push_back(lab[y]);
            }
        }

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

        for (ll x: v) {
            for (ll y: path[x]) {
                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) {
                        ll p = l;
                        if (at[c][l] <= at[c][r]) {
                            p = l;
                            ++l;
                        } else {
                            p = r;
                            --r;
                        }

                        ans -= gc(c, at[c][p], t ^ 1);
                        ans += gc(c, at[c][p], t);
                        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 'int main()':
cleaning.cpp:76:17: warning: statement has no effect [-Wunused-value]
   76 | #define pr(...) 135
      |                 ^~~
cleaning.cpp:291:9: note: in expansion of macro 'pr'
  291 |         pr(t); prs(string(50, '-'));
      |         ^~
cleaning.cpp:77:18: warning: statement has no effect [-Wunused-value]
   77 | #define prs(...) 135
      |                  ^~~
cleaning.cpp:291:16: note: in expansion of macro 'prs'
  291 |         pr(t); prs(string(50, '-'));
      |                ^~~
cleaning.cpp:77:18: warning: statement has no effect [-Wunused-value]
   77 | #define prs(...) 135
      |                  ^~~
cleaning.cpp:293:9: note: in expansion of macro 'prs'
  293 |         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);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 27 ms 20456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 20436 KB Output is correct
2 Correct 18 ms 20552 KB Output is correct
3 Correct 36 ms 25672 KB Output is correct
4 Correct 40 ms 25672 KB Output is correct
5 Correct 51 ms 27976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 20944 KB Output is correct
2 Correct 19 ms 21456 KB Output is correct
3 Correct 49 ms 35132 KB Output is correct
4 Correct 64 ms 36816 KB Output is correct
5 Correct 38 ms 33364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 20272 KB Output is correct
2 Correct 16 ms 19804 KB Output is correct
3 Correct 12 ms 19548 KB Output is correct
4 Correct 13 ms 19548 KB Output is correct
5 Correct 12 ms 19548 KB Output is correct
6 Correct 18 ms 20060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 29264 KB Output is correct
2 Correct 66 ms 29268 KB Output is correct
3 Correct 51 ms 23264 KB Output is correct
4 Correct 74 ms 29308 KB Output is correct
5 Correct 65 ms 29188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 32340 KB Output is correct
2 Correct 83 ms 31060 KB Output is correct
3 Correct 85 ms 32088 KB Output is correct
4 Correct 103 ms 31720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 27 ms 20456 KB Output is correct
3 Correct 17 ms 20436 KB Output is correct
4 Correct 18 ms 20552 KB Output is correct
5 Correct 36 ms 25672 KB Output is correct
6 Correct 40 ms 25672 KB Output is correct
7 Correct 51 ms 27976 KB Output is correct
8 Correct 19 ms 20944 KB Output is correct
9 Correct 19 ms 21456 KB Output is correct
10 Correct 49 ms 35132 KB Output is correct
11 Correct 64 ms 36816 KB Output is correct
12 Correct 38 ms 33364 KB Output is correct
13 Correct 23 ms 20272 KB Output is correct
14 Correct 16 ms 19804 KB Output is correct
15 Correct 12 ms 19548 KB Output is correct
16 Correct 13 ms 19548 KB Output is correct
17 Correct 12 ms 19548 KB Output is correct
18 Correct 18 ms 20060 KB Output is correct
19 Correct 62 ms 29264 KB Output is correct
20 Correct 66 ms 29268 KB Output is correct
21 Correct 51 ms 23264 KB Output is correct
22 Correct 74 ms 29308 KB Output is correct
23 Correct 65 ms 29188 KB Output is correct
24 Correct 91 ms 32340 KB Output is correct
25 Correct 83 ms 31060 KB Output is correct
26 Correct 85 ms 32088 KB Output is correct
27 Correct 103 ms 31720 KB Output is correct
28 Correct 50 ms 25940 KB Output is correct
29 Correct 70 ms 31316 KB Output is correct
30 Correct 64 ms 27976 KB Output is correct
31 Correct 61 ms 36788 KB Output is correct
32 Correct 63 ms 29152 KB Output is correct
33 Correct 58 ms 29008 KB Output is correct
34 Correct 75 ms 31828 KB Output is correct
35 Correct 75 ms 31620 KB Output is correct