이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std ;
const int MAX = 1e5 + 10 ;
int arr[MAX] ;
int n ;
vector< vector<int> >adj(MAX) ;
int vis[MAX][2] ;
long long dp[MAX][2][2] ;
void dfs(int node , int par , bool toggle)
{
if(vis[node][toggle])
return ;
vis[node][toggle] = 1 ;
dp[node][toggle][0] = dp[node][toggle][1] = n+1 ;
int cur = arr[node] ^ toggle ;
long long sum0 = 0 , sum1 = 1 ;
vector<long long>v[2] ;
for(auto &child : adj[node])
{
if(child == par)
continue ;
dfs(child , node , 0) , dfs(child , node , 1) ;
sum0 += dp[child][0][0] , sum1 += dp[child][1][0] ;
v[0].push_back(dp[child][0][1] - dp[child][0][0]) ;
v[1].push_back(dp[child][1][1] - dp[child][1][0]) ;
}
sort(v[0].begin() , v[0].end()) ;
sort(v[1].begin() , v[1].end()) ;
if((!cur))
dp[node][toggle][0] = sum0 ;
else
dp[node][toggle][1] = sum1 ;
for(int i = 0 ; i < v[0].size() ; ++i)
{
sum0 += v[0][i] ;
if((i+1) % 2 == cur)
dp[node][toggle][0] = min(dp[node][toggle][0] , sum0) ;
}
for(int i = 0 ; i < v[1].size() ; ++i)
{
sum1 += v[1][i] ;
if((i+1) % 2 != cur)
dp[node][toggle][1] = min(dp[node][toggle][1] , sum1) ;
}
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 0 ; i < n-1 ; ++i)
{
int x , y ;
cin>>x>>y ;
adj[x].push_back(y) ;
adj[y].push_back(x) ;
}
for(int i = 1 ; i <= n ; ++i)
cin>>arr[i] ;
dfs(1 , -1 , 0) ;
long long ans = min(dp[1][0][0] , dp[1][0][1]) ;
if(ans > n)
cout<<"impossible\n" ;
else
cout<<ans<<"\n" ;
return 0 ;
}
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp: In function 'void dfs(int, int, bool)':
xanadu.cpp:39:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0 ; i < v[0].size() ; ++i)
| ~~^~~~~~~~~~~~~
xanadu.cpp:45:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0 ; i < v[1].size() ; ++i)
| ~~^~~~~~~~~~~~~
# | 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... |