이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <stack>
#include <cstdio>
#include <climits>
#include <tuple>
#include <ctime>
#include <cstring>
#include <numeric>
#include <functional>
#include <chrono>
#include <cassert>
#include <bitset>
//#include <bit>
//#include <ranges>
//#include <numbers>
#define itn int
#define sacnf scanf
#define sz(a) ((int)((a).size()))
// printf("%.10f\n", ans);
using ll = long long;
using namespace std;
const ll mod = 1e9 + 7;
const int N = 1e6 + 1, inf = 1e9;
int n;
vector<int> on;
vector<vector<int>> tree;
bool tc1()
{
return n <= 20;
}
bool tc2()
{
return n <= 40;
}
ll convert(vector<int>& v)
{
ll res = 0, p = 1;
for (int i = sz(v) - 1; i >= 0; i--)
{
if (v[i]) res += p;
p <<= 1;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
on = vector<int>(n);
tree = vector<vector<int>>(n);
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
tree[--u].push_back(--v);
tree[v].push_back(u);
}
for (int i = 0; i < n; i++) cin >> on[i];
/*if (tc1())
{
int ans = 1e9;
for (int mask = 0; mask < (1 << n); mask++)
{
vector<int> tmp = on;
int cnt = 0;
for (int i = 0; i < n; i++)
{
if ((mask >> i) & 1)
{
int u = n - i - 1;
tmp[u] ^= 1;
for (int& v : tree[u]) tmp[v] ^= 1;
cnt++;
}
}
bool flag = 1;
for (int i = 0; i < n; i++)
{
if (tmp[i])
{
flag = 0;
break;
}
}
if (flag) ans = min(ans, cnt);
}
if (ans == 1e9) cout << "impossible";
else cout << ans;
}*/
if (tc2())
{
int ans = 1e9;
int k = (n - 1) / 2;
int cnt = k + 1;
map<ll, int> mp;
ll req = convert(on);
for (int mask = 0; mask < (1 << cnt); mask++)
{
vector<int> tmp = vector<int>(n);
int ones = 0;
for (int i = 0; i < cnt; i++)
{
if ((mask >> i) & 1)
{
int u = cnt - i - 1;
tmp[u] ^= 1;
for (int& v : tree[u]) tmp[v] ^= 1;
ones++;
}
}
ll num = convert(tmp);
if (mp.find(num) == mp.end()) mp[num] = ones;
else mp[num] = min(mp[num], ones);
}
cnt = n - 1 - (k + 1) + 1;
for (int mask = 0; mask < (1 << cnt); mask++)
{
vector<int> tmp = vector<int>(n);
int ones = 0;
for (int i = 0; i < cnt; i++)
{
if ((mask >> i) & 1)
{
int u = n - i - 1;
tmp[u] ^= 1;
for (int& v : tree[u]) tmp[v] ^= 1;
ones++;
}
}
ll num = convert(tmp);
if (mp.find(req ^ num) != mp.end()) ans = min(ans, mp[req ^ num] + ones);
}
if (ans == 1e9) cout << "impossible";
else cout << ans;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |