HDU 5305 Friends (DFS)

Source: Internet
Author: User


Friends Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others) Total Submission (s): 163 Accepted Submission (s):

Problem Description There is n people and m 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 n people wants to having the same number of online and offline friends (i.e. If one person has X Onine friends, he or she must has X offline friends too, but different people can has different number of online or OFFL Ine friends). Determine how many ways there is to satisfy their requirements.

Input the first line of the input is a single integer T (t=100), indicating the number of testcases.

For each testcase, the first line contains the integers n (1≤n≤8) and M (0≤m≤n (n−1) 2), indicating the number of people and The number of pairs of friends, respectively. Each of the next m lines contains-numbers x and y, which mean X and Y are friends. It is guaranteed that x≠y and every friend relationship would appear at the most once.

Output for each testcase, print one number indicating the answer.
Sample Input

2 3 3 1 2 2 3 3 1 4 4 1 2 2 3 3 4 4 1
Sample Output
0 2
Source multi-university Training Contest 2

Some friends can be offline online, requiring everyone's offline friends number equals the number of online friends

Problem Analysis: Data volume is small, direct explosion search, record the degree of each point, odd directly impossible, even divided into two arrays, c1[i] means I of the number of online friends, C2[i] for i's offline friends, and then a side by side search on the line, pay attention to a pruning, when an edge of the two endpoints have a C value of 0 , then return

#include <cstdio> #include <cstring> bool mp[10][10];
int deg[10], c1[10], c2[10];

int n, M, cnt, ans; struct EDGE {int u, v;}

E[100000];
        void DFS (int cur) {if (cur = = m + 1) {ans + +;
    Return
    } int v = E[CUR].V;
    int u = e[cur].u;
        if (C1[u] && c1[v]) {C1[u]--;
        C1[V]--;
        DFS (cur + 1);
        C1[u] + +;
    C1[V] + +;
        } if (C2[u] && c2[v]) {C2[u]--;
        C2[V]--;
        DFS (cur + 1);
        C2[u] + +;
    C2[V] + +;
} return;
    } int main () {int T;
    scanf ("%d", &t);
        while (T--) {cnt = 0;
        Ans = 0;
        memset (deg, 0, sizeof (deg));
        memset (e, 0, sizeof (e));
        memset (c1, 0, sizeof (C1));
        memset (c2, 0, sizeof (C2));
        scanf ("%d%d", &n, &m);
            for (int i = 1; I <= m; i++) {scanf ("%d%d", &e[i].u, &AMP;E[I].V);
         DEG[E[I].U] + +;   DEG[E[I].V] + +;
        } bool F = false;
            for (int i = 1; I <= n; i++) {c1[i] = c2[i] = deg[i]/2;
                if (Deg[i] & 1) {f = true;
            Break
            }} if (f) {printf ("0\n");
        Continue
        } DFS (1);
    printf ("%d\n", ans); }
}

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.