이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;
template<class x>
using ordered_set = tree<x, null_type,less<x>, rb_tree_tag,tree_order_statistics_node_update>;
ll mod=(ll)1e9+7;
ll mod1=998244353;
///the defines :)
#define endl '\n'
#define vi vector<int>
#define ent(arr) for(int i=0;i<arr.size();i++)cin>>arr[i];
#define all(arr) arr.begin(),arr.end()
#define allr(arr) arr.rbegin(),arr.rend()
#define sz size()
#define int long long
int dp[200002][2][2];
int n;
vi arr;
vector<vi>adj;
int dfs(int node,int parent,int tg,int cur)
{
int pog=arr[node]^tg^cur;
// cout<<node<<" "<<parent<<" "<<pog<<endl;
if(dp[node][tg][cur]!=-1)return dp[node][tg][cur];
if(parent&&adj[node].sz==1)return (pog?1e9:cur);
//odds and even thingies depending on pog, (i forgor)
int x=adj[node].sz;
int newdp[x+2][2];
memset(newdp,1e9,sizeof newdp);
for(int i=0;i<=x+1;i++)newdp[i][0]=newdp[i][1]=1e9;
newdp[0][pog]=cur;
int cnt=0;
vector<pair<int,int>>xd;
xd.push_back({0,0});
for(auto i:adj[node])
{
if(i==parent)continue;
int e=dfs(i,node,cur,1),e1=dfs(i,node,cur,0);
xd.push_back({e,e1});
}
for(int i=1;i<xd.sz;i++)
{
for(int j=0;j<2;j++)
{
newdp[i][j]=min({newdp[i-1][j^1]+xd[i].first,newdp[i-1][j]+xd[i].second});
}
}
/* cout<<node<<" "<<parent<<" "<<pog<<" "<<tg<<" "<<cur<<" "<<newdp[xd.sz][0]<<endl;
for(int i=0;i<xd.sz;i++)
{
cout<<newdp[i][0]<<" "<<newdp[i][1]<<endl;
}
for(auto i:xd)cout<<i.first<<" "<<i.second<<endl;
cout<<"---"<<endl;
*/
return dp[node][tg][cur]=newdp[xd.sz-1][0];
}
void preprocess() {}
void solve()
{
cin>>n;
adj.resize(n+1);
arr.resize(n+1);
for(int i=0;i<n-1;i++)
{
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
memset(dp,-1,sizeof dp);
for(int i=1;i<=n;i++)cin>>arr[i];
int x=min(dfs(1,0,0,1),dfs(1,0,0,0));
if(x>=1e9)cout<<"impossible"<<endl;
else cout<<x<<endl;
}
signed main()
{
// freopen("meta_game_input.txt","r",stdin);
// freopen("otput.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
preprocess();
//bla();
int t=1;
//cin>>t;
while(t--)
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp: In function 'long long int dfs(long long int, long long int, long long int, long long int)':
xanadu.cpp:44:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i=1;i<xd.sz;i++)
| ^
xanadu.cpp:35:9: warning: unused variable 'cnt' [-Wunused-variable]
35 | int cnt=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... |