제출 #406873

#제출 시각아이디문제언어결과실행 시간메모리
406873Ruxandra985The Xana coup (BOI21_xanadu)C++14
100 / 100
109 ms15312 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...