Logu P2661 information transmission and logu p2661 Information Transmission

Source: Internet
Author: User

Logu P2661 information transmission and logu p2661 Information Transmission

This question has been done a long time ago. After a few months, I still don't know about it.

First, consider each person as a node and generate a directed graph from the side that each person wants to tell him. Obviously, if a person is not on the ring, then it will never be able to hear his own information; if a person is on the ring, then you will hear your information after the round "the length of the ring containing the smallest length of this point. Now, it is required that everyone hear the minimum number of turns, that is, the minimum ring of the graph.

The bare minimum ring should be floyed, $ O (n ^ 3) $. Obviously, this is not the case. One thing you can note is that each vertex in the graph has at most one outbound edge. Then, it is obvious that a point cannot appear in multiple loops. That is to say, a point is either not in the ring or only in a separate ring. Therefore, you can first remove the points not on the ring. For each ring, you only need to start from any point on the ring and find the dfs to return to its path length.

How can we remove points not on the ring? Topology Sorting is supported. If the topology is sorted, only the point on the ring is left.

Error records:

1. deque is not used, and 47 rows are written as pop_back ()

2. Incorrect question meaning during Question writing, and 58 lines are written as max

1 // # pragma comment (linker, "/STACK: 102420.,10240000") 2 # include <cstdio> 3 // # pragma GCC optimize (2) 4 # include <deque> 5 # include <malloc. h> 6 using namespace std; 7 struct E 8 {9 int to, nxt; 10} e [400100]; 11 int f1 [200100], ne, in [200100]; 12 deque <int> q; 13 bool boo [200100]; 14 int n, ans = 0x3f3f3f; 15 int dfs (int x) 16 {17 boo [x] = 1; 18 // if (dep = 43429) 19 // {20 // printf ("1"); 21 //} 22 for (int k = f1 [X]; k! = 0; k = e [k]. nxt) 23 if (! Boo [e [k]. to]) 24 return dfs (e [k]. to) + 1; 25} 26 int main () 27 {28 // int size = 256 <20; // 256 MB // you may need to manually expand the stack on windows, otherwise, stack 29 // char * p = (char *) malloc (size) + size; 30 // _ asm _ ("movl % 0, % esp \ n ":" r "(p); 31 // freopen (" testdata. in "," r ", stdin); 32 int I, a, t, k; 33 scanf (" % d ", & n); 34 for (I = 1; I <= n; I ++) 35 {36 scanf ("% d", & a); 37 e [++ ne]. to = a; 38 e [ne]. nxt = f1 [I]; 39 f1 [I] = ne; 40 in [a] ++; 41} 42 for (I = 1; I <= n; I ++) 4 3 if (in [I] = 0) 44 q. push_back (I); 45 while (! Q. empty () 46 {47 t = q. front (); q. pop_front (); 48 boo [t] = 1; 49 for (k = f1 [t]; k! = 0; k = e [k]. nxt) 50 {51 in [e [k]. to] --; 52 if (in [e [k]. to] = 0) 53 q. push_back (e [k]. to); 54} 55} 56 for (I = 1; I <= n; I ++) 57 if (! Boo [I]) 58 ans = min (ans, dfs (I) + 1); 59 printf ("% d", ans); 60 return 0; 61}

Related Article

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.