1 Star 0 Fork 0

util6/算法刷题c++

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
AcWing323StrategyGame.cpp 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
util6 提交于 2022-03-13 22:45 +08:00 . 默认的
//
// Created by 刘宇韬 on 2022/3/13.
//
//
// Created by 刘宇韬 on 2022/3/13.
//
#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
const int N = 1505 ,M = N,INF = 0x3f3f3f3f;
int n,m;
int h[N],e[M],ne[M],w[M],idx;
bool st[N];
int dp[N][2];
int root;
void add(int a,int b){
e[idx] = b,ne[idx] = h[a],h[a] = idx++;
}
void dfs(int u){
//如果选当前根节点,就加上这个点的权值
dp[u][1] = 1;
dp[u][0] = 0;
for(int i = h[u]; i !=-1; i = ne[i]) {
int j = e[i];
dfs(j);
//如果选当前边,他的子节点可选可不选,从中挑一个权值大的
dp[u][1] += min(dp[j][1],dp[j][0]);
//如果不选当前边,就一定不选他的子节点
dp[u][0] += dp[j][1];
}
}
int main(){
while (scanf("%d",&n)!=-1){
memset(h,-1,sizeof h),idx = 0;
memset(st, 0, sizeof st);
root = 0;
for(int i = 0; i <n; i++) {
int id,cnt;
scanf("%d:(%d)",&id,&cnt);
while (cnt--){
int x;
cin>>x;
add(id,x);
st[x] = true;
}
}
while (st[root])root++;
dfs(root);
cout<<min(dp[root][0],dp[root][1])<<endl;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liuyutaocode/cppCode.git
git@gitee.com:liuyutaocode/cppCode.git
liuyutaocode
cppCode
算法刷题c++
master

搜索帮助