如果知道了树的形态,那么可以树形DP,每个时刻只需要计算必选根的独立集个数以及必不选根的独立集个数。

那么现在知道独立集个数,要构造出树,可以考虑DP这棵树的形态,然后将之前树形DP的值作为现在DP的状态,即$dp[i][j]$表示必选根的独立集个数为$i$,必不选根的独立集个数为$j$时,树的节点数最少是多少。

那么完成这个DP之后,输出方案只需要沿着最优值来的顺序dfs输出即可。

#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<time.h>
#include<assert.h>
#include<iostream>
using namespace std;
typedef long long LL;
typedef pair<int,int>pi;
const int Inf=100;
int cnt;
int dp[2020][2020];
struct Node{
short i,j,x,y;
Node(){}
Node(short i,short j,short x,short y):i(i),j(j),x(x),y(y){}
};
Node pre[2020][2020];
void dfs(int u,int x,int y){
if(x==1&&y==1)return ;
//printf("x=%d y=%d\n",x,y);
dfs(u,pre[x][y].i,pre[x][y].j);
++cnt;
printf("%d %d\n",u,cnt);
dfs(cnt,pre[x][y].x,pre[x][y].y);
}
vector<int>V[3000];
int main(){
int tl=0;
for(int i=1;i<=2005;i++){
for(int j=i;j<=2005;j+=i)V[j].push_back(i);
}
for(int i=0;i<2020;i++)for(int j=0;j<2020;j++)dp[i][j]=Inf;
dp[1][1]=1;
for(int i=1;i<=2005;i++){
for(int j=1;j<=2005;j++){
if(dp[i][j]>15)continue;
for(int y=1;y*i<=2005;y++){
for(int x=1;(x+y)*j<=2005;x++){
int tmp=dp[i][j]+dp[x][y];
int nx=y*i,ny=j*(x+y);
if(dp[nx][ny]>tmp){
dp[nx][ny]=tmp;
pre[nx][ny]=Node(i,j,x,y);
}
tl++;
}
}
for(int y=1;y*(i+j)<=2005;y++){
for(int x=1;x*j<=2005;x++){
int tmp=dp[i][j]+dp[x][y];
int nx=j*x,ny=y*(i+j);
if(dp[nx][ny]>tmp){
dp[nx][ny]=tmp;
pre[nx][ny]=Node(x,y,i,j);
}
tl++;
}
} }
}
/*
for(int nx=1;nx<=2005;nx++){
for(int ny=1;ny<=2005;ny++){
if(nx==1&&ny==1){dp[nx][ny]=1;continue;}
for(int it1=0;it1<V[nx].size();it1++){
for(int it2=0;it2<V[ny].size();it2++){
int i=V[nx][it1],j=V[ny][it2];
int y=nx/i,x=ny/j-y;
if(x>=1&&y>=1&&dp[i][j]+dp[x][y]<dp[nx][ny]){
dp[nx][ny]=dp[i][j]+dp[x][y];
pre[nx][ny]=Node(i,j,x,y);
}
tl++;
}
}
}
}
*/
//printf("tl=%d\n",tl);
int _;scanf("%d",&_);
while(_--){
int m;
scanf("%d",&m);
//for(int tm=1;tm<=2000;tm++){
// m=tm+1;
m++;
bool flag=0;
int sx=-1,sy;
for(int i=0;i<=m;i++){
if(dp[i][m-i]<=15){
sx=i;sy=m-i;
break;
}
}
if(sx<0)puts("-1");
else{
printf("%d\n",dp[sx][sy]);
cnt=1;
dfs(1,sx,sy);
}
}
return 0;
}

  

