浙大PAT甲級 1090 廣搜

廣搜求層次,然後進行排序求出最大層次以及在最大層次結點的數目。

AC代碼:

#include<iostream>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<list>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define ll long long
#define inf 999999999
using namespace std;
struct node
{
    int id;
    int lay;
};
vector<int> v[100000];
vector<node> vv;
bool cmp(node x,node y)
{
    return x.lay>y.lay;
}
void bfs(int x)
{
    queue<node> q;
    node tmp;
    tmp.id=x;
    tmp.lay=0;
    q.push(tmp);
    vv.push_back(tmp);
    while(!q.empty())
    {
        node rr=q.front();
        q.pop();
        for(int i=0;i<v[rr.id].size();i++)
        {
            node gg;
            gg.id=v[rr.id][i];
            gg.lay=rr.lay+1;
            q.push(gg);
            vv.push_back(gg);
        }
    }
}
int main()
{
      int n;
      double p,r;
      scanf("%d %lf %lf",&n,&p,&r);
      for(int i=0;i<n;i++)
      {
          int d;
          scanf("%d",&d);
          v[d].push_back(i);
      }
      bfs(-1);
     sort(vv.begin(),vv.end(),cmp);
     int ans=1;
     for(int i=1;i<vv.size();i++)
     {
         if(vv[i].lay==vv[0].lay)
         {
             ans++;
         }
     }
     double tt=(100+r)/100;
     double hh=p*pow(tt,vv[0].lay-1);
     printf("%.2lf %d",hh,ans);
}


發佈了91 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章