HDU-4607 Park Visit bfs | DP | dfs
2013-07-23 22:33
429 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607
首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m。如果m>k+1,那么最长链上还有其他分支,来回走一遍,因此答案为2*m-k-1。。。求最长链可以DP,两次BFS或者DFS等。。
首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m。如果m>k+1,那么最长链上还有其他分支,来回走一遍,因此答案为2*m-k-1。。。求最长链可以DP,两次BFS或者DFS等。。
//STATUS:C++_AC_453MS_3524KB #include <functional> #include <algorithm> #include <iostream> //#include <ext/rope> #include <fstream> #include <sstream> #include <iomanip> #include <numeric> #include <cstring> #include <cassert> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; //using namespace __gnu_cxx; //define #define pii pair<int,int> #define mem(a,b) memset(a,b,sizeof(a)) #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define PI acos(-1.0) //typedef typedef __int64 LL; typedef unsigned __int64 ULL; //const const int N=100010; const LL INF=0x3f3f3f3f; const int MOD=1000000007,STA=8000010; const LL LNF=1LL<<60; const double EPS=1e-8; const double OO=1e15; const int dx[4]={-1,0,1,0}; const int dy[4]={0,1,0,-1}; const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //Daily Use ... inline int sign(double x){return (x>EPS)-(x<-EPS);} template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;} template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;} template<class T> inline T lcm(T a,T b,T d){return a/d*b;} template<class T> inline T Min(T a,T b){return a<b?a:b;} template<class T> inline T Max(T a,T b){return a>b?a:b;} template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);} template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);} template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));} template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));} //End struct Edge{ int u,v; }e[N*2]; int first ,next[N*2],d ; int Ca,T,n,m,mt; void adde(int a,int b) { e[mt].u=a;e[mt].v=b; next[mt]=first[a];first[a]=mt++; e[mt].u=b;e[mt].v=a; next[mt]=first[b];first[b]=mt++; } int bfs(int s) { int u,i,hig; mem(d,0); d[s]=1; queue<int> q; q.push(s); hig=-1; while(!q.empty()) { u=q.front();q.pop(); for(i=first[u];i!=-1;i=next[i]){ if(!d[e[i].v]){ d[e[i].v]=d[e[i].u]+1; if(d[e[i].v]>hig){ hig=d[e[i].v]; T=e[i].v; } q.push(e[i].v); } } } return hig; } int main() { // freopen("in.txt","r",stdin); int i,j,a,b,hig; scanf("%d",&Ca); while(Ca--) { scanf("%d%d",&n,&m); mem(first,-1);mt=0; for(i=1;i<n;i++){ scanf("%d%d",&a,&b); adde(a,b); } bfs(1); hig=bfs(T); while(m--){ scanf("%d",&a); if(a>hig){ printf("%d\n",2*a-hig-1); } else printf("%d\n",a-1); } } return 0; }
相关文章推荐
- hdu 4607 ( Park Visit )
- hdu 4607 Park Visit
- hdu - 4607 - Park Visit
- hdu1428 记忆化搜索(BFS预处理最短路径和+DP+DFS)
- hdu-4607-Park Visit
- HDU 4607 Park Visit【思维】【图论】【搜索】
- 2013 多校联合 H Park Visit (hdu 4607)
- hdu 3681 Prison Break (状态压缩dp/dfs + bfs)
- Hdu 4607 Park Visit【思维+求树的最长链】好题!好题~
- 【hdu 1241】 Oil Deposits (dfs+bfs)
- HDU 1254推箱子(bfs+dfs)
- HDU 1241/UVa572 Oil Deposits(DFS&BFS)
- 【HDU 1241 Oil Deposits】 + DFS || BFS
- BFS+DFS(hdu 1044)
- HDU 2746 ——Max Sum Plus Plus Plus 【dfs+带备忘 或 DP】
- hdu 4771 Stealing Harry Potter's Precious(DFS+BFS)
- HDU_4771_Stealing Harry Potter's Precious(BFS+DFS)(13杭州现场赛B题)
- HDU1241_Oil Deposits(BFS)+(DFS)
- hdu 1242BFS 不过+DFS过
- HDU 1241 Oil Deposits (DFS+BFS)