답안 #658272

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
658272 2022-11-12T18:40:26 Z Kahou Spring cleaning (CEOI20_cleaning) C++14
18 / 100
1000 ms 21860 KB
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mk make_pair
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 1e5 + 50;
int n, q, r, sub[N], mark[N], cnt[N], h[N], st[N], ft[N], t;
int par[20][N], p[N], lf[N];
int oans, ans;
vector<int> adj[N];
vector<int> vc;
vector<int> adj2[N];

void dfs(int u, int p) {
        st[u] = ++t;
        par[0][u] = p;
        h[u] = h[p]+1;
        for (int v:adj[u]) {
                if (v == p) continue;
                dfs(v, u);
                sub[u] += sub[v];
        }
        ft[u] = ++t;
}
void dfs2(int u, int p) {
        for (int v:adj[u]) {
                if (v == p) continue;
                cnt[v] = cnt[u]+1-(sub[v]&1);
                dfs2(v, u);
        }
}
void dfs3(int u, int p) {
        for (int v:adj2[u]) {
                if (v == p) continue;
                dfs3(v, u);
                lf[u] += lf[v];
                if (lf[v]&1) {
                        ans += h[v]-h[u]-2*(cnt[v]-cnt[u]);
                }
        }
}

int Par(int u, int x) {
        for (int k = 0; k < 20; k++) {
                if (x>>k&1) u = par[k][u];
        }
        return u;
}
int LCA(int u, int v) {
        if (h[u] > h[v]) swap(u, v);
        v = Par(v, h[v]-h[u]);
        if (u == v) return u;

        for (int k = 19; k >= 0; k--) {
                if (par[k][u] != par[k][v]) {
                        u = par[k][u], v = par[k][v];
                }
        }
        return par[0][u];
}
bool cmp(int u, int v) {
        return st[u] < st[v];
}

void solve() {
        cin >> n >> q;
        for (int i = 1; i <= n-1; i++) {
                int u, v;
                cin >> u >> v;
                adj[u].push_back(v);
                adj[v].push_back(u);
        }
        for (int u = 1; u <= n; u++) {
                if ((int)adj[u].size() != 1) {
                        if (!r) r = u;
                }
                else sub[u] = mark[u] = 1;
        }
        dfs(r, 0);
        dfs2(r, 0);
        for (int u = 1; u <= n; u++) {
                if (u == r) continue;
                oans += 1-(sub[u]&1);
                lf[u] = mark[u];
        }
        for (int x = 1; x <= q; x++) {
                int d;
                cin >> d;
                for (int i = 1; i <= d; i++) {
                        int u;
                        cin >> u;
                        vc.push_back(u);
                        lf[u]++;
                }

                sort(vc.begin(), vc.end(), cmp);
                vc.resize(unique(vc.begin(), vc.end())-vc.begin());
                int tmp = vc.size();

                for (int i = 1; i < tmp; i++) {
                        vc.push_back(LCA(vc[i-1], vc[i]));
                }

                sort(vc.begin(), vc.end(), cmp);
                vc.resize(unique(vc.begin(), vc.end())-vc.begin());

                for (int i = 1; i < (int)vc.size(); i++) {
                        int u = vc[i];
                        p[u] = vc[i-1];
                        while (st[u] > ft[p[u]]) p[u] = p[p[u]]; 

                        adj2[u].push_back(p[u]);
                        adj2[p[u]].push_back(u);
                }
                int r2 = vc[0];
                ans = oans;

                dfs3(r2, 0);
                cout << ((sub[r]+lf[r2])&1? -1: n+d-1+ans) << '\n';

                for (int u:vc) {
                        adj2[u].clear();
                        lf[u] = mark[u];
                }
                vc.clear();
        }
}

int main() {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        solve();
        return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Execution timed out 1087 ms 6740 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 6228 KB Output is correct
2 Correct 18 ms 6220 KB Output is correct
3 Correct 34 ms 13096 KB Output is correct
4 Correct 57 ms 13224 KB Output is correct
5 Correct 62 ms 15884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 6744 KB Output is correct
2 Correct 16 ms 6748 KB Output is correct
3 Correct 49 ms 20208 KB Output is correct
4 Correct 91 ms 21860 KB Output is correct
5 Correct 41 ms 18656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1083 ms 7484 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 10576 KB Output is correct
2 Execution timed out 1082 ms 10156 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 74 ms 13768 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Execution timed out 1087 ms 6740 KB Time limit exceeded
3 Halted 0 ms 0 KB -