이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vo vector
#define pb push_back
#define se second
#define fi first
#define all(v) v.begin(), v.end()
typedef vector<int> vi;
typedef pair<int, int> pii;
#define umap unordered_map
#define uset unordered_set
#define rep(i, a, b) for(int i=(a); i<b; i++)
#define pr1(x) cerr << #x << '=' << x << ' ';
//for google contests
#define repd(i, a, b) for(int i=(b-1); i >= a; i--)
void _pr(signed x) {cerr << x;}
void _pr(long long x) {cerr << x;}
void _pr(unsigned long long x) {cerr << x;}
void _pr(double x) {cerr << x;}
void _pr(char x) {cerr << '\'' << x << '\'';}
void _pr(const char* x) {cerr << x;}
void _pr(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V> void _pr(const pair<T, V> &x);
template<typename T, typename V> void _pr(const pair<T, V> &x) {cerr << "\e[95m" << "[ "; _pr(x.first); cerr << ", "; _pr(x.second); cerr << "\e[95m" << ']';}
template<typename T> void _pr(const T &x) {int F=0; cerr << '{'; for(auto &i: x) cerr << (F++ ? ", " : ""), _pr(i); cerr << "\e[91m" << '}';}
template <typename T, typename... V> void _pr(T t, V... v) {_pr(t); if(sizeof...(v)) cerr << ", "; _pr(v...);}
#define pr(x...) cerr << "\e[91m" << __func__ << ':' << __LINE__ << " [" << #x << "] = ["; _pr(x); cerr << "\e[91m" << ']' << "\033[0m" << endl;
//go for outline with ;, then details
int const inf = LLONG_MAX, mxn = 1e5+4;
int n, ans=inf;
vo<vi> adj(mxn);
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n;
rep(i, 0, n-1){
int a, b; cin>>a>>b; a--, --b;
adj[a].pb(b), adj[b].pb(a);
}
string s, goal;
rep(i, 0, n) {char c; cin>>c; s.pb(c); goal.pb('0');}
map<string, int> dist;
dist[goal] = 0;
queue<string> qu; qu.push(goal);
while(qu.size()){
string v = qu.front(); qu.pop();
if(v==s) {ans = dist[v]; break;}
rep(u, 0, n){
string tmp = v;
//turn on u
tmp[u]^=1;
for(auto x: adj[u]){
tmp[x]^=1;
}
if(!dist.count(tmp)){
dist[tmp] = dist[v]+1;
qu.push(tmp);
}
}
}
if(ans==inf) cout << "impossible";
else cout << ans;
//checka om svaret alltid är mindre än N
}
# | 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... |