hdu3290 DFS
来源:互联网 发布:java认证考试报名 编辑:程序博客网 时间:2024/06/06 00:36
也算树形dp?~~
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN=30000;const int MAXM=50000;struct node{ int u,v;};node edge[MAXM];int first[MAXN],next[MAXM];int cc;int num[MAXN];int child_num[MAXN];int in[MAXN];inline void add_edge(int u,int v){ edge[cc].u=u; edge[cc].v=v; next[cc]=first[u]; first[u]=cc; cc++;}void dfs(int u){ if(num[u]!=-1) return ; int i; int k=0; int *temp=new int[MAXN]; for(i=first[u];i!=-1;i=next[i]) { int v=edge[i].v; dfs(v); temp[k++]=num[v]; } sort(temp,temp+k); num[u]=temp[(child_num[u]+1)/2-1]; delete temp; return ;}void Scan(int &num) //对G++使用{ char in; bool neg=false; while(((in=getchar()) > '9' || in<'0') && in!='-') ; if(in=='-') { neg=true; while((in=getchar()) >'9' || in<'0'); } num=in-'0'; while(in=getchar(),in>='0'&&in<='9') num*=10,num+=in-'0'; if(neg) num=0-num;}int main(){ int n; while(scanf("%d",&n)!=EOF) { int i; memset(first,-1,sizeof(first)); memset(next,-1,sizeof(next)); cc=0; memset(num,-1,sizeof(num)); memset(in,0,sizeof(in)); memset(child_num,0,sizeof(child_num)); for(i=1;i<=n;i++) { int p; Scan(p); child_num[i]=p; if(p==0) num[i]=i; int j; for(j=0;j<p;j++) { int x; Scan(x); in[x]++; add_edge(i,x); } } for(i=1;i<=n;i++) if(in[i]==0) { dfs(i); break; } printf("%d\n",num[i]); } return 0;}
- hdu3290 DFS
- 09-06 HDU_Steps4.3 DFS剪枝 HDU1010 HDU1016 HDU1426 HDU1455 HDU2510 HDU2553 HDU3290 HDU2616
- hdu3290 The magic apple tree
- DFS
- DFS
- dfs
- dfs
- dfs
- dfs
- DFS
- DFS
- dfs
- DFS
- DFS
- DFS
- dfs
- DFS
- dfs
- 中移动TD-LTE及对终端的要求
- webservice的定义、描述和用途详解
- 一个fork的面试题
- HDU 1085
- exit、check、stop和return
- hdu3290 DFS
- thinkphp 前台html调用函数 格式化输出
- hdu1238 Substrings (暴力)
- php 根据url自动生成缩略图
- 二叉树迭代器算法
- Andriod 环境配置以及第一个Android Application Project
- 寻找单链表的中间节点(可能包含环)
- MySQL存储过程错误No data - zero rows fetched, selected, or processed
- 编程之魂:与27位编程语言创始人对话