Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
....这题20%的MLE都是我贡献的迷之广搜MLE #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<functional> using namespace std; int n,p; int head[160],to[310],nxt[310],vc; int fa[160],bfso[160],bfsn[160],dl[160],dr[160],dfc; struct Q{ char c,rt[51],sz[51]; bool operator <(const Q &b)const {return this->c>b.c;} }qt,qtg;; priority_queue<Q>q; void dfs(int k) { dl[k]=++dfc; for(int i=head[k];i;i=nxt[i]) { if(to[i]!=fa[k]) { fa[to[i]]=k; dfs(to[i]); } } dr[k]=dfc; } void bfs() { int h=0,t=1,q[1000];q[1]=1; while(h<t) { h++; bfsn[q[h]]=h;bfso[h]=q[h]; for(int i=head[q[h]];i;i=nxt[i]) { if(to[i]!=fa[q[h]]) { t++; q[t]=to[i]; } } } } void init() { scanf("%d%d",&n,&p); int f,t; for(int i=1;i<=n-1;i++) { scanf("%d%d",&f,&t); to[++vc]=t; nxt[vc]=head[f]; head[f]=vc; to[++vc]=f; nxt[vc]=head[t]; head[t]=vc; } dfs(1);bfs(); } int main() { init(); if(p==n){printf("0");return 0;} qt.c=1;qt.rt[1]=1;qt.sz[1]=dr[1]-dl[1]+1; q.push(qt); while(!q.empty()) { qtg=q.top();q.pop(); for(int i=bfsn[qtg.rt[qtg.c]]+1;i<=n;i++) { int no=bfso[i]; qt=qtg; qt.c++; qt.rt[qt.c]=no; qt.sz[qt.c]=dr[no]-dl[no]+1; int j; for(j=qtg.c;j>=1;j--) if(dl[no]>=dl[qtg.rt[j]]&&dl[no]<=dr[qtg.rt[j]])break; qt.sz[j]-=qt.sz[qt.c]; if(qt.sz[j]==p||qt.sz[qt.c]==p) { printf("%d",qt.c-1); return 0; } q.push(qt); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator