無線廣播(Broadcast)


清華OJ——數據結構與算法實驗(中國石油大學)

無線廣播(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

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #define N 30005
 5 using namespace std;
 6 
 7 struct ss
 8 {
 9     int v,next;
10 };
11 ss edg[2*N];
12 int head[N],sum_edge=0;
13 
14 void addedge(int u,int v)
15 {
16     edg[sum_edge]=(ss){v,head[u]};
17     head[u]=sum_edge++;
18 }
19 
20 int read()
21 {
22     int now=0;
23     char ch=getchar();
24     while(!(ch>='0'&&ch<='9'))ch=getchar();
25     while(ch>='0'&&ch<='9')
26     {
27         now=now*10+ch-'0';
28         ch=getchar();
29     }
30     return now;
31 }
32 
33 int color[N]={0};
34 
35 int dfs(int x,int now_color)
36 {
37     if(color[x]&&color[x]!=now_color)return 0;
38     if(color[x]&&color[x]==now_color)return 1;
39     color[x]=now_color;
40     
41     for(int i=head[x];i!=-1;i=edg[i].next)
42     {
43         int v=edg[i].v;
44         if(!dfs(v,now_color*-1))return 0;
45     }
46     return 1;
47 }
48 
49 
50 int main()
51 {
52     memset(head,-1,sizeof(head));
53     int n,m,u,v;
54     n=read();
55     m=read();
56     
57     while(m--)
58     {
59         u=read();
60         v=read();
61         addedge(u,v);
62         addedge(v,u);
63     } 
64     
65     for(int i=1;i<=n;i++)
66     if(!color[i]&&!dfs(i,1))
67     {
68         printf("-1\n");
69         return 0;
70     }    
71     printf("1\n");
72     return 0;
73 } 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM