清華OJ無線廣播(Broadcast)

無線廣播(Broadcast)

Description
A broadcaster wants to set up a radio broadcast transmitter in an area. There are n towns in the area, and each town has a transmitter installed and plays its own program.
However, the company only licensed the two bands FM104.2 and FM98.6, and transmitters using the same band would interfere with each other. It is known that the signal coverage of each transmitter is a circular area with a radius of 20km. Therefore, if two towns with a distance of less than 20km use the same band, they will not be able to function properly due to band interference. Listen to the show. Now give a list of towns with distances less than 20km, and try to determine whether the company can make residents of the entire region hear the broadcasts normally.
Input
The first line is two integers n, m, which are the number of towns and the number of town pairs that are less than 20km. The next m lines, 2 integers per line, indicate that the distance between the two towns is less than 20km (numbering starts at 1).
Output
Output 1 if the requirement is met, otherwise -1.
Input sample
4 3
1 2
1 3
twenty four
Output sample
1
Restrictions
1 ≤ n ≤ 10000
1 ≤ m ≤ 30000
There is no need to consider the spatial characteristics of a given 20km town list, such as whether triangle inequality is satisfied, whether more information can be derived using transitivity, and so on.
Time: 2 sec
Space: 256MB
Tips
BFS
描述
某廣播公司要在一個地區架設無線廣播發射裝置。該地區共有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
空間:256MB

分析

簡單的無向圖+BFS

我的代碼

#include<iostream>
#include<cstring>
#include<cstdio>
//請勿搬抄,尤其中國石油大學的學生,一定要自己實現,避免查重,加油,有不理解的地方可以與我交流
using namespace std;
#define MAX 10010
int n, m;        //小鎮數-相距20Km內小鎮對數
int queue[MAX],head,tail;    //模擬隊列-隊首-隊尾
int cover;    //Broadcast放置數量


struct Node
{
    int num;
    Node *next;
    Node()
    {
        next = NULL;
    }
    Node(int n, Node *k) :num(n), next(k) {}
};

struct Town
{
    int state;
    Node *t;
    Town()
    {
        state = 0;
        t = NULL;
    }
    void insert(int num);
} town[MAX];

void Town::insert(int num)
{
    if (this->t == NULL)
        this->t = new Node(num,NULL);
    else
        this->t = new Node(num,this->t);
}

bool BFS(int x)
{
    queue[tail++] = x;
    town[x].state = 1;
    cover++;
    while (head < tail)
    {
        Town cur = town[queue[head]];
        Node *tmp = cur.t;
        while (tmp != NULL)
        {
            if (!town[tmp->num].state)
            {
                town[tmp->num].state = -cur.state;
                cover++;
                queue[tail++] = tmp->num;
            }
            else if (town[tmp->num].state == cur.state)
                return false;
            tmp = tmp->next;
        }
        head++;
    }
    return true;
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        town[x].insert(y);
        town[y].insert(x);
    }
    for (int i = 1; i <= n; i++)
    {
        if (town[i].state==0)
        {
            if (BFS(i) == false)
            {
                printf("-1\n");
                return 0;
            }
            if (cover == n)
            {
                printf("1\n");
                return 0;
            }
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章