UVa 10859 Placing Lampposts
来源:互联网 发布:防护骷髅面具淘宝 编辑:程序博客网 时间:2024/06/06 03:36
题目
UVa 10859 Placing Lampposts
题解
先读清楚题是覆盖边,和最大独立集是有区别的,然后无向无环就是森林!然后,,这题是看了题解的,感觉也不是比较难_ (:зゝ∠) _但是又不是很容易想出来,dp[i][0]表示父节点没放灯,然后应该就懂了。还有就是根节点的父亲是不能放的所以要+dp[root][0]
代码
//QWsin#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int M=2000;const int INF=(1<<30);const int maxn=1000+10;const int maxm=2000+10;int n,m;struct Edge{int u,v;Edge(int u=0,int v=0):u(u),v(v){}}e[maxm];int First[maxn],Next[maxm],ecnt;inline void add_edge(int u,int v){ Next[ecnt]=First[u];First[u]=ecnt;e[ecnt++]=Edge(u,v); Next[ecnt]=First[v];First[v]=ecnt;e[ecnt++]=Edge(v,u);}int dp[maxn][2],vis[maxn];void dfs(int u,int pre,const int &root){ vis[u]=1; int sigma0=0,sigma1=0; for(int i=First[u];i!=-1;i=Next[i]) if(e[i].v!=pre) { dfs(e[i].v,u,root); sigma0+=dp[e[i].v][0]; sigma1+=dp[e[i].v][1]; } dp[u][0]=sigma1+M+(u!=root); if(u==root) dp[u][0]=min(dp[u][0],sigma0); dp[u][1]=min(sigma0+(u!=root),sigma1+M);}inline void solve(){ int n,m;cin>>n>>m;ecnt=0; memset(vis,0,sizeof vis); memset(First,-1,sizeof First); for(int i=1,u,v;i<=m;++i) scanf("%d%d",&u,&v),add_edge(++u,++v); int ans=0; for(int i=1;i<=n;++i) if(!vis[i]) dfs(i,i,i),ans+=dp[i][0];//根节点没有父节点不能放 printf("%d %d %d\n",ans/M,m-ans%M,ans%M);}int main(){ freopen("std.in","r",stdin); int T;cin>>T; while(T--) solve(); return 0;}
0 0
- Uva-10859-Placing Lampposts
- UVA 10859 Placing Lampposts
- UVa 10859 Placing Lampposts
- UVa 10859 Placing Lampposts
- UVA--10859--Placing Lampposts--DP
- Uva - 10859 - Placing Lampposts(树形dp)
- Uva 10859 - Placing Lampposts 树形dp
- uva 10859 Placing Lampposts 树形dp
- uva 10859 Placing Lampposts (树形dp)
- UVA 10859 Placing Lampposts (动态规划)
- UVa 10859 Placing Lampposts / 树形DP
- uva 10859 - Placing Lampposts(树形dp)
- UVa:10859 Placing Lampposts(树形DP)
- Placing Lampposts - UVa 10859 树形dp
- UVA 10859 Placing Lampposts 树形dp(水
- uva 10859 Placing Lampposts 树形dp
- UVA - 10859 Placing Lampposts 暴力+森林+技巧
- uva 10859 Placing Lampposts,树形dp
- 我是怎么从0开始跑通ORBSLAM2+kinectv1的(win8或者win10)
- 第十四周项目1(3)--验证二叉排序树相关算法
- 叠罗汉1(最长递增子序列)
- .CreateFeatureClass报错原因解析
- 第六章 数据存储全方案-详解持久化技术
- UVa 10859 Placing Lampposts
- 集成Spring+SpringMVC+Mybatis+Shiro+Maven+JUnit的Java Web框架
- 1 + 2 - 3 + 4........n 提高运行速度
- 什么是Nib文件
- 查询表定义
- JavaScript权威指南_172_第17章_事件处理_17.6-鼠标滚轮事件
- strcmp、strcat、strcpy、strncpy函数的实现
- 【Unity】publishing setting keystore作用
- 万年历