poj 3107 Godfather
来源:互联网 发布:tensorflow与spark 编辑:程序博客网 时间:2024/05/17 04:35
刚看到这道题 啊
水水的树形dp啊
TLE
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;#define maxx 50050int N;int T;vector <int>root[maxx];bool vis[maxx];int num[maxx];int dp[maxx];int dfs(int x){ int n=root[x].size(); num[x]=1; vis[x]=1; for(int i=0;i<n;i++) { if(!vis[root[x][i]]) num[x]+=dfs(root[x][i]); } return num[x];}void cal (int x){ vis[x]=1; int n=root[x].size(); for(int i=0;i<n;i++) { int k=root[x][i]; if(vis[k]) { dp[x]=max(dp[x],N-num[x]); } else { dp[x]=max(dp[x],num[k]); cal(k); } }}int main (){ int a,b; while(scanf("%d",&N)==1){ for(int i=0;i<N;i++) root[i].clear(); for(int i=1;i<N;i++) { scanf("%d%d",&a,&b); root[a].push_back(b); root[b].push_back(a); } memset(num,0,sizeof(num)); memset(dp,0,sizeof(dp)); memset(vis,0,sizeof(vis)); dfs(1); memset(vis,0,sizeof(vis)); cal(1); int k; int ans=100000000; for(int i=1;i<=N;i++) { if(ans>=dp[i]) { ans=dp[i]; } } int h=0; int bb[maxx]; for(int i=1;i<=N;i++) { if(dp[i]==ans) { bb[h]=i; h++; } } for(int j=0;j<h;j++) { if(j==h-1) printf("%d\n",bb[j]); else printf("%d ",bb[j]); } }}想不通啊 怎末班 后来发现AC的代码全部都是邻接表写的
这怎末班啊 啊啊啊啊啊
不会啊
学吧
AC代码如下
这道题的思路和poj 1655 poj 2378没差的 改了改代码
只要你找到这个树的重心把它输出来就好了(可能存在多个)
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node{ int xx,next;}g[100050];int cnt;int num[50050];int dp[50050];int head[50050];bool vis[50050];void add(int u,int v){ g[cnt].next=head[u]; g[cnt].xx=v; head[u]=cnt++;}int N,ans;int dfs(int x){ vis[x]=1; num[x]=1; for ( int i = head [ x] ; i!=-1 ; i = g [ i ] . next ) { int k=g[i].xx; if(!vis[k]) { num[x]+=dfs(k); dp[x]=max(dp[x],num[k]); } } dp[x]=max(dp[x],N-num[x]); ans=min(ans,dp[x]); return num[x];}int main (){ scanf("%d",&N); int a,b; memset(dp,0,sizeof(dp)); memset(num,0,sizeof(num)); memset(vis,0,sizeof(vis)); memset(head,-1,sizeof(head)); cnt=0; for(int i=1;i<N;i++) { scanf("%d%d",&a,&b); add(a,b); add(b,a); } ans=0x7fffffff; dfs(1); for(int i=1;i<=N;i++) { if(dp[i]==ans) printf("%d ",i); } puts("");}
0 0
- POJ 3107 Godfather(DFS)
- POJ 3107 - Godfather
- POJ 3107 Godfather
- POJ 3107 Godfather
- POJ 3107 Godfather
- POJ 3107 Godfather
- poj 3107 Godfather
- poj 3107 Godfather
- poj 3107 Godfather
- poj 3107 Godfather
- POJ 3107 Godfather
- Godfather POJ - 3107
- POJ 3107 Godfather 笔记
- POJ-3107-Godfather
- [POJ] 3107 Godfather
- POJ 3107 Godfather 树状DP
- POJ 3107 Godfather(树形dp)
- Godfather - POJ 3107 树形dp
- java,android中https请求调用使用安全的方式
- 使用数据库作为Policy Store
- poj 1953 World Cup Noise[斐波那契数列](递归及递推问题系列)
- 使用userGrantedResource和userGrantedPermission表达式
- smarty缓存技术
- poj 3107 Godfather
- ~这些年,我们一起学过的java~08~线程之Thread的基本运用~
- 使用SecurityContext.getUserRoles()获取用户的角色和组信息
- 很特别的一个动态规划入门教程
- Tree组件使用指南之一:创建
- LeetCode | Sort List
- 用linux的命令从多个文件中统计ip的个数
- Tree组件使用指南之二:点击树节点刷新表单
- hdoj 3784 继续xxx定律