[关闭]
@DingCao-HJJ 2015-09-29T16:48:29.000000Z 字数 1063 阅读 995

sicily_1021 Couples

sicily


题目

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

N couples are standing in a circle, numbered consecutively clockwise from 1 to 2N. Husband and wife do not always stand together. We remove the couples who stand together until the circle is empty or we can't remove a couple any more.

Can we remove all the couples out of the circle?

Input

There may be several test cases in the input file. In each case, the first line is an integer N(1 <= N <= 100000)----the number of couples. In the following N lines, each line contains two integers ---- the numbers of each couple.
N = 0 indicates the end of the input.

Output

Output "Yes" if we can remove all the couples out of the circle. Otherwise, output "No".

Sample Input

4
1 4
2 3
5 6
7 8

2
1 3
2 4

0

Sample Output

Yes
No

思路

用数组模拟堆栈,如果检查到是一堆夫妻在一起,就将这对夫妻出栈,直到把所有夫妻都操作过一遍。最后判断栈顶是否为空即可。

代码

  1. #include<stdio.h>
  2. #include<string.h>
  3. int main() {
  4. int i, j, n, a, b, c[200000 + 20], d[200000 + 20];
  5. while (scanf("%d", &n) && n) {
  6. memset(c, 0, sizeof(c));
  7. memset(d, 0, sizeof(d));
  8. for (i = 0; i < n; i++) {
  9. scanf("%d %d", &a, &b);
  10. c[a - 1] = 2 * i + 1;
  11. c[b - 1] = 2 * i + 2;
  12. }
  13. for (i = 0, j = 0, d[0] = c[0]; i < 2 * n; i++) {
  14. if (c[i + 1] != d[j] + 1) {
  15. d[j + 1] = c[i + 1];
  16. j++;
  17. } else {
  18. d[j] = 0;
  19. j--;
  20. }
  21. }
  22. printf(!d[0] ? "Yes\n" : "No\n");
  23. }
  24. return 0;
  25. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注