답안 #390529

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
390529 2021-04-16T09:18:21 Z talant117408 Spring cleaning (CEOI20_cleaning) C++17
34 / 100
1000 ms 14776 KB
/*
    Code written by Talant I.D.
*/
#include <bits/stdc++.h>
 
using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
 
const int mod = 1e9+7;
 
ll mode(ll a) {
    a %= mod;
    if (a < 0) a += mod;
    return a;
}
 
ll subt(ll a, ll b) {
    return mode(mode(a)-mode(b));
}
 
ll add(ll a, ll b) {
    return mode(mode(a)+mode(b));
}
 
ll mult(ll a, ll b) {
    return mode(mode(a)*mode(b));
}
 
ll binpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b&1) res = mult(res, a);
        a = mult(a, a);
        b >>= 1;
    }
    return res;
}

const int N = 1e5+7;
vector <int> graph[N], graph2[N];
int n, subtree[N], par[N];
bool leaf[N];
ll ans;

bool subtask1() {
	return sz(graph[1]) == n-1;
}

bool subtask2() {
	bool flag = 1;
	for (int i = 1; i <= n; i++) {
		if (i == 1) {
			if (sz(graph[1]) != 1 || graph[1][0] != 2) flag = 0;
		}
		else if (i == n) {
			if (sz(graph[n]) != 1 || graph[n][0] != n-1) flag = 0;
		}
		else if (sz(graph[i]) != 2 || min(graph[i][0], graph[i][1]) != i-1 || max(graph[i][0], graph[i][1]) != i+1) {
			flag = 0;
		}
	}
	return flag;
}

void dfs(int v, int p) {
	par[v] = p;
	subtree[v] = 0;
	int children = 0;
	for (auto to : graph[v]) {
		if (to == p) continue;
		children++;
		dfs(to, v);
		subtree[v] += subtree[to];
	}
	if (children == 0) {
		subtree[v] = 1;
		leaf[v] = 1;
	}
	if (v != p) {
		if (subtree[v]&1) ans++;
		else ans += 2;	
	}
}

void dfs2(int v, int p) {
	subtree[v] = 0;
	int children = 0;
	for (auto to : graph2[v]) {
		if (to == p) continue;
		children++;
		dfs2(to, v);
		subtree[v] += subtree[to];
	}
	if (children == 0) subtree[v] = 1;
	if (v != p) {
		if (subtree[v]&1) ans++;
		else ans += 2;	
	}
}

int main() {
	do_not_disturb
	
	int q;
	cin >> n >> q;
	for (int i = 0; i < n-1; i++) {
		int x, y;
		cin >> x >> y;
		graph[x].pb(y);
		graph[y].pb(x);
	}
	
	if (subtask1()) {
		while (q--) {
			int m;
			cin >> m;
			vector <int> d(m), has(n+1);
			for (auto &to : d) cin >> to;
			for (auto to : d) has[to]++;
			multiset <int> st;
			ll ans = 0; 
			for (int i = 2; i <= n; i++) {
				while (has[i] > 2) {
					ans += 2;
					has[i] -= 2;
				}
				if (!has[i]) {
					st.insert(1);
				}
				else {
					while (has[i]) {
						st.insert(2);
						has[i]--;
					}
				}
			}
			if (sz(st)&1) {
				cout << -1 << endl;
			}
			else {
				while (sz(st)) {
					auto a = *st.begin();
					st.erase(st.find(a));
					auto b = *st.begin();
					st.erase(st.find(b));
					ans += a+b;
				}
				cout << ans << endl;
			}
		}
	}
	else if (subtask2()) {
		while (q--) {
			int m;
			cin >> m;
			vector <int> d(m);
			for (auto &to : d) cin >> to;
			sort(all(d));
			if (sz(d)&1) {
				cout << -1 << endl;
			}
			else {
				ll ans = n-1;
				for (int i = 0; i < sz(d); i += 2) {
					ans += abs(d[i]-d[i+1])+2;
				}
				cout << ans << endl;
			}
		}
	}
	else if (n <= 20000 && q <= 300) {
		while (q--) {
			ans = 0;
			for (int i = 1; i <= n; i++) {
				for (auto to : graph[i]) graph2[i].pb(to);
			}
			int nw = n;
			int m;
			cin >> m;
			vector <int> d(m);
			for (auto &to : d) cin >> to;
			for (auto to : d) {
				nw++;
				graph2[to].pb(nw);
				graph2[nw].pb(to);
			}
			int root = -1;
			for (int i = 1; i <= nw; i++) {
				if (sz(graph2[i]) != 1) {
					root = i;
					break;
				}
			}
			dfs2(root, root);
			if (subtree[root]%2 == 0) {
				cout << ans << endl;
			}
			else {
				cout << -1 << endl;
			}
			for (int i = 1; i <= nw; i++) {
				graph2[i].clear();
			}
		}
	}
	else {
		int root = -1;
		for (int i = 1; i <= n; i++) {
			if (sz(graph[i]) != 1) {
				root = i;
				break;
			}
		}
		dfs(root, root);
		while (q--) {
			int m;
			cin >> m;
			ll res = ans;
			vector <int> d(m);
			vector <int> parity(n+1);
			for (int i = 1; i <= n; i++) {
				parity[i] = subtree[i]%2;
			}
			for (auto &to : d) cin >> to;
			for (auto to : d) {
				if (leaf[to]) {
					res++;
				}
				else {
					auto cur = to;
					while (cur != par[cur]) {
						res++;
						parity[cur] = 1-parity[cur];
						cur = par[cur];
					}
					parity[cur] = 1-parity[cur];
					res++;
				}
			}
			if (parity[root]) {
				cout << -1 << endl;
			}
			else {
				cout << res << endl;
			}
		}
	}
	
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Incorrect 187 ms 6304 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 5580 KB Output is correct
2 Correct 12 ms 5600 KB Output is correct
3 Correct 51 ms 13560 KB Output is correct
4 Correct 49 ms 12216 KB Output is correct
5 Correct 75 ms 14776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 5424 KB Output is correct
2 Correct 17 ms 5452 KB Output is correct
3 Correct 43 ms 8008 KB Output is correct
4 Correct 55 ms 8132 KB Output is correct
5 Correct 40 ms 7704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 327 ms 6988 KB Output is correct
2 Correct 220 ms 6544 KB Output is correct
3 Correct 315 ms 6348 KB Output is correct
4 Correct 346 ms 7060 KB Output is correct
5 Correct 295 ms 7072 KB Output is correct
6 Correct 355 ms 7124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1088 ms 8204 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1090 ms 9760 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Incorrect 187 ms 6304 KB Output isn't correct