阅读背景:

清华数据结构无线广播AC100

来源:互联网 
#pragma warning(disable:4996)
#include<stdio.h>
using namespace std;
#define maxn 10000
#define Flag(i)((i==1)?2:1)
struct listnode
{
    listnode*pre;
    int num = -1;//记录该节点的编号
    listnode() {}
    listnode(int n, listnode*p = NULL) :num(n), pre(p) {}
};
struct p_list
{
    listnode*head, *trail;
    int flag = 0;
    int visit = 0;
    void init();
    p_list() { init(); }
    void insert(int&out);
};
void p_list::init()
{
    head = new listnode();
    trail = new listnode();
    head->pre = trail;
    trail->pre = NULL;
}
void p_list::insert(int&num)//越先进入的元素,越靠近队尾
{
    listnode*p = new listnode(num);
    p->pre = head->pre;
    head->pre = p;
}
p_list country[maxn];
int Queue[maxn];
int hi=0,result=1,trail=0;
void bsf(int pos)
{
    if(country[Queue[hi]].visit == 0)
    {
        auto p = country[pos].head->pre;
         while (p != country[pos].trail)
    {
        if(country[p->num].flag==0)country[p->num].flag = Flag(country[pos].flag);
        else if(country[p->num].flag == Flag(country[pos].flag));
        else{result=-1;break;}
        Queue[hi++]=p->num;
        p=p->pre;
    }
    country[pos].visit=1;
    }
    if(hi>0)
    bsf(Queue[--hi]);

}
void travel(int n, int m)
{
    country[0].flag = 1;
    for (auto i = 0; i<n&&result>0; i++)
    {
        Queue[0]=i;
        bsf(i);
    }
}
int main()
{
#ifndef _OJ_
    freopen("bfs.txt", "r", stdin);
    // freopen("travelout.txt", "w", stdout);
#endif
    int i, j, m, n;
    scanf("%d %d", &n, &m);
    auto t = m;
    while (t--) {
        scanf("%d %d", &i, &j);
        country[i - 1].insert(--j);
        country[j].insert(--i);

    }
    for (auto i = 0; i<n; i++)country[i].flag = 0;
    travel(n, m);
    printf("%d\n", result);
}
#pragma warning(disable:4996)
#include<st



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: