Submission #1299004

#TimeUsernameProblemLanguageResultExecution timeMemory
1299004quan606303Power Plant (JOI20_power)C++20
6 / 100
2 ms584 KiB
/* * @Author: RMQuan * @Date: 2025-12-04 14:00:03 * @Last Modified by: RMQuan * @Last Modified time: 2025-12-04 15:52:52 */ /*idea : */ #include <bits/stdc++.h> bool M1; #define int long long #define ll long long #define INTMAX INT_MAX #define INTMIN INT_MIN #define LONGMAX LLONG_MAX #define LONGMIN LLONG_MIN #define fi first #define se second #define memfull(a,b) memset(a,b,sizeof(a)); #define endl '\n' #define TASK "TEST" #define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);} using namespace std; const int MOD=1e9+7; const int maxn=2e5+7; vector<int> adj[maxn]; bool is_spl[maxn]; int n,dp[maxn][3]; /* dp[u][0]: co it nhat 0 thang spl thang hang dp[u][1]: co it nhat 1 thang spl thang hang dp[u][2]: co it nhat 2 thang spl thang hang */ int ans=0; void dfs(int u,int p) { if (is_spl[u]) { int cnt1=-1; dp[u][1]=1; int dem=0; for (auto v:adj[u]) { if (v==p)continue; dfs(v,u); //case 1 : tu keo len dp[u][2]=max(dp[u][2],dp[v][1]+1); dp[u][2]=max(dp[u][2],dp[v][2]-2); //case 2 : merge 2 cay lai voi nhau cnt1+=max(dp[v][1],dp[v][2]-2); dem++; } if (dem>=2)dp[u][1]=max(dp[u][1],cnt1); } else { int cnt=0,dem=0; for (auto v:adj[u]) { if (v==p)continue; dfs(v,u); //case 1 : tu keo len dp[u][1]=max(dp[u][1],dp[v][1]); dp[u][2]=max(dp[u][2],dp[v][2]); //case 2 : merge 2 cay lai voi nhau cnt+=max(dp[v][1],dp[v][2]-2); dem++; } if (dem>=2)dp[u][1]=max(dp[u][1],cnt); } ans=max({ans,dp[u][0],dp[u][1],dp[u][2]}); // cerr<<u<<" "<<dp[u][0]<<" "<<dp[u][1]<<" "<<dp[u][2]<<endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); file(); cin>>n; for (int i=1;i<n;i++) { int u,v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } for (int i=1;i<=n;i++) { char x; cin>>x; if (x=='1') is_spl[i]=1; else is_spl[i]=0; } dfs(1,0); cout<<ans; return 0; }

Compilation message (stderr)

power.cpp: In function 'int32_t main()':
power.cpp:25:50: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 | #define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
      |                                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
power.cpp:83:5: note: in expansion of macro 'file'
   83 |     file();
      |     ^~~~
power.cpp:25:81: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 | #define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
      |                                                                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
power.cpp:83:5: note: in expansion of macro 'file'
   83 |     file();
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...