CodeforcesRound #254 (Div.2)

Source: Internet
Author: User
NoteInthefirstsample, theresonlyonewaytopour, andthedangerwontincrease. Inthesecondsample, nomatterwepourthe1stchemicalfirst, orpourthe2ndchemicalfirst, second, therear

Note In the first sample, there's only one way to pour, and the danger won't increase. in the second sample, no matter we pour the 1 st chemical first, or pour the 2 nd chemical first, the answer is always 2. in the third sample, there ar

Note

In the first sample, there's only one way to pour, and the danger won't increase.

In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.

In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring ).

Write to yourself: the question of water and water, even WA, has never been found in the wrong place. finally, I found that there was no error in both the idea and details, but the type accuracy of the answer output was not enough (long should be used only for int ). A painful understanding. do not forget shame.

Analysis: drawing the reaction relationships of various chemicals, we can see that as long as any traversal graph (this graph may be non-connected) (DFS, BFS, and query set) can be used) traverse it to solve the problem (danger doubles if one edge exists ).

The following uses the "DFS represented by the adjacent matrix ":

#include
 
  #include
  
   #define maxn 55int G[maxn][maxn],vis[maxn];int n;long long ans;void DFS(int v){    int w;    vis[v]=1;    for(w=1;w<=n;w++)        if(G[v][w]!=0 && vis[w]==0)        {   ans=ans*2;  DFS(w); }}void DFSTraverse(){    int i;    for(i=1;i<=n;i++)        if(vis[i]==0)        DFS(i);}int main(){int m;int a,b;scanf("%d%d",&n,&m);memset(G,0,sizeof(G));memset(vis,0,sizeof(vis));ans=1;while(m--)    {        scanf("%d%d",&a,&b);        G[a][b]=G[b][a]=1;    }    DFSTraverse();    printf("%lld\n",ans);return 0;}
  
 

In addition, the following practices can be used for reference and collection:

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.