Getting Started with Dfs search (not perfect for the first time please also point out, thank you for your support)

Source: Internet
Author: User

Test instructions: Given the number of n from 1 to N, some of these numbers have a relationship with other numbers. Given all relationships, and give the initial value ans as 1, the n number is taken out in one order (each time the number taken out has a relationship with the number that was previously removed, the ANS value doubles.) Finally, the ANS value is the largest.

Thinking analysis: The relationship between the numbers in the subject can be expressed in the diagram. Columns such as n = 9;m = 9; (1,4); (2,3); (2,4); (7,2); (4,7); (5,8); (5,6); (8,6); so that the relationship between the numbers can be clearly expressed in graphs, And the number of points in any one connected graph p determines the maximum number of times that the ANS value is doubled to p-1 times, which we make by the connected graph to generate a spanning tree;

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace Std;

const int MAXN = 100;
int N, M, A, B, p;
Long long ans;
BOOL C[MAXN + 5][MAXN + 5], LOC[MAXN + 5]; C[x][y] = = 1 indicates that there is a relationship between x and Y.

void Dfs (int x)
{
for (int i = 1; I <= n; i++)
{
if (c[x][i] = = 1 && loc[i] = = 0) {Loc[i] = 1; p++; Dfs (i);}
}
}

int main ()
{
scanf ("%i64d%i64d", &n, &m);
memset (c, 0, sizeof (c));
memset (Loc, 0, sizeof (LOC));
for (int i = 1; I <= m; i++)//read into relationships between all numbers.
{
scanf ("%d%d", &a, &b);
C[A][B] = 1;
C[b][a] = 1;
}
ans = 1;
for (int i = 1; I <= n; i++)
{
p = 0; The P-Value records the number of points owned by each connected graph.
if (!loc[i])//loc[i] = 1 indicates that this point has been searched
{
Loc[i] = 1;
p++;
Dfs (i);
}
for (int j = 1; J <= P-1; j + +) ans *= 2;
}
printf ("%i64d\n", ans);
}

The difficulty lies in the reasonable search of all the numbers, and understand that each connected graph can get the best answer, by drawing and the tree generated by the graph to obtain the answer, so that the clutter of the digital relationship is visualized.

Getting Started with Dfs search (not perfect for the first time please also point out, thank you for your support)

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.