This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <limits.h>
#include <math.h>
#include <chrono>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define p2 pair<ll, ll>
#define p3 tuple<ll,ll,ll>
#define p4 vi
#define ip3 tuple<int,int,int>
#define ip4 tuple<int,int,int,int>
#define vp2 vector<p2>
#define vp3 vector<p3>
#define inf 2e9
#define linf 1e17
#define read(a) cin >> a
#define write(a) cout << (a) << "\n"
#define dread(type, a) type a; cin >> a
#define dread2(type, a, b) dread(type, a); dread(type, b)
#define dread3(type, a, b, c) dread2(type, a, b); dread(type, c)
#define dread4(type, a, b, c, d) dread3(type, a, b, c); dread(type, d)
#define dread5(type, a, b, c, d, e) dread4(type, a, b, c, d); dread(type, e)
#ifdef _DEBUG
#define deb __debugbreak();
#else
#define deb ;
#endif
#define rep(i, high) for (ll i = 0; i < high; i++)
#define repp(i, low, high) for (ll i = low; i < high; i++)
#define repe(i, container) for (auto& i : container)
#define per(i, high) for (ll i = high; i >= 0; i--)
#define readpush(type,vect) type temp; read(temp); vect.push_back(temp);
#define readvector(type, name, size) vector<type> name(size); rep(i,size) {dread(type,temp); name[i]=temp;}
#define readinsert(type,a) {type temp; read(temp); a.insert(temp);}
#define all(a) begin(a),end(a)
#define setcontains(set, x) (set.find(x) != set.end())
#define stringcontains(str, x) (str.find(x) != string::npos)
#define within(a, b, c, d) (a >= 0 && a < b && c >= 0 && c < d)
#define ceildiv(x,y) ((x + y - 1) / y)
#define fract(a) (a-floor(a))
#define sign(a) ((a>0) ? 1 : -1)
auto Start = chrono::high_resolution_clock::now();
inline void fast()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
vector<unordered_set<int>> children;
vector<int> parent;
vector<bool> is_on;
int n;
void dfs(int n, int par) {
parent[n] = par;
children[n].erase(par);
for(int child : children[n]) dfs(child, n);
}
vector<vector<ll*>> dp_table;
long long dp(int n, bool parent_switched, bool need_switch) {
ll*& ref = dp_table[n][parent_switched * 2 + need_switch];
if(ref != NULL) return *ref;
bool odd = is_on[n] ^ parent_switched ^ need_switch;
long long result = 0;
int flips = 0;
long long worst_flip = -1, worst_not_flip = -1;
for(int c : children[n]) {
long long flip = dp(c, need_switch, true);
long long no_flip = dp(c, need_switch, false);
result += min(flip, no_flip);
if(flip < no_flip) {
flips++;
if(no_flip - flip < worst_flip || worst_flip == -1) {
worst_flip = no_flip - flip;
}
} else {
if(flip - no_flip < worst_not_flip || worst_not_flip == -1) {
worst_not_flip = flip - no_flip;
}
}
}
if((flips % 2 == 1) != odd) {
if(worst_flip == -1 && worst_not_flip == -1) return inf;
else if((worst_flip == -1) ^ (worst_not_flip == -1)) result += max(worst_not_flip, worst_flip);
else result += min(worst_not_flip, worst_flip);
}
ref = new ll(result + need_switch);
return *ref;
}
int main() {
//fast();
cin >> n;
is_on.resize(n);
children.resize(n);
parent.resize(n);
dp_table.resize(n, vector<ll*>(4, NULL));
for(int i = 0; i < n - 1; i++) {
dread2(int, a, b);
a--;
b--;
children[a].insert(b);
children[b].insert(a);
}
dfs(0, -1);
rep(i, n) {
char q;
cin >> q;
is_on[i] = q == '1';
}
long long result = min(dp(0, false, false), dp(0, false, true));
if(result < n) write(result);
else write("impossible");
}
/*
5
1 2
1 3
2 4
2 5
0 1 0 1 1
5
1 2
2 3
3 4
4 5
0 1 1 1 1
*/
Compilation message (stderr)
xanadu.cpp:18: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
18 | #pragma GCC optimization ("O3")
|
xanadu.cpp:19: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
19 | #pragma GCC optimization ("unroll-loops")
|
# | 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... |