답안 #1096963

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1096963 2024-10-05T15:07:30 Z ShaShi Spring cleaning (CEOI20_cleaning) C++17
18 / 100
408 ms 76904 KB
#include <bits/stdc++.h>
 
#define int long long 
 
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
 
#define F first 
#define S second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define kill(x) cout << x << "\n", exit(0);
#define pii pair<int, int>
#define pll pair<long long, long long>
#define endl "\n"
 
 
 
using namespace std;
typedef long long ll;
// typedef __int128_t lll;
typedef long double ld;
 
 
const int MAXN = (int)1e6 + 7;
const int MOD = (int)1e9 + 7;
const ll INF = (ll)1e9 + 7;
 
 
int n, m, k, tmp, t, tmp2, tmp3, tmp4, u, v, w, flag, q, ans, N, X, Y;
int arr[MAXN], h[MAXN], par[MAXN], sar[MAXN], deg[MAXN], st[MAXN], ft[MAXN], sz[MAXN], R, barg, cnt[MAXN], ind[MAXN];
vector<int> adj[MAXN], vec, old_barg;
vector<pii> child[MAXN];


/* Segment Tree */
#define mid ((l+r)>>1)
#define lid (id<<1)
#define rid (lid|1)



pii seg[MAXN<<2];
bool lazy[MAXN<<2];


inline pii merge(pii x, pii y) { return {x.F+y.F, x.S+y.S}; }

inline void relax(int l, int r, int id) {
    if (!lazy[id]) return;

    swap(seg[lid].F, seg[lid].S); lazy[lid] ^= 1;
    swap(seg[rid].F, seg[rid].S); lazy[rid] ^= 1;

    lazy[id] = 0;
}


void build(int l=0, int r=n, int id=1) {
    if (l+1 == r) {
        if (cnt[ind[l]]&1) seg[id].S++;
        else seg[id].F++;

        return;
    }

    build(l, mid, lid); build(mid, r, rid);

    seg[id] = merge(seg[lid], seg[rid]);
}


void upd(int s, int t, int l=0, int r=n, int id=1) {
    if (s >= t) return;

    if (s <= l && t >= r) {
        swap(seg[id].F, seg[id].S); lazy[id] ^= 1;
        return;
    }

    relax(l, r, id);

    if (s < mid) upd(s, t, l, mid, lid);
    if (t > mid) upd(s, t, mid, r, rid);

    seg[id] = merge(seg[lid], seg[rid]);
}

/* Segment Tree */
 

void DFSsz(int v) {
    ind[flag] = v; st[v] = flag++; sz[v] = 1; cnt[v] = (deg[v] == 1);

    for (int u:adj[v]) {
        if (u == par[v]) continue;

        par[u] = v; h[u] = h[v]+1;
        
        DFSsz(u); 
        
        sz[v] += sz[u]; child[v].pb({sz[u], u}); cnt[v] += cnt[u];
    }

    ft[v] = flag;
}


void DFS(int v, int b) {
    sar[v] = b; sort(all(child[v]), greater<pii>());

    for (int i=0; i<child[v].size(); i++) {
        int u = child[v][i].S;

        if (!i) DFS(u, b);
        else DFS(u, u);
    }
}


inline void delta_deg(int v, int k) {
    barg -= (deg[v] == 1);
    deg[v] += k;
    barg += (deg[v] == 1);
}


inline void root_path(int u) {
    while (u) {
        int v = sar[u];
        upd(st[v], st[u]+1);
        u = par[v];
    }
}


int32_t main() {
    #ifdef LOCAL
    freopen("inp.in", "r", stdin);
    freopen("res.out", "w", stdout);
    #else
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #endif

    cin >> n >> q;

    for (int i=1; i<n; i++) {
        cin >> u >> v;

        adj[u].pb(v); adj[v].pb(u); delta_deg(u, 1); delta_deg(v, 1);
    }

    for (int i=1; i<=n; i++) if (deg[i] > 1) R = i;

    // cout << "! " << R << endl;

    DFSsz(R); DFS(R, R); build();

    while (q--) {
        cin >> k; barg += k;

        for (int i=0; i<k; i++) {
            int v; cin >> v;

            if (deg[v] == 1) old_barg.pb(v), root_path(v);

            vec.pb(v); delta_deg(v, 1); root_path(v);
        }

        // cout << "@ " << barg << endl;
        // cout << n+k-1 << " " << seg[1].F << " " << !(barg&1) << endl;

        if (barg&1) cout << -1 << endl;
        else cout << (n+k-1) + seg[1].F - !(barg&1) << endl;

        barg -= k;
        for (int v:vec) delta_deg(v, -1), root_path(v);
        for (int v:old_barg) root_path(v);
        vec.clear(); old_barg.clear();
    }

    return 0;
}

Compilation message

cleaning.cpp: In function 'void DFS(long long int, long long int)':
cleaning.cpp:114:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |     for (int i=0; i<child[v].size(); i++) {
      |                   ~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 47452 KB Output is correct
2 Incorrect 204 ms 52052 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 49348 KB Output is correct
2 Correct 52 ms 49352 KB Output is correct
3 Correct 67 ms 65096 KB Output is correct
4 Correct 131 ms 63808 KB Output is correct
5 Correct 151 ms 68028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 50376 KB Output is correct
2 Correct 46 ms 50388 KB Output is correct
3 Correct 71 ms 76904 KB Output is correct
4 Correct 111 ms 76488 KB Output is correct
5 Correct 62 ms 74504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 100 ms 52564 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 223 ms 60092 KB Output is correct
2 Incorrect 408 ms 59988 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 331 ms 67408 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 47452 KB Output is correct
2 Incorrect 204 ms 52052 KB Output isn't correct
3 Halted 0 ms 0 KB -