大一寒假集训 stl&并查集

A - Let the Balloon Rise HDU - 1004   迭代器遍历 

题目大意:给一些气球颜色输出数量最多的气球颜色。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<vector>
 4 #include<map>
 5 using namespace std;
 6 const int maxn = 10000;
 7 int main() {
 8     int n;
 9     while (scanf("%d", &n),n) {
10         map<string, int>mp;
11         char s[100];
12         while (n--) {
13             scanf("%s", s);
14             if (mp.count(s) == 0)
15                 mp.insert(pair<string, int>(s, 1));
16             else mp[s]++;
17         }
18         int max = 0;
19         string str;
20         for (map<string, int>::iterator it = mp.begin(); it != mp.end();++it)
21         {
22             if (it->second > max) {max = it->second;str = it->first;}
23         }
24         cout << str << endl;
25     }
26 }
View Code
 1 #include <iostream>
 2 #include <map>
 3 #include <string>
 4 #include<algorithm>
 5 using namespace std;
 6 int main()
 7 {
 8     //map<string, int> m;
 9     string s;
10     string ans;
11     int n;
12     while (scanf("%d", &n), n) {
13         int max = 0;
14         map<string, int> m;
15         while (n--)
16         {
17             cin >> s;
18             if (m.find(s) == m.end())
19             {
20                 m[s] = 1;
21             }
22             else
23             {
24                 m[s]++;
25             }
26 
27             if (m[s] > max) {
28                 max = m[s];
29                 ans = s;
30             }
31         }
32         cout << ans << endl;
33     }
34     return 0;
35 }
View Code

B - 人见人爱A-B HDU - 2034 

 1 #include <iostream>
 2 #include <map>
 3 #include <string>
 4 #include<algorithm>
 5 #include<vector>
 6 #define maxn 110
 7 using namespace std;
 8 int main()
 9 {
10     int n, m;
11     while (scanf("%d %d",&n,&m),n+m)
12     {
13         int t;
14         vector<int>a,b,c;
15         for (int i = 0;i < n;i++)
16         {
17             cin >> t;
18             a.push_back(t);
19         }
20         for (int i = 0;i < m;i++)
21         {
22             cin >> t;
23             b.push_back(t);
24         }
25         for (int i = 0;i < n;i++)
26         {
27             if (find(b.begin(), b.end(), a[i]) == b.end()) { c.push_back(a[i]); }
28         }
29         sort(c.begin(), c.end());
30         if (c.begin() == c.end())printf("NULL");
31         /*for (vector<int>::iterator it = c.begin();it != c.end();it++) {
32             cout << *it << " ";
33         }两种二选一*/
34         for (int i = 0;i < c.size();i++) {
35             cout << c[i] << " ";
36         }
37         printf("\n");
38     }
39 
40 }
View Code

C - 稳定排序 HDU - 1872    map排序

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<map>
 6 #include<vector>
 7 #include<set>
 8 #include<string>
 9 #include<cmath>
10 #include<cstring>
11 #define INF 0x3f3f3f3f3f3f3f3f
12 using namespace std;
13 const int maxn = 310;
14 struct pe {
15     string s;
16     int score;
17     int xu;
18 }per[maxn];
19 bool cmp(pe a, pe b) {
20     if (a.score != b.score) return a.score > b.score;
21     if (a.score == b.score && a.xu != b.xu)return a.xu < b.xu;
22 }
23 int main()
24 {
25     int n;
26     string ss;
27     int grade;
28     while (scanf("%d", &n) != EOF) {
29         for (int i = 0;i < n;i++) {
30             cin >> per[i].s >> per[i].score;
31             per[i].xu = i;
32         }
33         sort(per, per + n, cmp);
34         int flag = 1, fl = 1;//flag判断正确,fl判断稳定
35         for (int i = 0;i < n;i++) {
36             cin >> ss >> grade;
37             if (grade != per[i].score) { flag = 0; }
38             if (grade == per[i].score && ss != per[i].s) { fl = 0; }
39         }
40         if (flag == 1 && fl == 1) { printf("Right\n"); }
41         else if (flag == 1 && fl == 0) {
42             printf("Not Stable\n");
43             for (int i = 0;i < n;i++) {
44             cout << per[i].s << " " << per[i].score << "\n";
45             }
46         }
47         else if (flag == 0) {
48             printf("Error\n");
49             for (int i = 0;i < n;i++) {
50             cout << per[i].s << " " << per[i].score << "\n";
51             }
52         }
53     }
54 
55 }
View Code

