zoukankan      html  css  js  c++  java
  • 无线广播(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;
    }
    
  • 相关阅读:
    Entity Framework 和NHibernate的区别
    Windows 2008 的TCP/IP原理
    Mono 2.0正式发布了
    自定义Unity对象生命周期管理集成ADO.NET Entity Framework
    Entity Framework(EF)数据查询
    WCF采用 netTcpBinding 发生的Socket errors
    ADO.NET 实体框架概述
    IronPython 2.0 beta 5
    用sp_change_users_login消除Sql Server的孤立用户
    微软修改了Managed Extensibility Framework(MEF)的协议
  • 原文地址:https://www.cnblogs.com/albert-biu/p/11542116.html
Copyright © 2011-2022 走看看