이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define DIMN 100010
using namespace std;
int dp[DIMN][2][2] , c[DIMN];
vector <int> v[DIMN];
void dfs (int nod , int tt){
int i , vecin , par , impar , par_ant , ok = 0;
for (i = 0 ; i < v[nod].size() ; i++){
vecin = v[nod][i];
if (vecin != tt){
dfs(vecin , nod);
ok = 1;
}
}
if (!ok){ /// e frunza
dp[nod][c[nod]][0] = 0;
dp[nod][1 - c[nod]][1] = 1;
return;
}
/// daca eu apas nod, toti vecinii trb sa fie 1
par = 0;
impar = DIMN;
for (i = 0 ; i < v[nod].size() ; i++){
vecin = v[nod][i];
if (vecin != tt){
par_ant = par;
par = min(par + dp[vecin][1][0] , impar + dp[vecin][1][1]);
impar = min(impar + dp[vecin][1][0] , par_ant + dp[vecin][1][1]);
}
}
if (c[nod] == 1){
dp[nod][0][1] = min(dp[nod][0][1] , par + 1);
dp[nod][1][1] = min (dp[nod][1][1] , impar + 1);
}
else { /// c[nod] = 0;
dp[nod][0][1] = min(dp[nod][0][1] , impar + 1);
dp[nod][1][1] = min (dp[nod][1][1] , par + 1);
}
/// daca nu apas nod, toti vecinii trebuie sa fie 0
par = 0;
impar = DIMN;
for (i = 0 ; i < v[nod].size() ; i++){
vecin = v[nod][i];
if (vecin != tt){
par_ant = par;
par = min(par + dp[vecin][0][0] , impar + dp[vecin][0][1]);
impar = min(impar + dp[vecin][0][0] , par_ant + dp[vecin][0][1]);
}
}
if (c[nod] == 1){
dp[nod][0][0] = min(dp[nod][0][0] , impar);
dp[nod][1][0] = min (dp[nod][1][0] , par);
}
else { /// c[nod] = 0;
dp[nod][0][0] = min(dp[nod][0][0] , par);
dp[nod][1][0] = min (dp[nod][1][0] , impar);
}
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int n , i , x , y;
fscanf (fin,"%d",&n);
for (i = 1 ; i < n ; i++){
fscanf (fin,"%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
for (i = 1 ; i <= n ; i++){
fscanf (fin,"%d",&c[i]);
dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = n + 1;
}
dfs(1 , 0);
if (min(dp[1][0][0] , dp[1][0][1]) > n)
fprintf (fout,"impossible");
else fprintf (fout,"%d",min(dp[1][0][0] , dp[1][0][1]));
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp: In function 'void dfs(int, int)':
xanadu.cpp:10:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for (i = 0 ; i < v[nod].size() ; i++){
| ~~^~~~~~~~~~~~~~~
xanadu.cpp:31:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (i = 0 ; i < v[nod].size() ; i++){
| ~~^~~~~~~~~~~~~~~
xanadu.cpp:56:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (i = 0 ; i < v[nod].size() ; i++){
| ~~^~~~~~~~~~~~~~~
xanadu.cpp: In function 'int main()':
xanadu.cpp:87:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | fscanf (fin,"%d",&n);
| ~~~~~~~^~~~~~~~~~~~~
xanadu.cpp:89:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | fscanf (fin,"%d%d",&x,&y);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
xanadu.cpp:94:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | fscanf (fin,"%d",&c[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... |