이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
#define int ll
const int N=101000;
const int inf=200000;
int n,a[N],dp[N][2][2];
VI g[N];
void dfs(int u,int p) {
VI ch;
for (int v:g[u]) {
if (v==p) continue;
dfs(v,u);
ch.pb(v);
}
if (ch.empty()) {
if (a[u]==0) dp[u][0][0]=0,dp[u][1][1]=1;
else dp[u][1][0]=1,dp[u][0][1]=0;
} else {
vector<VI> ndp(SZ(ch),VI(2,inf));
rep(x,0,2) rep(y,0,2) {
int st=(a[u]+x+y)%2;
rep(i,0,SZ(ch)) {
int v=ch[i];
if (i==0) {
ndp[i][0]=dp[v][0][x];
ndp[i][1]=dp[v][1][x];
} else {
ndp[i][0]=min(ndp[i-1][0]+dp[v][0][x],ndp[i-1][1]+dp[v][1][x]);
ndp[i][1]=min(ndp[i-1][1]+dp[v][0][x],ndp[i-1][0]+dp[v][1][x]);
}
}
dp[u][x][y]=min(dp[u][x][y],ndp[SZ(ch)-1][st]+x);
}
}
}
signed main() {
scanf("%lld",&n);
rep(i,0,n-1) {
int u,v;
scanf("%lld%lld",&u,&v);
g[u].pb(v);
g[v].pb(u);
}
int root=1;
rep(i,1,n+1) if (SZ(g[i])<=SZ(g[root])) root=i;
rep(i,1,n+1) scanf("%lld",a+i);
rep(i,1,n+1) rep(x,0,2) rep(y,0,2) dp[i][x][y]=inf;
dfs(root,0);
// rep(i,1,n+1) {
// rep(x,0,2) rep(y,0,2) printf("i:%d x:%d y:%d dp:%d\n",i,x,y,dp[i][x][y]);
// }
int ans=min(dp[root][1][0],dp[root][0][0]);
if (ans>=inf) printf("impossible");
else printf("%lld\n",ans);
}
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp: In function 'int main()':
xanadu.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%lld",&n);
| ~~~~~^~~~~~~~~~~
xanadu.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%lld%lld",&u,&v);
| ~~~~~^~~~~~~~~~~~~~~~~~
xanadu.cpp:65:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | rep(i,1,n+1) scanf("%lld",a+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... |