[关闭]
@tenlee 2015-07-25T02:14:20.000000Z 字数 1105 阅读 1550

HDU 5305 Friends(2015多校联合)

HDUOJ 题解


题目链接:戳我

题目大意:

n个人,m对朋友关系,朋友之间可以选择成为 在线朋友 或者 离线朋友,每个人都想有相同数目的 在线朋友 和 离线朋友。(比如一个人有 x 个在线朋友,那么他必须有 x 个离线朋友)但是不同的人 x 可以不同。求有多少种方案可以满足他们的要求。

样例解释

2 两个测试用例
3 3 三个人,三对朋友关系
1 2 1 2 是朋友,他们可以选择成为离线或者在线朋友
2 3 2 3 是朋友,他们可以选择成为离线或者在线朋友
3 1 3 4 是朋友,他们可以选择成为离线或者在线朋友
4 4 同理
1 2
2 3
3 4
4 1

解题思路

此处输入图片的描述

代码

  1. //Author LJH
  2. //www.cnblogs.com/tenlee
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cctype>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #define clc(a, b) memset(a, b, sizeof(a))
  14. using namespace std;
  15. const int inf = 0x3f;
  16. const int INF = 0x3f3f3f3f;
  17. const int maxn = 10;
  18. int du_on[maxn], du_off[maxn], g[maxn][maxn];
  19. int n, m, ans;
  20. void dfs(int x, int y)
  21. {
  22. if(x > n) ans++;
  23. else if(y > n)
  24. {
  25. if(du_on[x] != du_off[x]) return; //如果一个人的在线朋友和离线朋友不相等,直接退出,
  26. dfs(x+1, x+2);
  27. }
  28. else
  29. if(g[x][y]) //枚举
  30. {
  31. du_on[x]++; du_on[y]++;//在线朋友个数加一
  32. dfs(x, y+1);
  33. du_on[x]--; du_on[y]--;//还原回来,因为下面的dfs可能会用到
  34. du_off[x]++; du_off[y]++;
  35. dfs(x, y+1);
  36. du_off[x]--; du_off[y]--;
  37. }
  38. else
  39. dfs(x, y+1);
  40. }
  41. int main()
  42. {
  43. int T, x, y;
  44. scanf("%d", &T);
  45. while(T--)
  46. {
  47. scanf("%d %d", &n, &m);
  48. clc(du_on, 0);
  49. clc(du_off, 0);
  50. clc(g, 0);
  51. while(m--)
  52. {
  53. scanf("%d %d", &x, &y);
  54. g[x][y] = g[y][x] = 1;
  55. }
  56. ans = 0;
  57. dfs(1, 2);
  58. printf("%d\n", ans);
  59. }
  60. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注