Inverse thinking + using and checking the number of dynamic maintenance connected blocks--luogu P1197

Source: Internet
Author: User

Main topic:

Give you a $ N $ point figure with $ M $ bar side, next give a length of $ K $ an integer, according to the order of the integers are given to delete the corresponding number of points in turn, to find out the number of connected blocks at the beginning of each subsequent deletion of a point and the number of connected blocks. (The connected block is a point set, and any two points within the set can reach each other)

Idea: General idea:

This topic is difficult to think about, because it is difficult to maintain the number of connected blocks of a continuously deleted graph every time. So we have to think backwards. If we consider the deletion process backwards, it becomes the process of adding it from the last point to the diagram. So the question becomes the number of the connecting blocks of each added graph, each time a graph is added to a point and some edges are connected from this point.

How to use and check the number of connected blocks maintained:

We first make each point independent, and then we follow the given relationship with and check the set merge. If two points can be combined, indicating that the two points before the different connected blocks, now the two connected blocks are connected, the number of connected blocks is reduced by one. If these two points are not merged, the two points are already in a connected block, and the number of connected blocks remains unchanged.

Code:
#include <iostream> #include <cstdio> #include <vector> #include <algorithm>using namespace std ; const int n = 5e5 + 5;int N, M, k;int u, v;int num = 0; Number of connected blocks int Arr[n];int F[n]; and check set bool Is_delete[n]; Mark those points to remove vectors <int> to[n];vector <int> ans; Storage answers vector <int> point_rest;    The remaining point of the deletion is int get_f (int v) {if (f[v] = = v) return v; else return f[v] = Get_f (F[v]);}    void merge (int u, int v) {int T1 = get_f (u);    int t2 = Get_f (v); if (t1! = t2) F[t2] = t1;}    int read () {int s = 0, w = 1;    char ch = getchar ();        while (Ch < ' 0 ' | | ch > ' 9 ') {if (ch = = '-') w =-1;    ch = getchar ();        } while (Ch >= ' 0 ' && ch <= ' 9 ') {s = s * + CH-' 0 ';    ch = getchar (); } return S * w;}    void write (int x) {if (x < 0) Putchar ('-'), x =-X;    if (x > 9) write (X/10); Putchar (x 10 + ' 0 ');}    int main () {n = read (), M = Read ();     for (register int i = 1; I <= m; ++i) {   U = Read (), V = read ();        To[u].push_back (v);    To[v].push_back (U);    } k = read ();        for (register int i = 1; I <= K; ++i) {Arr[i] = read (); Is_delete[arr[i]] = true; Mark the point to be deleted} for (register int i = 0; i < n; ++i) {f[i] = i;//Initialize and check set if (!            Is_delete[i]) {++num;        Point_rest.push_back (i);    }} vector <int>:: iterator it;        for (it = Point_rest.begin (); It! = Point_rest.end (); ++it) {vector <int>:: iterator it_tmp; for (it_tmp = To[*it].begin (); It_tmp! = To[*it].end (); ++it_tmp) {if (!                IS_DELETE[*IT_TMP]) {int T1 = Get_f (*it);                int t2 = Get_f (*it_tmp);                    if (t1! = t2) {merge ((*it), (*it_tmp));                --num;        }}}} for (register int i = k; I >= 1; i.) {ans.push_back (num); ++num; Add a number of connected points that have not been merged increase Is_delete[arr[i]] = false; for (it = To[arr[i]].begin (); It! = To[arr[i]].end (); ++it) {if (!                Is_delete[*it]) {int T1 = Get_f (Arr[i]);                int t2 = Get_f (*it);                    if (t1! = t2) {merge (Arr[i], (*it));                --num;    }}}} ans.push_back (num);    for (register int i = (int) ans.size ()-1; I >= 0;-i) write (Ans[i]), Putchar (' \ n '); return 0;}

Inverse thinking + using and checking dynamic maintenance of the number of connected blocks--luogu P1197 puzzle

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.