이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 100005
vector <int> state;
vector <vector <int>> adj;
int dp[2][2][MAX_N];
int dfs(int u ,bool st ,bool sp ,int p){
if(adj[u].size() == 1 && p != -1)
return (state[u]^sp) == st? st : MAX_N;
auto&ret = dp[st][sp][u];
if(~ret)
return ret;
ret = MAX_N;
vector <pair<int ,int>> chld;
for(int&v : adj[u]) if(v != p)
chld.push_back({dfs(v ,0 ,st ,u) ,dfs(v ,1 ,st ,u)});
sort(chld.begin() ,chld.end() ,[](auto&i ,auto&j){
return i.second-i.first < j.second-j.first;
});
int64_t tot = 0;
for(int i = 0; i < chld.size(); i++)
tot += chld[i].first;
int j = 0;
if((state[u]^sp) != st)
tot += chld[j].second - chld[j].first ,j++;
ret = min((int64_t)ret ,tot);
while(j < chld.size()){
tot += chld[j].second - chld[j].first ,j++;
if(j == chld.size()) break;
tot += chld[j].second - chld[j].first ,j++;
ret = min((int64_t)ret ,tot);
}
return ret = ret + st;
}
int main()
{
int n;
scanf("%d",&n);
adj.resize(n+1);
state.resize(n+1);
for(int a ,b ,i = 1; i < n; i++){
scanf("%d%d",&a,&b);
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i = 1; i <= n; i++)
scanf("%d",&state[i]);
memset(dp ,-1 ,sizeof dp);
int ans = min(dfs(1 ,0 ,0 ,-1) ,dfs(1 ,1 ,0 ,-1));
printf(ans >= MAX_N? "impossible\n" : "%d\n",ans);
}
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp: In function 'int dfs(int, bool, bool, int)':
xanadu.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < chld.size(); i++)
| ~~^~~~~~~~~~~~~
xanadu.cpp:30:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | while(j < chld.size()){
| ~~^~~~~~~~~~~~~
xanadu.cpp:32:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | if(j == chld.size()) break;
| ~~^~~~~~~~~~~~~~
xanadu.cpp: In function 'int main()':
xanadu.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
xanadu.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d%d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~
xanadu.cpp:51:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d",&state[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... |