HDU 5305 Friends (second DFS + pruning for 2015 + schools)

Source: Internet
Author: User

FriendsTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 552 Accepted Submission (s): 253


Problem Descriptionthere is People and Pairs of friends. For every pair of friends, they can choose to become online friends (communicating using online applications) or offline F Riends (mostly using face-to-face communication). However, everyone in these People wants to having the same number of online and offline friends (i.e. If one person has Onine friends, he or she must have Offline friends too, but different people can has different number of online or offline friends). Determine how many ways there is to satisfy their requirements.

Inputthe first line of the input was a single integer , indicating the number of testcases.

For each testcase, the first line contains the integers < Span class= "Mrow" id= "mathjax-span-26" style= "" > and , indicating the number of people and the number of pairs of friends, respectively. Each of the next Lines contains, numbers and , which mean and Is friends. It's Guaranteed that And every friend relationship would appear at the most once.

Outputfor each testcase, print one number indicating the answer.
Sample Input
23 31 22 33 14 41 22 33 44 1

Sample Output
02

Source2015 multi-university Training Contest 2 problem Solving ideas:
Notice that the data range is small, so brute force search + pruning.
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include < algorithm>using namespace Std;const int maxn = 30;int X[MAXN],Y[MAXN], S[MAXN], S1[MAXN], S2[MAXN], G[maxn][maxn];int n , M;int ans;void dfs (int dep) {if (dep > m) {for (int i=1;i<=n;i++) {if (S1[i]! = S2[i        ]) return;        } ans++;    return;    } int u = x[dep], v = y[dep];        if (S1[u] < S[u]/2 && s1[v] < S[v]/2) {s1[u]++; s1[v]++;        DFS (DEP + 1); s1[u]--;    s1[v]--;        } if (S2[u] < S[u]/2 && s2[v] < S[v]/2) {s2[u]++; s2[v]++;        DFS (DEP + 1); s2[u]--;    s2[v]--;    }}int Main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n, &m);        memset (g, 0, sizeof (g));            for (int i=1; i<=m; i++) {scanf ("%d%d", &x[i], &y[i]); G[x[i]][y[i]] = G[y[i]][x[i]]= 1;        } int flag = 1;            for (int i=1; i<=n; i++) {s[i] = s1[i] = S2[i] = 0;            for (int j=1; j<=n; J + +) {if (i! = J && G[i][j] = = 1) s[i]++;        } if (S[i] & 1) flag = 0;            } if (!flag) {printf ("0\n");        Continue        } ans = 0;        DFS (1);    printf ("%d\n", ans); }}


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

HDU 5305 Friends (second DFS + pruning for 2015 + schools)

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.