P5247

· · 题解

双向广搜可以通过,不过需要一些常数优化。

存边可以使用平衡树,不想手写可使用 set 或 map 但常数并不如意,可使用 vector 实现的假的平衡树,但拥有奇奇怪怪的常数,使其变得很快。

双向宽搜可以使用循环展开变得快一些。

如果还不够快,那就加上一个快读快写就行了,但实测不加快读可以过。

代码:

#include <bits/stdc++.h>
using namespace std;
namespace fast_IO {
#define FASTIO
#define IOSIZE 100000
    char ibuf[IOSIZE], obuf[IOSIZE];
    char *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#ifdef ONLINE_JUDGE
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#endif//fread in OJ, stdio in local

#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
    template<typename T> inline T read() {
        T s = 0;
        int w = 1;
        char ch;
        while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1;
        if (ch == EOF) return false;
        while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar();
        return s * w;
    }
    template<typename T> inline bool read(T &s) {
        s = 0;
        int w = 1;
        char ch;
        while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1;
        if (ch == EOF) return false;
        while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar();
        return s *= w, true;
    }
    inline bool read(char &s) {
        while (s = getchar(), isspace(s));
        return true;
    }
    inline bool read(char *s) {
        char ch;
        while (ch = getchar(), isspace(ch));
        if (ch == EOF) return false;
        while (!isspace(ch)) *s++ = ch, ch = getchar();
        *s = '\000';
        return true;
    }
    template<typename T> inline void print(T x) {
        if (x < 0) putchar('-'), x = -x;
        if (x > 9) print(x / 10);
        putchar(x % 10 + 48);
    }
    inline void print(char x) {
        putchar(x);
    }
    inline void print(char *x) {
        while (*x) putchar(*x++);
    }
    inline void print(const char *x) {
        for (int i = 0; x[i]; i++) putchar(x[i]);
    }
#ifdef _GLIBCXX_STRING
    inline bool read(std::string& s) {
        s = "";
        char ch;
        while (ch = getchar(), isspace(ch));
        if (ch == EOF) return false;
        while (!isspace(ch)) s += ch, ch = getchar();
        return true;
    }
    inline void print(std::string x) {
        for (int i = 0, n = x.size(); i < n; i++)
            putchar(x[i]);
    }
#endif//string
    template<typename T, typename... T1> inline int read(T& a, T1&... other) {
        return read(a) + read(other...);
    }
    template<typename T, typename... T1> inline void print(T a, T1... other) {
        print(a);
        print(other...);
    }

    struct Fast_IO {
        ~Fast_IO() {
            fwrite(obuf, p3 - obuf, 1, stdout);
        }
    } io;
    template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) {
        return read(b), io;
    }
    template<typename T> Fast_IO& operator << (Fast_IO &io, T b) {
        return print(b), io;
    }
#define cout io
#define cin io
#define endl '\n'
}
using namespace fast_IO;
const int N = 5000 + 5; 
int n, m;
vector<int> a[N];
int vis[N];
int qx[N], qy[N];
bool chk(int x, int y) {
    int l1, r1, l2, r2;
    l1 = r1 = l2 = r2 = 0;
    qx[r1++] = x, qy[r2++] = y;
    vis[x] = 1;
    vis[y] = 2;
    if (x == y) return true;
    while (true) {
        if (l1 == r1) return false;
        int u = qx[l1++];
        for (int i = 0; i < a[u].size(); ++i) {
            int v = a[u][i];
            if (vis[v] == 2) return 1;
            if (vis[v] == 0) {
                qx[r1++] = v;
                vis[v] = 1;
            }
        }
        if (l2 == r2) return false;
        u = qy[l2++];
        for (int i = 0; i < a[u].size(); ++i) {
            int v = a[u][i];
            if (vis[v] == 1) return 1;
            if (vis[v] == 0) {
                qy[r2++] = v;
                vis[v] = 2;
            }
        }
    }
    return false;
}
int main() {
    cin >> n >> m;
    int lst = 0, op, x, y;
    while (m--) {
        cin >> op >> x >> y;
        x ^= lst, y ^= lst;
        if (op == 0) {
            a[x].insert(lower_bound(a[x].begin(), a[x].end(), y), y);
            a[y].insert(lower_bound(a[y].begin(), a[y].end(), x), x);
        }
        else if (op == 1) {
            a[x].erase(lower_bound(a[x].begin(), a[x].end(), y));
            a[y].erase(lower_bound(a[y].begin(), a[y].end(), x));
        }
        else {
            memset(vis, 0, sizeof(vis));
            if (chk(x, y)) {
                cout << "Y\n";
                lst = x;
            }
            else {
                cout << "N\n";
                lst = y;
            }
        }
    }
    return 0;
}