并查集 奇偶游戏

奇偶游戏

带权并查集+离散化

/*
    s[l,r]为奇数-> s[r]-s[l-1](前缀和)为奇数->s[r]与s[l-1]不同类
    若为偶数,则s[r]与s[l-1]为同一类
    用带权并查集维护
*/
#include 
#include 
#define MAXN 20000
using namespace std;
int p[MAXN], d[MAXN];
unordered_map mp;
int n, k;
int get(int x){ //离散化,重新编号
    if(!mp.count(x)) return mp[x] = ++n;
    return mp[x];
}
int find(const int &x){
    if(x!=p[x]){
        int t = find(p[x]);
        d[x] += d[p[x]];
        p[x] = t;
    }
    return p[x];
}
int main(){
    cin>>n>>k;
    n = 0;
    for(int i=0; i> x >> y >> s;
        x--;
        x=get(x), y=get(y);
        int px = find(x), py = find(y);
        int temp = 0;
        if(s == "odd") temp = 1;
        if(px == py){
            if( ((d[x]+d[y])%2+2)%2 !=temp){
                cout<

你可能感兴趣的:(游戏,算法)