我是靠谱客的博主 漂亮山水,这篇文章主要介绍UVa 540 - Team Queue,现在分享给大家,希望可以做个参考。

題目:多級隊列;排隊打飯,如果當一個隊伍裡面有自己團隊的人,就可以插隊到團隊後面;求出隊序列。

分析:數據結構、并查集。自己實現多級隊列的數據結構。利用鏈錶實現多級隊列的數據機構。

             

            定義兩種結構:1鏈錶頭節點,2鏈錶內節點;

            相同團隊,用一個鏈錶維護,為了方便查找,使用hash;

            鏈錶頭結點:數據域value存儲hash值,指向下一個團隊鏈錶的next指針,

                                    指向本團隊的隊首指針first,指向團隊的隊尾指針last;

            鏈錶內節點:數據域value存數數據值,指向下一個團隊成員的next指針;

            總體利用一個root(鏈錶頭節點)節點,具體操作看代碼吧;

            這裡還是用了并查集構造團隊集合;

說明:這個數據結構可以加入回收節點的優化,以後有時間可以把這個封裝一下╮(╯▽╰)╭。

复制代码
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
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> using namespace std; //union_set int sets[1000001]; int rank[1000001]; void set_inital(int a, int b) { for (int i = a; i <= b; ++ i) { rank[i] = 0; sets[i] = i; } } int set_find(int a) { if (a != sets[a]) sets[a] = set_find(sets[a]); return sets[a]; } void set_union(int a, int b) { if (rank[a] < rank[b]) sets[a] = b; else { if (rank[a] == rank[b]) rank[a] ++; sets[b] = a; } } //end_union_set //multilevel_queue typedef struct _queuelist { int first, last; int value; int next; }queuelist; queuelist mlList[10001], mlRoot; typedef struct _queuenode { int value; int next; }queuenode; queuenode mlNode[1000001]; int hashlist[1000001]; int mlnodesize; int mllistsize; void mlqueue_iniital() { memset(hashlist, -1, sizeof(hashlist)); memset(mlList, 0, sizeof(mlList)); memset(mlNode, 0, sizeof(mlNode)); mlnodesize = 1; mllistsize = 1; mlRoot.first = mlRoot.last = 0; } void mlqueue_enqueue(int id, int value) { if (hashlist[id] == -1) {//不存在同类,创建对应的链表,放在链表列的最后并 hashlist[id] = mllistsize; mlList[hashlist[id]].first = mlnodesize; mlList[mlRoot.last].next = mllistsize; mlRoot.last = mllistsize ++; mlList[mlRoot.last].value = id; if (mlRoot.first == 0)//第一个节点 mlRoot.first = mlRoot.last; } //已存在同类,插入对应的链表末端 int ListIndex = hashlist[id]; mlNode[mlnodesize].value = value; mlNode[mlList[ListIndex].last].next = mlnodesize; mlList[ListIndex].last = mlnodesize ++; } int mlqueue_dequeue() { //没有元素 if (mlRoot.first == 0) return -1; //删除第一个链表的第一个元素 int ListIndex = mlRoot.first; int value = mlNode[mlList[ListIndex].first].value; mlList[ListIndex].first = mlNode[mlList[ListIndex].first].next; //链表只有一个元素,删除链表 if (mlList[ListIndex].first == 0) { hashlist[mlList[ListIndex].value] = -1; mlRoot.first = mlList[mlRoot.first].next; //唯一元素,清空队列的链表 if (mlRoot.first == 0) { mlRoot.last = 0; } } return value; } //end_multilevel_queue int main() { int cases = 1, t, m;//read team queue while (~scanf("%d",&t) && t) { //read data int buf[1001]; set_inital(0, 999999); while (t --) { scanf("%d%d",&m,&buf[0]); for (int i = 1; i < m; ++ i) { scanf("%d",&buf[i]); } for (int i = 1; i < m; ++ i) { set_union(set_find(buf[0]), set_find(buf[i])); } } //deal data int id; char command[20]; mlqueue_iniital(); printf("Scenario #%dn",cases ++); while (~scanf("%s",command) && strcmp(command, "STOP")) { if (!strcmp(command, "ENQUEUE")) { scanf("%d",&id); mlqueue_enqueue(set_find(id), id); } if (!strcmp(command, "DEQUEUE")) { printf("%dn",mlqueue_dequeue()); } } puts(""); } return 0; }


最后

以上就是漂亮山水最近收集整理的关于UVa 540 - Team Queue的全部内容,更多相关UVa内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(113)

评论列表共有 0 条评论

立即
投稿
返回
顶部