栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3029 BEATBIT

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3029 BEATBIT

#include <map>#include <cstdio>#include <cstring>using namespace std;template<int MAXN>struct DisjointSet{    int p[MAXN];    void clear(int n)    {        for (int i = 0; i < n; i++) { p[i] = i;        }    }    int root(int x)    {        if (p[x] == x) { return x;        }        else { return p[x] = root(p[x]);        }    }    void set_friend(int i, int j)    {        i = root(i);        j = root(j);        p[i] = j;    }    bool is_friend(int i, int j)    {        return root(i) == root(j);    }};struct Ins{    int lab, ins, dst;    bool read()    {        static char buf[128];        scanf("%s", buf);        if (strcmp(buf, "END") == 0) { return false;        }        sscanf(buf, "%d", &lab);        scanf("%s", buf);        if (*buf == 'R') { ins = (buf[3] == '1');        }        else { ins = 2 + (buf[0] == 'B'); scanf("%d", &dst);        }        return true;    }};DisjointSet<20002> ds;int n[2];Ins ins[2][10001];map<int, int> mp[2];bool flag;void gao(int i, int j){    if (!flag || ds.is_friend(i, n[0] + j)) {        return;    }    else if (ins[0][i].ins <= 1 && ins[1][j].ins <= 1) {        if (ins[0][i].ins != ins[1][j].ins) { flag = false;        }    }    else if (ins[0][i].ins == 2){        int ni = mp[0][ins[0][i].dst];        ds.set_friend(i, ni);        gao(ni, j);    }    else if (ins[1][j].ins == 2) {        int nj = mp[1][ins[1][j].dst];        ds.set_friend(n[0] + j, n[0] + nj);        gao(i, nj);    }    else if (ins[0][i].ins <= 1) {        gao(i, j + 1);        gao(i, mp[1][ins[1][j].dst]);    }    else if (ins[1][j].ins <= 1) {        gao(i + 1, j);        gao(mp[0][ins[0][i].dst], j);    }    else {        gao(i + 1, j + 1);        gao(mp[0][ins[0][i].dst], mp[1][ins[1][j].dst]);    }    if (flag) {        ds.set_friend(i, n[0] + j);    }}int main(){    int re;    scanf("%d", &re);    while (re--) {        scanf("%*d");        for (int i = 0; i < 2; i++) { n[i] = 0; mp[i].clear(); while (ins[i][n[i]].read()) {     mp[i][ins[i][n[i]].lab] = n[i]++; }        }        ds.clear(n[0] + n[1]);        flag = true;        gao(0, 0);        printf("%dn", flag ? 1 : 0);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/374481.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号