POJ3107&&POJ1655【树形DP/简单搜索】
来源:互联网 发布:黑暗圣斗士知乎 编辑:程序博客网 时间:2024/06/05 15:48
写完1655无聊翻了翻discuss,然后顺便改改过了3107…
定义一个节点的节点的平衡值为删除这个节点后产生分支连通块元素最大的数量。
POJ3107题意:
给一颗n个结点的树,节点编号为1~n,
可能有多种方案,按编号顺序输出。
POJ1655题意:
如果多种方案只取最小节点编号并输出这个值。
思路:
dp[node] 代表以 node 为根的子树的节点数量。
答案:
然后每次取个小值就好了。
POJ3107Code:
//#include <bits/stdc++.h>#include<iostream>#include<vector>#include<cstdio>#include<string.h>#include<algorithm>using namespace std;typedef pair<int,int> PII;typedef long long LL;//#pragma comment(linker, "/STACK:102400000,102400000")const int N=5e4+10;struct Edge{ int v; int next;} edge[N<<1];int head[N],tol;int n;void init(){ tol=0; memset(head,-1,sizeof(head));}void add(int u,int v){ edge[tol].v=v; edge[tol].next=head[u]; head[u]=tol++;}bool vis[N];int ans,dp[N];vector<int>node;void DFS(int u){ int temp1,temp2,v; int Min = 0,sum=0; dp[u]=1; for(int i=head[u];~i;i=edge[i].next){ v = edge[i].v; if(vis[v]) continue; vis[v]=true; DFS(v); Min = max(Min,dp[v]); sum += dp[v]; } dp[u] = sum + dp[u]; temp1 = n - dp[u]; if(Min) temp2 = Min; temp1 = max(temp1,temp2); if(ans > temp1){ ans = temp1; node.clear(); node.push_back(u); } else if(ans == temp1) node.push_back(u);}void input(){ int u,v; scanf("%d",&n); init(); for(int i=1; i<n; i++){ scanf("%d%d",&u,&v); add(u,v); add(v,u); }}void solve(){ memset(vis,false,sizeof(vis)); vis[1]=true; ans = n; node.clear(); DFS(1); sort(node.begin(),node.end()); int sz = node.size(); for(int i=0;i<sz;i++){ if(i) printf(" "); printf("%d",node[i]); }}int main(){ input(); solve(); return 0;}
阅读全文
0 0
- POJ3107&&POJ1655【树形DP/简单搜索】
- poj3107(树形dp)
- poj1655树形dp
- [POJ3107]Godfather(树形dp)
- POJ3107-God father(树形dp??)
- poj1655 2378 3107 树形dp
- POJ1655:Balancing Act(树形DP)
- poj1655 Balancing Act(树形dp)
- 【树形DP+前向星】 poj3107 Godfather
- poj3107--Godfather(树形DP练习3)
- POJ3107-树的重心&树形DP-Godfather
- 树形dp求树的重心 poj1655
- poj1655 Balancing Act 【树形DP(很弱)】
- poj1655 Balancing Act(树形dp)
- [POJ1655]Balancing Act(树形dp)
- poj1655 Balancing Act(树形dp)
- poj3107、2378、1655(树形DP,割点)
- 【poj1655】【poj3107】【求树的重心】
- 在选择一款web漏洞扫描工具时,你最关心的哪一点?
- android 3种菜单
- 【android】View的事件体系2-滑动方式
- Android MediaCodec硬解码AAC音频文件(实时AAC音频帧)并播放
- JavaScript匿名函数和闭包
- POJ3107&&POJ1655【树形DP/简单搜索】
- lesson7.5FileInpuStreamt和FileWriter类
- 选择结构
- [ElasticSearch]分析之Analysis(分析)
- IO流的使用
- Aruba 云服务代金券
- 导入maven项目
- MySQL数据库(33)
- 二分查找