无线广播(Broadcast)

描述
某广播公司要在一个地区架设无线广播发射装置。该地区共有n个小镇,每个小镇都要安装一台发射机并播放各自的节目。

不过,该公司只获得了FM104.2和FM98.6两个波段的授权,而使用同一波段的发射机会互相干扰。已知每台发射机的信号覆盖范围是以它为圆心,20km为半径的圆形区域,因此,如果距离小于20km的两个小镇使用同样的波段,那么它们就会由于波段干扰而无法正常收听节目。现在给出这些距离小于20km的小镇列表,试判断该公司能否使得整个地区的居民正常听到广播节目。

输入
第一行为两个整数n,m,分别为小镇的个数以及接下来小于20km的小镇对的数目。 接下来的m行,每行2个整数,表示两个小镇的距离小于20km(编号从1开始)。

输出
如果能够满足要求,输出1,否则输出-1。

输入样例
4 3
1 2
1 3
2 4
输出样例
1
限制
1 ≤ n ≤ 10000

1 ≤ m ≤ 30000

不需要考虑给定的20km小镇列表的空间特性,比如是否满足三角不等式,是否利用传递性可以推出更多的信息等等。

时间:2 sec

典型的着色问题,注意图一定要是无向图,不然可能出现孤立点而产生错误。

#include <iostream>

using namespace std;
int VexNum,EdgeNum;
int const maxn = 30010;

struct Arc {
    int AdjVex;
    Arc* nextArc;
    Arc(int a,Arc* b=NULL):AdjVex(a),nextArc(b){}
};

struct Vex {
    Arc* firstArc;
    int color;
    Vex(int a = 0, Arc* b = NULL):color(a),firstArc(b){}
    void clear() {
        while(firstArc) {
            Arc* tmp = firstArc;
            firstArc = firstArc->nextArc;
            delete tmp;
        }
    }
}V[maxn];


int f(int lo,int co) {
    Arc* p ;
    if(co==0) {
        V[lo].color = 1;
           p = V[lo].firstArc;
           while(p) {
              if(f(p->AdjVex,1)==-1) return -1;
              p = p->nextArc;
           }
        return 1;
    } 
    if(co==1) {
        if(V[lo].color==1) return -1;
        if (V[lo].color==0) {
            V[lo].color = 2;
            p = V[lo].firstArc;
               while(p) {
              if(f(p->AdjVex,2)==-1) return -1;
              p = p->nextArc;
           }
           return 1;
        }
    }

    if(co==2) {
        if(V[lo].color==2) return -1;
        if (V[lo].color==0) {
            V[lo].color = 1;
            p = V[lo].firstArc;
               while(p) {
              if(f(p->AdjVex,1)==-1) return -1;
              p = p->nextArc;
           }
           return 1;
        }
    }
}




int main() {
   cin>>VexNum>>EdgeNum;
   for (int i=0;i<EdgeNum;i++) {
        int a1,a2;
        cin>>a1>>a2;
        Arc* tmp = new Arc(a2,V[a1].firstArc);
        V[a1].firstArc = tmp;
        tmp = new Arc(a1,V[a2].firstArc);
        V[a2].firstArc = tmp;
   }
   cout<<f(1,0)<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值