Codeforces 501C - Misha and Forest (机智)

题意

给出每个点的度数和和这个点连接的点的异或和,输出图的每条边。

思路

看了squee_spoon的题解。

这题的突破口在于叶子。因为叶子的sum就是点的编号。

所以在读取输入的时候把num为1的点都加入到队列,然后取出来,之后就能得到一条边。以此类推

代码

  1. #include <cstdio>
  2. #include <stack>
  3. #include <list>
  4. #include <set>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <queue>
  9. #include <functional>
  10. #include <cstring>
  11. #include <algorithm>
  12. #include <cctype>
  13. #include <string>
  14. #include <map>
  15. #include <cmath>
  16. using namespace std;
  17. #define LL long long
  18. #define ULL unsigned long long
  19. #define SZ(x) (int)x.size()
  20. #define Lowbit(x) ((x) & (-x))
  21. #define MP(a, b) make_pair(a, b)
  22. #define MS(arr, num) memset(arr, num, sizeof(arr))
  23. #define PB push_back
  24. #define F first
  25. #define S second
  26. #define ROP freopen("input.txt", "r", stdin);
  27. #define MID(a, b) (a + ((b - a) >> 1))
  28. #define LC rt << 1, l, mid
  29. #define RC rt << 1|1, mid + 1, r
  30. #define LRT rt << 1
  31. #define RRT rt << 1|1
  32. #define BitCount(x) __builtin_popcount(x)
  33. #define BitCountll(x) __builtin_popcountll(x)
  34. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  35. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  36. const double PI = acos(-1.0);
  37. const int INF = 0x3f3f3f3f;
  38. const double eps = 1e-8;
  39. const int MAXN = 100000 + 10;
  40. const int MOD = 1000007;
  41. const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
  42. typedef pair<int, int> pii;
  43. typedef vector<int>::iterator viti;
  44. typedef vector<pii>::iterator vitii;
  45. struct POINT
  46. {
  47. int num, sum;
  48. }arr[MAXN];
  49. queue<int> Q;
  50. int main()
  51. {
  52. //ROP;
  53. int n, i, j;
  54. scanf("%d", &n);
  55. int ans = 0;
  56. for (i = 0; i < n; i++)
  57. {
  58. scanf("%d%d", &arr[i].num, &arr[i].sum);
  59. ans += arr[i].num;
  60. if (arr[i].num == 1) Q.push(i);
  61. }
  62. printf("%d\n", (ans>>1));
  63. while (!Q.empty())
  64. {
  65. int leaf = Q.front(); Q.pop();
  66. if (arr[leaf].num == 0) continue;
  67. arr[leaf].num--;
  68. int u = arr[leaf].sum;
  69. arr[u].num--;
  70. arr[u].sum ^= leaf;
  71. if (arr[u].num == 1) Q.push(u);
  72. printf("%d %d\n", leaf, u);
  73. }
  74. return 0;
  75. }
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值