Use priority_queue to simulate :
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 const int MAXN = 10000+10; 7 using namespace std; 8 9 typedef struct{ 10 string name; 11 int rp; 12 }node; 13 14 bool operator <( node x, node y ){ 15 if(x.rp>y.rp) return 1; 16 else if(x.rp==y.rp && x.name<y.name) return 1; 17 return 0; 18 } 19 20 priority_queue <node> q[MAXN]; 21 22 int n,m; 23 node anode; 24 int main() 25 { 26 while(scanf("%d %d",&n,&m)!=EOF){ 27 int t; 28 for(int i=1;i<=n;i++){ 29 while(!q[i].empty()) q[i].pop(); 30 scanf("%d",&t); 31 while(t--){ 32 cin>>anode.name>>anode.rp; 33 q[i].push(anode); 34 } 35 } 36 37 string cmd; 38 int a,b,c; 39 for(int i=1;i<=m;i++){ 40 cin>>cmd; 41 if(cmd=="GETON"){ 42 cin>>a>>anode.name>>anode.rp; 43 q[a].push(anode); 44 } 45 else if(cmd=="JOIN") { 46 scanf("%d %d",&a,&b); 47 while(!q[b].empty()) { 48 q[a].push(q[b].top()); 49 q[b].pop(); 50 } 51 } 52 else { 53 scanf("%d",&a); 54 cout<<q[a].top().name<<endl; 55 q[a].pop(); 56 } 57 } 58 } 59 60 61 62 return 0; 63 }