| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1299035 | quan606303 | Power Plant (JOI20_power) | C++20 | 87 ms | 32792 KiB |
/*
* @Author: RMQuan
* @Date: 2025-12-04 14:00:03
* @Last Modified by: RMQuan
* @Last Modified time: 2025-12-04 16:40:28
*/
/*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);
// 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
cnt1+=max(0ll,max(dp[v][1],dp[v][2]-2));
dem++;
}
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(0ll,max(dp[v][1],dp[v][2]-2));
dem++;
}
dp[u][1]=max(dp[u][1],cnt);
//if (u==3)cerr<<cnt<<endl;
}
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;
}
for (int i=1;i<=n;i++)dp[i][1]=dp[i][2]=-1e9;
dfs(1,0);
cout<<ans;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
