"Algorithmic Learning Notes" 41. And check set SJTU OJ 1283 Mixture

Source: Internet
Author: User

---restore content starts---

Description

CC likes chemistry very much, and especially likes to pour a lot of liquids together.

Now CC has n liquids, where m pairs react, and now she wants to pour the n liquid into a container in a certain order, giving her the most exciting experience and making the risk factor as large as possible.

We can calculate the risk factor so that there is no liquid in the first container and the risk factor is 1. Each time a liquid is poured into a container, if one or more liquids in the container react with the liquid, the risk factor is multiplied by 2, otherwise the risk factor is unchanged.

Please find out the maximum risk factor for pouring these n liquids together.

Input Format

The first behavior is two numbers n and M.

Next m line, two numbers a, b for each line. Indicates that liquid A and B liquid will react. Ensure that the same side does not recur.

30% data: \ (1 \le n \le 10 \)

100% data: \ (1 \le n \le 1000 \)

Output Format

One line, the maximum risk factor.

Sample Input
3 21 22 3
Sample Output
4


The essence of Mathematics is:
Given n points, M-sides, find out how many independent connected blocks K


Pay attention to high precision ...


For this problem, you can think of each point as a separate block, and then each input side, we make corresponding updates, so we can finally be divided into K independent block
But it cannot be stored in graphs because the graph is too large and time-consuming to store. Think of our goal is to figure out the number of independent blocks, you can use the idea of the collection to complete this matter
This data structure is called and checked union find

The source of understanding is: http://blog.csdn.net/dellaserss/article/details/7724401
This blogger is simply too funny ...

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>using namespacestd;intprenode[1050]; intnum[ -]={0};intLen =1;//2 times the Power PartyvoidMUL2 (intPower) {num[0]=1;  for(inti =0; I < power; ++i) {intR =0; intOri_len =Len;  for(intj =0; J < Ori_len; ++j) {intTMP = num[j]*2+R; R=0; NUM[J]= (TMP)%Ten; //cout<<num[j]<<endl;            if(TMP >=Ten) {R= (TMP)/Ten;//, Num[len++]=r;            }        }        if(r!=0) Num[len++] =R; }}//CheckintFindintx) {//find the root node of the sub-graph where x is located    intres =x;  while(res! = Prenode[res]) {//Res is the root node condition is that the root node of RES is itselfres =Prenode[res]; }    //The compression path allows all nodes involved in this query to have the parent node of the root node so as to form only two levels of tree structure (not guaranteed to be related to the input edge order)     while(X! =Res) {        intPre =Prenode[x]; PRENODE[X]=Res; X=Pre; }    returnRes;}//andvoidJoinintXintY) {//put X and y together in the same sub-graph    intRootx = Find (x), Rooty =find (y); if(Rootx! = Rooty)//necessary if x, Y is not part of the same sub-graphPrenode[rootx] = Rooty;//belong to the same sub-graph    return;} intMainintargcChar Const*argv[]) {        intn,m; CIN>>n>>m; //Initialize n point root node for itself     for(inti =1; I <= N; ++i) prenode[i]=i; //Adjust each edge     for(inti =0; I < m; ++i) {intx, y; CIN>>x>>y;       Join (x, y); }    //find the number of connected regions K    intK =0;  for(inti =1; I <= N; ++i)if(prenode[i]==i) K++; //cout<<k<<endl; //cout<< (1<< (n-k)) <<endl;MUL2 (nk);  for(inti = len-1; I >=0; --i) {cout<<Num[i]; }cout<<Endl; return 0;} 
View Code

"Algorithmic Learning Notes" 41. And check set SJTU OJ 1283 Mixture

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.