[HDOJ5510]Bazinga(并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510

普通集合会tle,换高贵的并查集。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19
20 using namespace std;
21
22 const int maxn = 510;
23 const int maxm = 2020;
24 int n, ans;
25 int len[maxn];
26 char s[maxn][maxm];
27 set<int> rep[maxn];
28
29 inline bool scan_d(int &num) {
30     char in;bool IsN=false;
31     in=getchar();
32     if(in==EOF) return false;
33     while(in!=‘-‘&&(in<‘0‘||in>‘9‘)) in=getchar();
34     if(in==‘-‘){ IsN=true;num=0;}
35     else num=in-‘0‘;
36     while(in=getchar(),in>=‘0‘&&in<=‘9‘){
37             num*=10,num+=in-‘0‘;
38     }
39     if(IsN) num=-num;
40     return true;
41 }
42
43 int main() {
44     // freopen("in", "r", stdin);
45     int T, _ = 1;
46     scan_d(T);
47     while(T--) {
48         ans = 0;
49         memset(s, 0, sizeof(s));
50         memset(len, 0, sizeof(len));
51         scan_d(n);
52         for(int i = 1; i <= n; i++) {
53             scanf("%s", s[i]);
54             rep[i].clear();
55         }
56         int ans = -1;
57         for(int i = 1; i <= n; i++) {
58             for(int j = i - 1; j >= 1; j--) {
59                 if(len[i] < len[j]) continue;
60                 if(rep[i].find(j) != rep[i].end()) continue;
61                 if(strstr(s[i], s[j]) != NULL) {
62                     rep[i].insert(j);
63                 }
64                 else {
65                     ans = i;
66                     break;
67                 }
68             }
69         }
70         printf("Case #%d: %d\n", _++, ans);
71     }
72     return 0;
73 }

TLE

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19
20 using namespace std;
21
22 const int maxn = 510;
23 const int maxm = 2020;
24 int n, ans;
25 int pre[maxn];
26 int len[maxn];
27 char s[maxn][maxm];
28
29 int find(int x) {
30     return x == pre[x] ? x : pre[x] = find(pre[x]);
31 }
32 bool check(int x, int y) {
33     x = find(x);
34     y = find(y);
35     if(x == y) return 1;
36     return 0;
37 }
38
39 int main() {
40     // freopen("in", "r", stdin);
41     int T, _ = 1;
42     scanf("%d", &T);
43     while(T--) {
44         ans = 0;
45         memset(s, 0, sizeof(s));
46         memset(len, 0, sizeof(len));
47         scanf("%d", &n);
48         for(int i = 1; i <= n; i++) {
49             scanf("%s", s[i]);
50         }
51         for(int i = 1; i <= n; i++) {
52             pre[i] = i;
53         }
54         int ans = -1;
55         for(int i = 1; i <= n; i++) {
56             for(int j = i - 1; j >= 1; j--) {
57                 if(len[i] < len[j]) continue;
58                 if(check(i, j)) continue;
59                 if(strstr(s[i], s[j]) != NULL) {
60                     pre[i] = j;
61                 }
62                 else {
63                     ans = i;
64                     break;
65                 }
66             }
67         }
68         printf("Case #%d: %d\n", _++, ans);
69     }
70     return 0;
71 }
时间: 2024-12-16 09:56:16

[HDOJ5510]Bazinga(并查集)的相关文章

CodeForces 745C Hongcow Builds A Nation 并查集

题意: 给了你n个城市 m条边 k个政府 每个政府管辖的区域内不能和其他政府的区域有相连 即政府之间不存在路径 问你在维护这种关系的同时 最多再加多少条边 思路: 先找出来每个联通块 再找出来没有归属的孤立的点 把他们都放到最大的联通块里 然后每个联通块之间的点两两连边是n*(n-1)/2条边 最后算出来的ans-m就好了 (看别人的博客学了一个max_element 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a

并查集(个人模版)

并查集: 1 int find(int a) 2 { 3 int r=a; 4 while(f[r]!=r) 5 r=f[r]; 6 int i=a; 7 int j; 8 while(i!=r) 9 { 10 j=f[i]; 11 f[i]=r; 12 i=j; 13 } 14 return r; 15 } 16 int merge(int a,int b) 17 { 18 int A,B; 19 A=find(a); 20 B=find(b); 21 if(A!=B) 22 { 23 f[B

并查集应用

题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls

【bzoj3674】 可持久化并查集加强版

http://www.lydsy.com/JudgeOnline/problem.php?id=3674 (题目链接) 题意 维护并查集3个操作:合并:回到完成第k个操作后的状态:查询. Solution 其实就是用主席树的叶子节点维护并查集的可持久化数组fa[]. 细节 终于认识到了按秩合并的强大,单纯写个路径压缩Re飞,写了路径压缩+按秩合并比单纯的按秩合并每快多少→_→ 代码 // bzoj3674 #include<algorithm> #include<iostream>

BZOJ1015[JSOI2008]星球大战starwar[并查集]

1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 5253  Solved: 2395[Submit][Status][Discuss] Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧道互相直接或间接地连接. 但好景不长,很快帝国又重

HDU 5606 tree 并查集

tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans?i??=size[findset(i)],size表示每个并查集根的size Ans_i=size[findset(i)],sizeAns?i??=size[findset(i)],size表示每个并查集根的sizesize. #include<cstdio> #include<cstring> #include<algorithm>

HDU 5441 离线处理 + 并查集

题意:给n个节点m条带权值边的无向图.然后q个问题,每次询问点对的数目,点对需要满足的条件是:1)连通:2)其路径的最大权值不能超过询问值. 分析:如果没次询问一次,dfs一次,很可能超时,因此可以用并查集.离线处理,把边按权值排序,把问题按大小排序.然后离线的过程就是不断向图中加边的过程. 比如样例如下: 然后离线处理,排完序后将会是一条一条的加边:问题也排了序,因此是个累加过程... 1 #include <cstdio> 2 #include <iostream> 3 #in

poj1988 Cube Stacking(并查集

题目地址:http://poj.org/problem?id=1988 题意:共n个数,p个操作.输入p.有两个操作M和C.M x y表示把x所在的栈放到y所在的栈上(比如M 2 6:[2 4]放到[1 6]上为[2 4 1 6]),C x为输出x下面有几个数. 思路:并查集每个集合以栈最下面的数为根,维护两个数组num[x]表示x所在集合节点总数,count[x]表示x下方节点个数.每次查找压缩路径的时候更新count(换父节点的时候每轮都把父节点的count加给儿子,就可以一直更新到x所在栈

习题:过路费(kruskal+并查集+LCA)

过路费  [问题描述]在某个遥远的国家里,有 n 个城市.编号为 1,2,3,…,n.这个国家的政府修 建了 m 条双向道路,每条道路连接着两个城市.政府规定从城市 S 到城市 T 需 要收取的过路费为所经过城市之间道路长度的最大值.如:A 到 B 长度为 2,B 到 C 长度为 3,那么开车从 A 经过 B 到 C 需要上交的过路费为 3. 佳佳是个做生意的人,需要经常开车从任意一个城市到另外一个城市,因此 他需要频繁地上交过路费,由于忙于做生意,所以他无时间来寻找交过路费最低 的行驶路线.然