ZOJ3951 : Independent Set

如果知道了树的形态,那么可以树形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;
}

  

时间: 2024-08-06 20:02:39

ZOJ3951 : Independent Set的相关文章

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 than other categories. Free Language Agnostic Programming Books 97 Things Every Programmer Should Know Algorithms and Data-Structures (PDF) Algorithm

写在independent的途中

做开发,算起来,当程序员其实才3四年而已,我并不是什么专业的程序员-大学时软件工程,毕业后参加培训当码农,然后一直就这么几年一下下来. 也许这也叫犯贱,非得找点难事儿折腾自己,不然,总觉得这人生没有什么更深层次的东西,至于一下子把所有精力放在一个微观点上,一下去当什么研究啊,什么学者学到死的东西,我这种人,是死也不会去干的. 要说我会干的,也许还真的就是这么两个字儿-独立. 这,该说是所有人的追求目标吧,久在凡笼里,复得归自在,人生何处不相逢,今朝明朝都是春. 这么说吧,还是想独立,哪天,自己为

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

由于使用了NDK编译的可执行文件在应用中调用,在Android 7.0上的运行情况发现,当运行该可执行文件时,报如下错误: error: only position independent executables (PIE) are supported. PIE这个安全机制从4.1引入,但是Android L之前的系统版本并不会去检验可执行文件是否基于PIE编译出的.因此不会报错.但是Android L已经开启验证,如果调用的可执行文件不是基于PIE方式编译的,则无法运行.解决办法非常简单,在A

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 is in the set, then its children should not be in the set. So that the set has the largest number of nodes. Analysis: Recursion method. Return the LIS

Method of Seamless Integration and Independent Evolution of Information-Centric Networking via Software Defined Networking

A method of transferring data between a software defined network (SDN) and an information-centric network (ICN), wherein the method comprises receiving a request from an SDN node for a specific named content stored on an ICN, wherein the request is e

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

原文网址:http://blog.csdn.net/hxdanya/article/details/39371759 由于使用了NDK编译的可执行文件在应用中调用,在4.4及之前的版本上一直没出问题.最近由于要测试在Android L上的运行情况发现,当运行该可执行文件时,报如下错误: error: only position independent executables (PIE) are supported. PIE这个安全机制从4.1引入,但是Android L之前的系统版本并不会去检验

【转载】独立成分分析(Independent Component Analysis)ICA

独立成分分析(Independent Component Analysis) 1. 问题: 1.上节提到的PCA是一种数据降维的方法,但是只对符合高斯分布的样本点比较有效,那么对于其他分布的样本,有没有主元分解的方法呢? 2.经典的鸡尾酒宴会问题(cocktail party problem).假设在party中有n个人,他们可以同时说话,我们也在房间中一些角落里共放置了n个声音接收器(Microphone)用来记录声音.宴会过后,我们从n个麦克风中得到了一组数据,i表示采样的时间顺序,也就是说

[转]独立成分分析(Independent Component Analysis)

原文地址:http://www.cnblogs.com/jerrylead/archive/2011/04/19/2021071.html 独立成分分析(Independent Component Analysis) 1. 问题: 1.上节提到的PCA是一种数据降维的方法,但是只对符合高斯分布的样本点比较有效,那么对于其他分布的样本,有没有主元分解的方法呢? 2.经典的鸡尾酒宴会问题(cocktail party problem).假设在party中有n个人,他们可以同时说话,我们也在房间中一些

Position Independent Code (PIC) in shared libraries【转载】

I've described the need for special handling of shared libraries while loading them into the process's address space in a previous article. Briefly, when the linker creates a shared library, it doesn't know in advance where it might be loaded. This c