UVA 10859 Placing Lampposts 树形dp(水
来源:互联网 发布:铝合金门窗效果图软件 编辑:程序博客网 时间:2024/05/07 09:19
题目链接:点击打开链接
题意:
白书P70
思路:
简单题,每个点分放或不放。
import java.io.PrintWriter;import java.util.ArrayList;import java.util.Scanner;public class Main {int min(int a,int b){return a>b?b:a;}int max(int a,int b){return a>b?a:b;}static int N = 1005;static int M = 2005;ArrayList<Integer>[] G = new ArrayList[N];int n, m;boolean[] vis = new boolean[N];int[][] dp = new int[N][2];int dfs(int u){vis[u] = true;int ans = 0;dp[u][0] = 0; dp[u][1] = M;//0表示不放for(int i = 0; i < G[u].size(); i++){int v = G[u].get(i); if(vis[v])continue;dfs(v);dp[u][1] += min(dp[v][0]+1, dp[v][1]);dp[u][0] += dp[v][1]+1;}return min(dp[u][0], dp[u][1]);}void init(){n = cin.nextInt(); m = cin.nextInt();for(int i = 0; i < n; i++){vis[i] = false;G[i].clear();}for(int i = 1, u, v; i <= m; i++){u = cin.nextInt(); v = cin.nextInt();G[u].add(v); G[v].add(u);}}void work() {for(int i = 0; i < N; i++)G[i] = new ArrayList();int T; T = cin.nextInt();while(T-->0){init();int ans = 0;for(int i = 0; i < n; i++)if(vis[i] == false)ans += dfs(i);out.printf("%d %d %d", ans/M, m-ans%M, ans%M); out.println();}}Main() {cin = new Scanner(System.in);out = new PrintWriter(System.out);}public static void main(String[] args) {Main e = new Main();e.work();out.close();}public Scanner cin;public static PrintWriter out;}
0 0
- 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 / 树形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(树形DP)
- UVA 10859 Placing Lampposts--树形dp
- UVa 10859 Placing Lampposts (树形DP)
- 10859 - Placing Lampposts(树形dp)
- UVA--10859--Placing Lampposts--DP
- 【动态规划】【树形DP】[UVa 10859]Placing Lampposts
- UVA 题目10859 Placing Lampposts(树形DP)
- SVN客户端常用命令使用方法专家指导
- SUSAN算子——边缘检测
- 精易模块中普通填表初始化失败的解决方案
- Python模块学习——pythoncom模块
- redis的高级应用之一(Redis安全性\主从复制\事务处理)
- UVA 10859 Placing Lampposts 树形dp(水
- NP完全性问题
- Hadoop单机模式安装
- 希尔排序
- [ASP.NET MVC 小牛之路]03 - Razor语法
- 【网络协议】TCP连接的建立和释放
- sql 查询语句
- CC2540 UBSDongle 驱动安装安装失败的解决方法
- swift 调用 webservice