ACM/ICPC Data Structures-adjacency table +BFS (Tshinghua oj-radio broadcast)

Source: Internet
Author: User

If this problem can constitute the area of non-interference, its composition of the figure is actually Hamilton Road, So if can be observed directly into the existence of Hamilton Road proof, even if not observed, I believe that Acmer can also be converted into BFS problem, this problem is a good graph theory, it is helpful to examine the basic skills of their own graph theory.

Wireless broadcast (broadcast) Description

A broadcasting company is to set up a radio transmitting device in a region. There are a total of n towns in the area, and each town has to install a transmitter and broadcast its own program.

However, the company only received authorization from the FM104.2 and FM98.6 two bands, while the launch opportunities in the same band interfered with one another. It is known that the signal coverage of each transmitter is centered on it, and the 20km is a circular area with a radius, so if two smaller towns with a distance of less than 20km use the same bands, they will not be able to listen to the program properly due to band interference. Now give the list of smaller towns with distances less than 20km, and try to determine whether the company can make the entire region's residents hear the radio program normally.

input

The first behavior is two integer n,m, which is the number of small towns and the small town pairs that are next less than 20km. The next M-line, 2 integers per line, indicates that the distance between the two towns is less than 20km (numbering starts from 1).

Output

If the requirement is met, Output 1, otherwise output-1.

Input Sample
4 31 21 32 4
Output Sample
1
Limit

1≤n≤10000

1≤m≤30000

There is no need to consider the spatial characteristics of a given 20km town list, such as whether or not to satisfy the triangular inequalities, whether the use of transitivity can introduce more information and so on.

Time: 2 sec

Space: 256MB

Tips

BFS

The topic will be a pair of small town distance less than 20Km simulation for a pair of undirected edge nodes , so that you can generate a multi-connected (not necessarily) undirected graph, the original problem requires that the small town can not place the same frequency of broadcast, so you can compare the problem The hierarchical traversal problem of tree-- the data of parent node and sub-node cannot be the same, can be transformed into the breadth-first search problem of graph --the data of node and its adjacency point can not be the same (using the BFS layer to extend outward and mark, find a pair of adjacency point data same return false, Returns true if all tokens are successful).

In particular: To queue any point as the source point (labeled 1), outward all its adjacency points into the queue (marked as-1), and then the source point out of the team, and then take the first point of all the adjacent points of the queue (marked as 1), this judgment has no adjacency points for the first line with the mark, there is the return false ...

Specific as follows:

  

1 //adjacency Table +bfs-Proof of existence similar to Hamilton Road2 //here, 1 marks the source point, -1 marks the adjacency point, 1 marks the adjacency point of the adjacency point, and so on. (There is no contradiction exists)3 //time:39ms memory:13760kb (No.8)4#include <iostream>5#include <cstring>6#include <cstdio>7 using namespacestd;8 9 #defineMAX 10005Ten  One intN, M;//number of small towns-within 20Km distance of small town A intQueue[max],head,tail;//Analog Queue-team head-tail - intCover//Broadcast Placing Quantity -  the //Nexttown - structnode{ -     intnum; -Node *Next; +Node () {next =NULL;} -Node (intN, Node *nn): num (n), next (NN) {} + }; A  at //Small Town - structtown{ -     intState//Status -Node *nt;//Nexttown -Town () {state =0; NT =NULL;} -     voidInsertintnum); in }town[max]; -  to /*Insert New Edge*/ + voidTown::insert (intnum) - { the     if( This->nt = =NULL) *          This->nt =NewNode (num,null); $     Else  This->nt =NewNode (NUM, This-NT);Panax Notoginseng } -  the BOOLBFS (intx) + { Aqueue[tail++] =x; theTown[x].state =1; +cover++;//no.x was cover -      while(Head <tail) $     { $Town cur = town[queue[head]];//Current Small Town -Node *tmp = Cur.nt;//point to Nexttown -          while(TMP! =NULL) the         { -             if(!town[tmp->num].state) {WuyiTown[tmp->num].state =-cur.state;//cover different broadcast thecover++;//No. (tmp->num) was cover -queue[tail++] = tmp->num; Wu             } -             Else if(town[tmp->num].state = = cur.state)//be disturbed About                 return false; $TMP = tmp->Next; -         } -head++; -     } A     return true; + } the  - intMain () $ { thescanf"%d%d", &n, &m); the      for(inti =0; I < m; i++) the     { the         intx, y; -scanf"%d%d", &x, &y);//d (x, y) <20km in Town[x].insert (y); the Town[y].insert (x); the     } About      for(inti =1; I <= N; i++) the     { the         if(!town[i].state) the         { +             if(BFS (i) = =false)//call bfs-signal is disturbed -             { theprintf"-1\n");Bayi                 return 0; the             } the             if(cover = = N)//Place (placement) Complete -             { -printf"1\n"); the                 return 0; the             } the         } the     } -  the     return 0; the}
small Ink = = Original

ACM/ICPC Data Structures-adjacency table +BFS (Tshinghua oj-radio broadcast)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.