题目链接:
http://codeforces.com/gym/100650/attachments
题意:
给你n个父子关系,然后让你求出前三多的第d代儿子的人是谁
题解:
建一棵树。dfs搞一搞
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//
const int maxn = 1e3+10;
string s1,s2;
vector<int> E[maxn];
map<string,int> mp;
int tot;
struct node{
string name;
int num;
bool operator<(const node &rhs)const{
if(num == rhs.num)
return name < rhs.name;
return num > rhs.num;
}
}fam[maxn];
int getID(string s){
if(mp[s]==0){
mp[s] = ++tot;
fam[tot].name = s;
fam[tot].num = 0;
}
return mp[s];
}
int solve(int x,int d){
if(d == 0)
return 1;
int ans = 0;
for(int i=0; i<(int)E[x].size(); i++){
ans += solve(E[x][i],d-1);
}
return ans;
}
int main(){
int T=read();
for(int cas=1; cas<=T; cas++){
if(cas!=1) cout << endl;
mp.clear();
for(int i=0; i<=maxn; i++) E[i].clear();
tot = 0;
int n=read(),d=read();
for(int i=0; i<n; i++){
cin >> s1;
int k = read();
for(int j=0; j<k; j++){
cin >> s2;
E[getID(s1)].push_back(getID(s2));
}
}
for(int i=1; i<=tot; i++){
fam[i].num = solve(i,d);
}
sort(fam+1,fam+1+tot);
int i,tmp=0;
// for(int i=1; i<=tot; i++){
// cout << fam[i].name << " " << fam[i].num << endl;
// }
cout << "Tree " << cas << ":\n";
for(i=1; i<=min(3,tot); i++){
if(fam[i].num == 0) break;
cout << fam[i].name << " " << fam[i].num << endl;
tmp = fam[i].num;
}
// cout << "oo " << tmp << endl;
for(int j=i; j<=tot; j++){
if(fam[j].num<tmp || fam[j].num==0) break;
cout << fam[j].name << " " << fam[j].num << endl;
}
}
return 0;
}