답안 #949292

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949292 2024-03-19T05:08:02 Z GrindMachine Spring cleaning (CEOI20_cleaning) C++17
34 / 100
1000 ms 22276 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*



*/

const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

vector<ll> adj[N];
vector<ll> depth(N);
vector<ll> leaves(N);

ll dfs1(ll u, ll p){
    leaves[u] = 0;

    ll children = 0;
    ll cost = 0;

    trav(v,adj[u]){
        if(v == p) conts;
        depth[v] = depth[u]+1;
        cost += dfs1(v,u);
        children++;
    }

    if(!children){
        leaves[u] = 1;
        return depth[u];
    }

    trav(v,adj[u]){
        if(v == p) conts;
        leaves[u] += leaves[v];
    }

    if(leaves[u]){
        if(leaves[u]&1){
            cost -= 2*depth[u]*(leaves[u]>>1);
            leaves[u] = 1;
        }
        else{
            cost -= 2*depth[u]*((leaves[u]-2)>>1);
            leaves[u] = 2;
        }
    }

    return cost;
}

void solve(int test_case)
{
    ll n,q; cin >> n >> q;
    rep1(i,n-1){
        ll u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    ll root = -1;
    rep1(i,n){
        if(sz(adj[i]) >= 2){
            root = i;
        }
    }

    while(q--){
        ll k; cin >> k;
        ll ptr = n+1;
        vector<ll> nodes;

        rep1(i,k){
            ll u; cin >> u;
            nodes.pb(u);
            adj[u].pb(ptr++);
        }

        ll ans = dfs1(root,-1);
        if(leaves[root]&1) ans = -1;
        cout << ans << endl;

        trav(u,nodes){
            adj[u].pop_back();
        }
    }
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Execution timed out 1088 ms 9764 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 10452 KB Output is correct
2 Correct 11 ms 10452 KB Output is correct
3 Correct 21 ms 12956 KB Output is correct
4 Correct 31 ms 13856 KB Output is correct
5 Correct 35 ms 15304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 10896 KB Output is correct
2 Correct 16 ms 10844 KB Output is correct
3 Correct 31 ms 20312 KB Output is correct
4 Correct 46 ms 22276 KB Output is correct
5 Correct 28 ms 19032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 10324 KB Output is correct
2 Correct 136 ms 9516 KB Output is correct
3 Correct 202 ms 9048 KB Output is correct
4 Correct 234 ms 9712 KB Output is correct
5 Correct 183 ms 9820 KB Output is correct
6 Correct 243 ms 10248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1051 ms 12116 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1018 ms 13424 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Execution timed out 1088 ms 9764 KB Time limit exceeded
3 Halted 0 ms 0 KB -