Codeforces Gym 100650B Countdown DFS

题目链接:

http://codeforces.com/gym/100650/attachments

题意:

给你n个父子关系,然后让你求出前三多的第d代儿子的人是谁

题解:

建一棵树。dfs搞一搞

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define MS(a) memset(a,0,sizeof(a))
 5 #define MP make_pair
 6 #define PB push_back
 7 const int INF = 0x3f3f3f3f;
 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
 9 inline ll read(){
10     ll x=0,f=1;char ch=getchar();
11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
12     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
13     return x*f;
14 }
15 //
16 const int maxn = 1e3+10;
17 
18 string s1,s2;
19 vector<int> E[maxn];
20 map<string,int> mp;
21 int tot;
22 
23 struct node{
24     string name;
25     int num;
26     bool operator<(const node &rhs)const{
27         if(num == rhs.num)
28             return name < rhs.name;
29         return num > rhs.num;
30     }
31 }fam[maxn];
32 
33 int getID(string s){
34     if(mp[s]==0){
35         mp[s] = ++tot;
36         fam[tot].name = s;
37         fam[tot].num = 0;
38     }
39     return mp[s];
40 }
41 
42 int solve(int x,int d){
43     if(d == 0)
44         return 1;
45 
46     int ans = 0;
47     for(int i=0; i<(int)E[x].size(); i++){
48         ans += solve(E[x][i],d-1);
49     }
50     return ans;
51 }
52 
53 int main(){
54     int T=read();
55     for(int cas=1; cas<=T; cas++){
56         if(cas!=1) cout << endl;
57         mp.clear();
58         for(int i=0; i<=maxn; i++) E[i].clear();
59         tot = 0;
60         int n=read(),d=read();
61         for(int i=0; i<n; i++){
62             cin >> s1;
63             int k = read();
64             for(int j=0; j<k; j++){
65                 cin >> s2;
66                 E[getID(s1)].push_back(getID(s2));
67             }
68         }
69 
70         for(int i=1; i<=tot; i++){
71             fam[i].num = solve(i,d);
72         }
73         sort(fam+1,fam+1+tot);
74 
75         int i,tmp=0;
76         // for(int i=1; i<=tot; i++){
77         //  cout << fam[i].name << " " << fam[i].num << endl;
78         // }
79         cout << "Tree " << cas << ":\n";
80         for(i=1; i<=min(3,tot); i++){
81             if(fam[i].num == 0) break;
82             cout << fam[i].name << " " << fam[i].num << endl;
83             tmp = fam[i].num;
84         }
85         // cout << "oo " << tmp << endl;
86         for(int j=i; j<=tot; j++){
87             if(fam[j].num<tmp || fam[j].num==0) break;
88             cout << fam[j].name << " " << fam[j].num << endl;
89         }
90     }
91 
92     return 0;
93 }

 

转载于:https://www.cnblogs.com/yxg123123/p/6827676.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值