CSU 1908: The Big Escape

来源:互联网 发布:2016淘宝店好做吗 编辑:程序博客网 时间:2024/06/04 19:29

Description

There is a tree-like prison. Expect the root node, each node has a prisoner, and the root node is the only exit. Each node can accommodate a large number of prisoners, but each edge per minute only one prisoner can pass.
Now, the big escape begins, every prisoner wants to escape to the exit.Do you know when the last one escapes from the prison.

Input

There are lots of case.
For each test case.The first line contains two integers n,m(n<=100000, 1<=m<=n), which indicates the number of nodes and the root node.
The next n-1 lines describe the tree.

Output

For each test case, you output one line “Case #%d:%d”

Sample Input

10 21 22 32 42 51 65 73 82 92 10

Sample Output

Case #1:2大意通俗易懂,讲下思路,其实就是道贼水的题,由于各种奇奇怪怪的原因所以写下博客。这道题其实求得时字树的节点个数,为什么呢,对于每个节点,他的子节点一定会堵到这个节点这里(根节点不用考率,因为到根节点就出去了),子节点的个数也就成为了所有人通过的时间也就成为了这些节点逃出去的时间,然后所有的节点都会来到第二层的节点处,所以以第二层节点为根结点计算就行了,比赛的时候用了bfs遍历,结果wa了,后面改成dfs就过了,bfs显然也是可以的,但是dfs更方便点,但是我和我的倔强一定要用bfs过一遍,才发现比赛的时候写的忘记把兄弟节点的数量传过去了,后面改了以后,还是wa了,后面对拍数据才发现数组开小了,因为要用邻接表捡了双向边,所以数组要大一点,比赛的时候也还好没有倔强,比完赛还是倔强点好,下面还是给出dfs的代码吧
#include <iostream>#include<queue>#include<vector>#include<algorithm>#include<functional>#include<string>#include<string.h>#include<cmath>#include<cstdio>using namespace std;const int maxn = 150000+10;int erear,head[maxn];int root,n;int vis[maxn];int cnt[maxn];int ans = -1;int kase = 1;struct node{    int x,to,next;}E[maxn];void __init__(){    memset(head,-1,sizeof(head));    erear = 0;}void build(int x,int y){    E[erear].x = x;    E[erear].to= y;    E[erear].next = head[x];    head[x] = erear++;}int dfs(int root,int &sum){    for(int i = head[root]; i!=-1 ;i = E[i].next)    {        if(vis[E[i].to]==0)        {            vis[E[i].to] = 1;            sum++;            dfs(E[i].to,sum);        }    }}int main(){     //freopen("D:\\C.in","r",stdin); // freopen("D:\\out.txt","w",stdout);    while(~scanf("%d%d",&n,&root))    {        __init__();        ans = 0;        memset(vis,0,sizeof(vis));        memset(cnt,0,sizeof(cnt));        for(int i = 0;i < n-1;i++)        {            int x,y;            scanf("%d%d",&x,&y);            build(x,y);            build(y,x);        }        vis[root] = 1;       for(int i = head[root];i!=-1  ;i=E[i].next)       {           //cout<<i<<' '<<E[i].to<<endl;           int sum = 1;           vis[E[i].to] = 1;            dfs(E[i].to,sum);            ans = max(sum,ans);            /*if(kase == 10003)            cout<<sum<<endl;*/       }        cout<<"Case #"<<kase++<<":"<<ans<<endl;    }    //fclose(stdin);//关闭文件//fclose(stdout);    return 0;}


0 0
原创粉丝点击