D - 畅通工程 HDU - 1232 

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<map>
 6 #include<vector>
 7 #include<set>
 8 #include<string>
 9 #include<cmath>
10 #include<cstring>
11 #define INF 0x3f3f3f3f3f3f3f3f
12 using namespace std;
13 const int maxn=1010;
14 int n, m;
15 int father[maxn];
16 void init(int n) {
17     for (int i = 1;i <=n;i++) {
18         father[i] = i;
19     }
20 }
21 int find(int x) {
22     return x == father[x] ? x : father[x] = find(father[x]);
23 }
24 void unite(int x, int y) {
25     int fx = find(x);
26     int fy = find(y);
27     father[fx] = fy;
28 }
29 int main()
30 {
31     while (scanf("%d %d", &n, &m), n) {
32         init(n);
33         int a, b;
34         for (int i = 0;i < m;i++) {
35             scanf("%d %d", &a, &b);
36             unite(a, b);
37         }
38         int ans = 0;
39         for (int i = 1;i <= n;i++) {
40             for (int j = i + 1;j <= n;j++) {
41                 if (find(i) != find(j)) { ans++;unite(i, j); }
42             }
43         }
44         printf("%d\n", ans);
45     }
46 }
View Code

E - Wireless Network POJ - 2236   并查集

题目大意:O表示修复给出数字的电脑,S表示询问两台电脑之间是否联通。给出的数字为坐标。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<map>
 6 #include<vector>
 7 #include<set>
 8 #include<string>
 9 #include<cmath>
10 #include<cstring>
11 #define INF 0x3f3f3f3f3f3f3f3f
12 using namespace std;
13 const int maxn = 1010;
14 int father[maxn];
15 double dis[maxn][maxn];
16 int x[maxn], y[maxn], condition[maxn];
17 int N, d;
18 void init() {
19     for (int i = 1;i <= maxn;i++) {
20         father[i] = i;
21     }
22 }
23 int find(int x) {
24     return x == father[x] ? x : father[x] = find(father[x]);
25 }
26 void unite(int x, int y) {
27     int fx = find(x);
28     int fy = find(y);
29     father[fx] = fy;
30 }
31 double distance(int x, int y, int x1, int y1) {
32     return ((x - x1) * (x - x1) + (y - y1) * (y - y1));
33 }
34 int main()
35 {
36     scanf("%d %d", &N, &d);
37     for (int i = 1;i <= N;i++) {
38         scanf("%d %d", x + i, y + i);
39         condition[i] = 0;
40     }
41     for (int i = 1;i <= N;i++) {
42         for (int j = i + 1;j <= N;j++) {
43             dis[j][i] = dis[i][j] = distance(x[i], y[i], x[j], y[j]);
44         }
45     }
46     init();
47     char s[2];
48     int num1, num2;
49     while (~scanf("%s", s)) {
50         if (s[0] == 'O') {
51             scanf("%d", &num1);
52             if (condition[num1] != 1) {
53                 condition[num1] = 1;
54                 for (int i = 1;i <= N;i++) {
55                     if (condition[i] && dis[i][num1] <= d * d)
56                     {
57                         unite(num1, i);
58                     }
59                 }
60             }
61             else continue;
62         }
63         else {
64             scanf("%d %d", &num1, &num2);
65             if (find(num1) == find(num2))printf("SUCCESS\n");
66             else printf("FAIL\n");
67         }
68     }
69     return 0;
70 
71 }
View Code

F - How Many Tables HDU - 1213  

题目大意:不认识的人不坐一桌。最少要几桌。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<map>
 6 #include<vector>
 7 #include<set>
 8 #include<string>
 9 #include<cmath>
