无线广播(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\n");
                return 0;
            }
        }
    }
    printf("1\n");
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值