Hdu2473-junk-mail Filter deletes and sets nodes using a virtual array

Source: Internet
Author: User

Hdu2473-junk-mail filter:http://acm.hdu.edu.cn/showproblem.php?pid=2473

The main topic: M a B, representing A and b are the same class, S A, representing the original given the information of a is wrong, and now separate it into a category. Ask for the last number of messages in total.

If it's just a classification, it's very straightforward to think about it and look it up. But if you use and check the set directly there will be a mistake, because the deleted node may also have other sub-nodes, if you delete it directly, the result will be wrong. Here you need to use a virtual array, trick, and the father array to achieve the purpose of "delete" (actually the node still exists).

Code:

#include <iostream> #include <cstring> #include <cstdio>using namespace std;const int maxn = 1000111*2;    int n,m;int X,y;int cnt,case = 0,ans;int trick[maxn],father[maxn],hash[maxn];void Initial () {cnt = N; Ans = 0;for (int i = 0;i < maxn;i++)//Open another virtual array Trick, start and Father store the same information father[i] = trick[i] = I,hash[i] = 0;} int Find (int x) {if (x = = Father[x]) return X;return father[x] = find (Father[x]);} void Union (int x,int y) {x = find (x); y = find (y); if (x! = y) father[y] = x;} void Delete (int x) {father[cnt] = cnt;//The information that is passed in is Trick, where the change does not affect the query of the child nodes of x trick[x] = cnt++;//updated information in Trick, delete x, The expression x becomes a single collection}char Op[5];int main () {while (~scanf ("%d%d", &n,&m), N + M) {Initial (), while (m--) {scanf ("%s", op); (op[0] = = ' M ') {scanf ("%d%d", &x,&y); Union (Trick[x],trick[y]);//pass in the information in the Trick, but find the merger with father, the information is stored in Trick, not destroyed}else{scanf ("%d", &x);D elete (x);//delete x node}}for (int i = 0;i < n;i++)//Only N humans are not changed, the lookup is divided into several sets {x = Find (Trick[i]); HASH[X]) hash[x] = 1,ans++;} printf ("Case #%d:%D\n ", ++case,ans);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu2473-junk-mail Filter deletes and sets nodes using a virtual array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.