• 无线广播(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

    1. 原理与要点:简单的染色问题,给图染两个颜色,相邻的点颜色不能相同。bfs遍历整个图,如果其邻接点已染色,就把这个点染成另一种颜色。如果一个点的邻接点中有两种不同的颜色,则无解。
    2. 遇到的问题:
    3. 时间和空间复杂度:时间复杂度(O(M+N)),空间复杂度(O(M+N))
    #include "iostream"
    #include "cstdio"
    
    using namespace std;
    const int maxn = 4e5 + 100;
    
    int ver[maxn], Next[maxn], head[maxn];
    int vis[maxn];
    int tot = 0;
    int que[maxn];
    
    void add(int x, int y) {
        ver[++tot] = y;
        Next[tot] = head[x];
        head[x] = tot;
    }
    
    
    bool bfs(int start) {
        int hea = 0, tail = 0;
        que[tail++] = start;
        vis[start] = 1;
        int now, nex;
        while (hea != tail) {
            now = que[hea++];
            if (vis[now] == 1) nex = 2;
            else nex = 1;
            for (int i = head[now]; i; i = Next[i]) {
    
                if (vis[ver[i]] == vis[now]) return false;
                if (vis[ver[i]]) continue;
                vis[ver[i]] = nex;
                que[tail++] = ver[i];
            }
        }
        return true;
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        int u, v, n, m;
        scanf("%d %d", &n, &m);
        for (int i = 0; i < m; i++) {
            scanf("%d %d", &u, &v);
            add(u, v);
            add(v, u);
        }
    
        for (int i = 1; i <= n; i++) {
            if (!vis[i]) {
                if (!bfs(i)) {
                    printf("-1
    ");
                    return 0;
                }
            }
        }
        printf("1
    ");
        return 0;
    }
    
  • 相关阅读:
    linux socket c : send data when socket close—SIGPIPE, Broken pipe
    (OK) server-client-pthread-c language
    (OK) Linux epoll模型—socket epoll server client chat—pthread
    (OK) pthread—epoll-loops-on-disconnection-of-a-client—server
    (OK) Linux epoll模型—socket epoll server client chat
    (OK) 刘姐实验中的大数据分析—awk—paste—system
    (OK) cBPM-CentOS—wrapped by fastcgi—files—executing commands
    (Not OK) 在CentOS7—编译nginx—for—Android—Makefile
    (Not OK) 在CentOS7—编译nginx—for—Android
    (OK) 在CentOS7—编译OpenSSL 静态库—for—Android
  • 原文地址:https://www.cnblogs.com/albert-biu/p/11542116.html
Copyright © 2020-2023  润新知