• [HDOJ2473]Junk-Mail Filter(并查集,删除操作,马甲)


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

    给两个操作:
    M X Y:将X和Y看成一类。

    S X:将X单独划归成一类。

    最后问的是有多少类。

    并查集,但是带有删除操作。然而并查集本身不支持删除,网上说可以引入一个id来表示一个点。就好像一个人上网有很多小号一样,假如这个人的小号被封掉并且永久不能解封,还想继续玩下去的话那就重新建一个号,再生成一个id,表示这个id是这个人就好了。

    注意在删除操作的时候不能把原来id的pre值重置。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rll(a) scanf("%lld", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f7f, sizeof(a))
     53 #define lrt rt << 1
     54 #define rrt rt << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onenum(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long ULL;
     62 typedef pair<int, int> pii;
     63 typedef pair<string, int> psi;
     64 typedef map<string, int> msi;
     65 typedef vector<int> vi;
     66 typedef vector<LL> vl;
     67 typedef vector<vl> vvl;
     68 typedef vector<bool> vb;
     69 
     70 const int maxn = 2000010;
     71 int pre[maxn];
     72 int id[maxn];
     73 int sum[maxn];
     74 int n, m;
     75 int cnt, t;
     76 
     77 int find(int x) {
     78     RT x == pre[x] ? x : pre[x] = find(pre[x]);
     79 }
     80 
     81 void unite(int x, int y) {
     82     x = find(x);
     83     y = find(y);
     84     if(x != y) pre[y] = x;
     85 }
     86 
     87 int main() {
     88     // FRead();
     89     char cmd[5];
     90     int a, b, _ = 1;
     91     while(~Rint(n) && ~Rint(m) && n + m) {
     92         cnt = n + 10; Cls(sum);
     93         Rep(i, maxn) id[i] = i, pre[i] = i;
     94         W(m) {
     95             Rs(cmd);
     96             if(cmd[0] == 'M') {
     97                 Rint(a); Rint(b);
     98                 unite(id[a], id[b]);
     99             }
    100             if(cmd[0] == 'S') {
    101                 Rint(a);
    102                 id[a] = cnt++;
    103             }
    104         }
    105         Rep(i, n) sum[i] = find(id[i]);
    106         sort(sum, sum+n); t = unique(sum, sum+n) - sum;
    107         printf("Case #%d: %d
    ", _++, t);
    108     }
    109     RT 0;
    110 }
  • 相关阅读:
    Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)
    OpenCV学习笔记——多种Smooth平滑处理
    HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)
    POJ 2533 Longest Ordered Subsequence(LIS模版题)
    NBUT 1186 Get the Width(DFS求树的宽度,水题)
    Codeforeces 617E XOR and Favorite Number(莫队+小技巧)
    CodeBlocks的汉化、主题美化及其调试功能的实现
    PAT天梯赛练习题 L3-010. 是否完全二叉搜索树(完全二叉树的判断)
    POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
    HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
  • 原文地址:https://www.cnblogs.com/kirai/p/5545661.html
Copyright © 2020-2023  润新知