PA7题解报告——无线广播(Broadcast)

    技术2022-07-11  114

    数据结构与算法实验2020夏第二批(中国石油大学) PA7题解报告——无线广播(Broadcast)

    目录

    题目描述题目分析编码实现

    一、题目描述

    1. 描述

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

    2. 输入

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

    3. 输出

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

    4. 例

    //输入 4 3 1 2 1 3 2 4 //输出 1

    5. 限制

    1 ≤ n ≤ 10000 1 ≤ m ≤ 30000 不需要考虑给定的20km小镇列表的空间特性,比如是否满足三角不等式,是否利用传递性可以推出更多的信息等等。 时间:2 sec 空间:256MB

    6. 提示

    BFS

    二、题目分析

    代码实现

    BFS遍历整个图,如果一个点的邻接点有不同颜色则无解,如果其邻接点已染色则把这个点染色。

    复杂度分析:

    时间复杂度: O ( n + m ) O(n+m) O(n+m)空间复杂度: O ( n + m ) O(n+m) O(n+m)

    三、编码实现

    说明: 下述代码全部为【数据结构与算法实验 OJ 平台】提交过的代码。

    #include <cstdio> #include <iostream> using namespace std; const int MAXSIZE = 10005; bool graph[MAXSIZE][MAXSIZE]; class Queue { private: int val[MAXSIZE]; public: int head = 0; int tail = 0; void Push(int i) { val[tail] = i; tail = (tail + 1) % MAXSIZE; } int Pop() { int res = val[head]; head = (head + 1) % MAXSIZE; return res; } bool isEmpty() { return head == tail; } }; bool BFSTraverse(int n, bool judger[], bool judger2[]); int main() { int n, m; bool judger[MAXSIZE], judger2[MAXSIZE]; cin >> n >> m; for (int i = 0; i != n; ++i) { judger2[i] = false; judger[i] = false; for (int j = 0; j < n; ++j) { graph[i][j] = false; } } for (int i = 0; i < m; ++i) { int v, w; cin >> v >> w; graph[v - 1][w - 1] = true; graph[w - 1][v - 1] = true; } if (BFSTraverse(n, judger, judger2)) { cout << "1" << endl; } else { cout << "-1" << endl; } return 0; } bool BFSTraverse(int n, bool judger[], bool judger2[]) { Queue Q; Q.Push(0); judger2[0] = true; int v, w; while (Q.isEmpty() == false) { v = Q.Pop(); judger2[v] = false; judger[v] = true; for (w = 0; w != n; ++w) { if (!judger[w] && graph[v][w]) { if (!judger2[w]) { judger2[w] = true; Q.Push(w); } else return false; } } } return true; }
    Processed: 0.022, SQL: 9