ZOJ3951 : Independent Set的更多相关文章

  1. 写一个程序可以对两个字符串进行测试,得知第一个字符串是否包含在第二个字符串中。如字符串”PEN”包含在字符串“INDEPENDENT”中。

    package lovo.test; import java.util.Scanner; public class Java { @param args public static void main ...

  2. Deep Learning 13_深度学习UFLDL教程:Independent Component Analysis_Exercise(斯坦福大学深度学习教程)

    前言 理论知识:UFLDL教程.Deep learning:三十三(ICA模型).Deep learning:三十九(ICA模型练习) 实验环境:win7, matlab2015b,16G内存,2T机 ...

  3. [ZZ] KlayGE 游戏引擎 之 Order Independent Transparency(OIT)

    转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=2233 http://dogasshole.iteye.com/blog/1429665 ht ...

  4. Andrew Ng机器学习公开课笔记–Independent Components Analysis

    网易公开课,第15课 notes,11 参考, PCA本质是旋转找到新的基(basis),即坐标轴,并且新的基的维数大大降低 ICA也是找到新的基,但是目的是完全不一样的,而且ICA是不会降维的 对于 ...

  5. Questions that are independent of programming language. These questions are typically more abstract than other categories.

    Questions that are independent of programming language.  These questions are typically more abstract ...

  6. Interview-Largest independent set in binary tree.

    BT(binary tree), want to find the LIS(largest independent set) of the BT. LIS: if the current node i ...

  7. 【转】NDK编译可执行文件在Android L中运行显示error: only position independent executables (PIE) are supported.失败问题解决办法。

    原文网址:http://blog.csdn.net/hxdanya/article/details/39371759 由于使用了NDK编译的可执行文件在应用中调用,在4.4及之前的版本上一直没出问题. ...

  8. 基于Hama并联平台Finding a Maximal Independent Set 设计与实现算法

    笔者:白松 NPU学生. 转载请注明出处:http://blog.csdn.net/xin_jmail/article/details/32101483. 本文參加了2014年CSDN博文大赛,假设您 ...

  9. More than one file was found with OS independent path 錯誤

    More than one file was found with OS independent path 'lib/armeabi/libmrpoid.so',. 翻譯過來就是:在操作系統的獨立目錄 ...

随机推荐

  1. 一条bash命令,清除指定的网络接口列表

    在K8S的安装配置过程, 由于不断的测试, 会不断的生成各式各样的虚拟网络接口. 那么,不重新安装之前,清除前次产生的这些垃圾接口, 不让它们影响下次的测试,是很有必要的. 如何快速删除呢? 如下命令 ...

  2. Hadoop数据分析平台项目实战(基于CDH版本集群部署与安装)

    1.Hadoop的主要应用场景: a.数据分析平台. b.推荐系统. c.业务系统的底层存储系统. d.业务监控系统. 2.开发环境:Linux集群(Centos64位)+Window开发模式(win ...

  3. webpack学习笔记--配置devServer

    devServer 1-6 使用DevServer 介绍过用来提高开发效率的 DevServer ,它提供了一些配置项可以改变 DevServer 的默认行为. 要配置 DevServer ,除了在配 ...

  4. [转] 三种方法实现js跨域访问

    1.基于iframe实现跨域 基于iframe实现的跨域要求两个域具有aa.xx.com,bb.xx.com这种特点,也就是两个页面必须属于一个基础域(例如都是xxx.com,或是xxx.com.cn ...

  5. [转] 组件库按需加载 借助babel-plugin-import实现

    前段时间一直在基于webpack进行前端资源包的瘦身.在项目中基于路由进行代码分离,http://www.cnblogs.com/legu/p/7251562.html.对于公司内部的组件库,所有内容 ...

  6. openpose pytorch代码分析

    github: https://github.com/tensorboy/pytorch_Realtime_Multi-Person_Pose_Estimation # -*- coding: utf ...

  7. 用webstorm搭建vue项目

    本文只针对新手. 首先要明白几个名词(概念). Node.js: Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 ...

  8. 浅析H5图片上传

    概述 最近需求上需要实现图片上传的功能,简单记录下实现过程.目前实现的功能比较简单,主要有以下几点: 图片预览 图片删除 拖拽上传 压缩上传 移动端实现方案:使用File API 主要使用到 File ...

  9. jQuery插件学习之选项卡Tab

    在网站开发中经常会用到选项卡功能,为了节省一下写代码时间,封装了一下tab插件,方便调用. 来看一下效果: tab-1 tab-2 tab-3 tabs-1-panel tabs-2-panel ta ...

  10. hdfs基本操作

    hdfs基本操作 1.查询命令 hadoop dfs -ls /   查询/目录下的所有文件和文件夹 hadoop dfs -ls -R 以递归的方式查询/目录下的所有文件 2.创建文件夹 hadoo ...