HDU 5305 Friends (Deep Search)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5305


Surface:

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


Problem Descriptionthere 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 have x 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 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≤< Span id= "mathjax-span-47" class= "Mi" style= "font-family:mathjax_math; font-size:70.7%; Font-style:italic ">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 Is friends. It's Guaranteed that x≠y 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

Solving:

Because 28 is not very large, it is easy to think of search, but the direct search will be timed out, plus a person when the reality or virtual friends is greater than the number of its own half of the relationship, the pruning can be returned. Note that the start can be directly sentenced to a person to close the coefficient is odd, then no more than the search.


Code:

#include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace std;  int t,n,m,cnt_net[10],cnt_real[10],cnt_all[10],a,b,ans,fm[40],to[40];void dfs (int no,bool status) {A=fm[No];  B=to[no];    if (status) {cnt_real[a]++;    cnt_real[b]++; if ((CNT_REAL[A]&GT;CNT_ALL[A]/2) | | |        (CNT_REAL[B]&GT;CNT_ALL[B]/2))  Return    } else {cnt_net[a]++;    cnt_net[b]++; if ((CNT_NET[A]&GT;CNT_ALL[A]/2) | | |        (CNT_NET[B]&GT;CNT_ALL[B]/2))  Return      } if (no==m) {bool sign=true;              for (int i=1;i<=n;i++) {if (Cnt_real[i]!=cnt_net[i]) {sign=false;          Break      }} if (sign) ans++;  Return  } dfs (no+1,1);  cnt_real[fm[no+1]]--;  cnt_real[to[no+1]]--;  DFS (no+1,0);  cnt_net[fm[no+1]]--; cnt_net[to[no+1]]--;}    int main () {scanf ("%d", &t);      while (t--) {bool flag=true;      ans=0;      scanf ("%d%d", &n,&m);   memset (cnt_net,0,sizeof (cnt_net));   memset (cnt_real,0,sizeof (cnt_real));      memset (cnt_all,0,sizeof (Cnt_all));        for (int i=1;i<=m;i++) {scanf ("%d%d", &a,&b);        Fm[i]=a;        To[i]=b;        cnt_all[a]++;      cnt_all[b]++;              } for (int i=1;i<=n;i++) {if (cnt_all[i]%2) {flag=false;          Break          }} if (!flag) {printf ("0\n");      Continue        } if (m) {DFS (1,0);        cnt_net[fm[1]]--;    cnt_net[to[1]]--;        cout<<cnt_net[fm[1]]<< "" <<cnt_net[to[1]]<<endl;      DFS (a);      } else Ans=1;    printf ("%d\n", ans); } return 0;}


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

HDU 5305 Friends (Deep Search)

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.