POJ1733 Parity game(dfs,思路,离散化)


Now and then you play the following game with your friend. Your friend
writes down a sequence consisting of zeroes and ones. You choose a
continuous subsequence (for example the subsequence from the third to
the fifth digit inclusively) and ask him, whether this subsequence
contains even or odd number of ones. Your friend answers your question
and you can ask him about another subsequence and so on. Your task is
to guess the entire sequence of numbers.

You suspect some of your friend’s answers may not be correct and you
want to convict him of falsehood. Thus you have decided to write a
program to help you in this matter. The program will receive a series
of your questions together with the answers you have received from
your friend. The aim of this program is to find the first answer which
is provably wrong, i.e. that there exists a sequence satisfying
answers to all the previous questions, but no such sequence satisfies
this answer.


The first line of input contains one number, which is the length of
the sequence of zeroes and ones. This length is less or equal to
1000000000. In the second line, there is one positive integer which is the number of questions asked and answers to them. The number of
questions and answers is less or equal to 5000. The remaining lines
specify questions and answers. Each line contains one question and the
answer to this question: two integers (the position of the first and
last digit in the chosen subsequence) and one word which is either
even' orodd’ (the answer, i.e. the parity of the number of ones in
the chosen subsequence, where even' means an even number of ones and
odd’ means an odd number).


There is only one line in output containing one integer X. Number X
says that there exists a sequence of zeroes and ones satisfying first
X parity conditions, but there exists none satisfying X+1 conditions.
If there exists a sequence of zeroes and ones satisfying all the given
conditions, then number X should be the number of all the questions

Sample Input

1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd

Sample Output





  • 奇数+奇数=偶数
  • 偶数+偶数=奇数
  • 奇数+偶数=奇数



#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define inf 0x3f3f3f3f
typedef long long ll;
const int N = 5000 + 10;
struct edge
    int x, y, w;
} e[N];
struct node
    int val, w;
    node() {}
    node(int _val, int _w)
        val = _val, w = _w;
int pos[N], flag, vis[N];
vector<node> g[N];
int Y, W; //当前的右端点和当前所求区间的奇偶性
void dfs(int u, int step)
    if (u == Y)
        if (step % 2 != W)
            flag = 0;
    vis[u] = 1;
    for (int i = 0; i < g[u].size(); i++)
        node now = g[u][i];
        if (!vis[now.val])
            dfs(now.val, now.w + step);
int main()
    //freopen("in.txt", "r", stdin);
    int n, q;
    char s[10];
    int len = 0;
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= q; i++)
        scanf("%d%d%s", &e[i].x, &e[i].y, s);
        if (s[0] == 'e')
            e[i].w = 0;
            e[i].w = 1;
        pos[len++] = e[i].x;
        pos[len++] = e[i].y;
    sort(pos, pos + len);
    int m = unique(pos, pos + len) - pos;
    for (int i = 1; i <= q; i++)
        int x = lower_bound(pos, pos + m, e[i].x) - pos;
        int y = lower_bound(pos, pos + m, e[i].y) - pos;
        e[i].x = x;
        e[i].y = y;
    for (int i = 1; i <= q; i++)
        flag = 1;
        Y = e[i].y;
        W = e[i].w;
        mem(vis, 0);
        dfs(e[i].x, 0);
        if (flag == 0)
            printf("%d\n", i - 1);
            return 0;
        g[e[i].x].push_back(node(e[i].y, e[i].w));
        g[e[i].y].push_back(node(e[i].x, e[i].w));
    printf("%d\n", q);

    return 0;
版权声明:欢迎转载~转载请注明出处! https://blog.csdn.net/riba2534/article/details/80346378
文章标签: 离散化 dfs
个人分类: 【搜索(DFS/BFS)】
想对作者说点什么? 我来说一句



POJ1733 Parity game(dfs,思路,离散化)