10 #include<cstring>
11 #define INF 0x3f3f3f3f3f3f3f3f
12 using namespace std;
13 const int maxn = 2000;
14 int father[maxn];
15 void init() {
16     for (int i = 1;i <= maxn;i++) {
17         father[i] = i;
18     }
19 }
20 int find(int x) {
21     return x == father[x] ? x : father[x] = find(father[x]);
22 }
23 void unite(int x, int y) {
24     int fx = find(x);
25     int fy = find(y);
26     father[fx] = fy;
27 }
28 int main()
29 {
30     int t;
31     scanf("%d", &t);
32     while (t--) {
33         init();
34         int n, m;
35         scanf("%d %d", &n, &m);
36         while (m--) {
37             int a, b;
38             scanf("%d %d", &a, &b);
39             unite(a, b);
40         }
41         vector<int>v;
42         for (int i = 1;i <= n;i++) {
43             int res = find(i);
44             if (find(v.begin(), v.end(), res) == v.end()) { v.push_back(res); }
45         }
46         printf("%d\n", v.size());
47     }
48 
49 }
View Code

G - 食物链 POJ - 1182 

 1 #include<algorithm>
 2 #include <iostream>
 3 #include<cstdio>
 4 using namespace std;
 5 const int maxn = 150000 + 15;
 6 int parent[maxn];
 7 void init(int n) {
 8     for (int i = 0;i <n;i++) {
 9         parent[i] = i;
10     }
11 }
12 int find(int x) {
13     if (parent[x] == x)
14         return x;
15     else
16         return parent[x] = find(parent[x]);
17 }
18 void unite(int x, int y) {
19     y = find(y);
20     x = find(x);
21     parent[x] = y;
22 
23 }
24 bool same(int x, int y) {
25     return find(x) == find(y);
26 }
27 int n, k;
28 int main() {
29     scanf("%d %d", &n, &k);
30     init(n * 3);
31     int d, x, y;
32     int ans = 0;
33     for (int i = 0;i < k;i++) {
34         scanf("%d%d%d", &d, &x, &y);
35         x = x - 1;y = y - 1;
36         if ((x == y && d == 2) || x >= n || y >= n || x < 0 || y < 0) {//x和y相同不能建立捕食关系,x y超范围 
37             ans++;
38             continue;
39         }
40         if (d == 1) {
41             if (same(x, y + n) || same(x, y + 2 * n))//x和y存在捕食关系 
42                 ans++;
43             else {
44                 unite(x, y);
45                 unite(x + n, y + n);
46                 unite(x + 2 * n, y + 2 * n);
47             }
48         }
49         else {
50             if (find(x) == find(y) || find(x) == find(y + 2 * n))//x和y同级不能建立捕食关系,x和y存在相反的捕食关系 
51                 ans++;
52             else {
53                 unite(x, y + n);
54                 unite(x + n, y + 2 * n);
55                 unite(x + 2 * n, y);
56             }
57         }
58     }
59     printf("%d\n", ans);
60     return 0;
61 }
View Code

H - 看病要排队 HDU - 1873  优先队列

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<map>
 6 #include<vector>
 7 #include<set>
 8 #include<string>
 9 #include<cmath>
10 #include<cstring>
11 #define INF 0x3f3f3f3f3f3f3f3f
12 using namespace std;
13 const int maxn = 1010;
14 struct patient {
15     int id;
16     int rank;
17     friend bool operator<(patient a, patient b) {
18         if (a.rank == b.rank)
19             return a.id > b.id;
20         else
21             return a.rank < b.rank;
22     }
23 }member, member1;
24 int main()
25 {
26     int n;
27     while (~scanf("%d", &n)) {
28         int a, b;
29         char s[10];
30         int num;
31         priority_queue<patient> q[5];
32         int add = 0;
33         while (n--) {
34             scanf("%s", s);
35             if (s[0] == 'I') {
36                 add++;
37                 cin >> a >> member.rank;
38                 member.id = add;
39                 q[a].push(member);
40             }
41             else {
42                 cin >> a;
43                 if (q[a].empty()) {
44                     printf("EMPTY\n");
45                 }
46                 else {
47                     member1 = q[a].top();
48                     q[a].pop();
49                     printf("%d\n", member1.id);
50                 }
51 
52             }
53         }
54     }
55 }
View Code

 

posted @ 2020-02-08 10:00  programmer_w  阅读(1)  评论(0)    收藏  举报