Repository navigation
Expand file tree
/
Copy pathCDOWN.cpp
More file actions
189 lines (182 loc) · 4.57 KB
/
Copy pathCDOWN.cpp
File metadata and controls
189 lines (182 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<set>
#include<vector>
#include<algorithm>
#include<functional>
#include<map>
#include<string>
#include<cstring>
#include<utility>
#include<climits>
#include<queue>
#include<deque>
#define mp make_pair
#define pb push_back
#define REP(i,a,b) for(i=a;i<=b;i++)
#define FOR(i,a,b) for(i=a;i<b; i++)
#define sci(x) scanf("%d",&x)
#define sc2i(x,y) scanf("%d%d",&x,&y)
#define scli(x) scanf("%I64d", &x)
#define scl2i(x,y) scanf("%I64d%I64d",&x,&y)
#define debug(n) cout<<__LINE__<<" #n "<<n<<endl
#define debug2(a,b) cout<<__LINE__<<" #a "<<a<<" #b "<<b<<endl
#define debug3(a,b,c) cout<<__LINE__<<" #a "<<a<<" #b "<<b<<" #c "<<c<<endl
#define fill(a,x) memset(a,x,sizeof(a))
#define trace(x) cout<<__FUNCTION__<<" : "<<__LINE__<<" #x "<<x<<endl
#define trace2(x,y) cout<<__FUNCTION__<<" : "<<__LINE__<<" #x "<<x<<" #y "<<y<<endl
#define tracea(x,a,b) cout<<__FUNCTION__<<" : "<<__LINE__<<" #x "; FOR(i,a,b) cout<<x[i]<<" , " ; cout<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned int ui;
typedef long double ld;
typedef vector<int> VI;
typedef vector<long> VL;
typedef vector<long long> VLL;
inline int scan()
{
register int n=0;
register int sign=1;
register char c=getchar_unlocked();
while( (c<'0' || c>'9') && c!='-' )
c=getchar_unlocked();
if( c=='-' )
{
sign=-1;
c=getchar_unlocked();
}
while( c>='0' && c<='9' )
{
n=n*10+c-'0';
c=getchar_unlocked();
}
return n;
}
int rgraph[10000]; // the map from child to parent
int COUNT[10000];
//map<int,set<int> > graph; // the map from parent to all its children
map<string,int> nmap ; // mapping name to the index
map<int,string> rev_nmap; // map the index to the name
set<string> ppls; // the set of found people
set<string>::iterator sit ; // the iterator for the set
struct ps
{
int count;
int idx;
};
bool ps_compare(ps lhs,ps rhs ) { return lhs.count > rhs.count ; }
int main()
{
ps pstruct[10000];
register int i,j;
int t,tcount=1;
string stmp1,stmp2,stmp3;
int tmp1,tmp2,tmp3,idx;
int tt;
int c;
sci(tt);
while(tt--)
{
// cout<<"New test case : "<<endl;
idx=0;
fill(rgraph,-1);
// graph.clear();
nmap.clear(); rev_nmap.clear(); ppls.clear();
sc2i(c,t);
while(c--)
{
cin>>stmp1;
if( ppls.find(stmp1)==ppls.end() ) // i.e. not found
{
ppls.insert(stmp1);
nmap[stmp1]=idx;
rev_nmap[idx]=stmp1;
// graph[idx]=set<int>();
idx++;
}
cin>>tmp1; // the count of what to follow
// cout<<"read edges from : "<<stmp1<<endl;
while( tmp1-- )
{
cin>>stmp2;
if( ppls.find(stmp2)==ppls.end() )
{
ppls.insert(stmp2);
nmap[stmp2]=idx;
rev_nmap[idx]=stmp2;
idx++;
}
rgraph[nmap[stmp2]]=nmap[stmp1];
// cout<<" the child : "<<stmp2<<" : "<<nmap[stmp2]<<" : "<<nmap[stmp1]<<endl;
}
}
// input complete now start the count
// debug
/* cout<<"total people identified : "<<idx<<endl;
cout<<"list of edges :"<<endl;
FOR(i,0,idx)
if( rgraph[i]>0 ) // if there is a parent
cout<<rev_nmap[i]<<" : to : "<<rev_nmap[rgraph[i]]<<endl;
*/
fill(COUNT,0);
if( t==1 ) // i.e. count the parents
{
FOR(i,0,idx)
{
if( rgraph[i]>=0 ) COUNT[rgraph[i]]++;
}
}
else if( t==2 )
{
FOR(i,0,idx)
{
if( rgraph[i]>=0 && rgraph[rgraph[i]] >=0 ) COUNT[rgraph[rgraph[i]]]++;
}
}
FOR(i,0,idx)
{
pstruct[i].idx=i;
pstruct[i].count=COUNT[i];
}
sort(pstruct,pstruct+idx,ps_compare);
// the first 3 are in our category
printf("Tree %d:\n",tcount);
// cout<<"Tree "<<tcount<<endl;
/**
FOR(i,0,3)
{
if( i<idx )
{
cout<<rev_nmap[pstruct[i].idx]<<" "<<pstruct[i].count<<endl;
}
}
**/
int outcount=0;
int curr_idx=0; // the pstruct to consider next
int curr_cnt;
set<string> the_set; // the set holding the similar
while(1)
{
curr_cnt=pstruct[curr_idx].count;
if( curr_cnt== 0 ) break; // no need to print
the_set.clear();
while(pstruct[curr_idx].count==curr_cnt)
{
the_set.insert(rev_nmap[pstruct[curr_idx].idx]);
curr_idx++;
}
outcount+=the_set.size();
for(sit=the_set.begin();sit!=the_set.end();sit++)
{
printf("%s %d\n",(*sit).c_str(),curr_cnt);
// cout<<*sit<<" "<<curr_cnt<<endl;
}
if( outcount>=3 )
break;
}
tcount++;
}
return 0;
}