Practice 3383:cell Phone Network

Source: Internet
Author: User

Portal: http://bailian.openjudge.cn/practice/3383/

Exercises

The topic is the minimum dominating set.

Learn the solution to the minimum dominating set:

Tree DP (free to push)

Greedy: DFS traversal after the inverse DFS sequence is processed, if the current point is not at the dominating set and does not have a dominating set of edges, then mark it as a father for the dominating set member and process father and self (marked and dominating the set of edges)

The specific implementation uses an array of records to dictate whether an array record is dominant set or connected over edge.

# include <stdio.h># include<string.h># include<iostream># include<algorithm>//# include <bits/stdc++.h>using namespaceStd;typedefLong LongLl;typedefLong Doubleld;typedef unsignedLong Longull;Const intM = 5e5 +Ten;Const intMoD = 1e9+7; # define RG register# define STStatic/*First select a point for the root, and then follow the depth of the first traversal to get a traversal sequence, according to the order of the sequence of the resulting series greedy, for a point that does not belong to the dominant set is not associated with the point of control set, if his parent node does not belong to the dominant set, the parent node into the dominating set*/intN;intHEAD[M], nxt[m], to[m], tot=0;BOOLS[m], is[M];//has been covered yet (in set or connected with ... in set), in setinlinevoidAddintUintv) {++tot; Nxt[tot] =Head[u]; Head[u]= tot; To[tot] =v;} InlinevoidAdde (intUintv) {Add (U, v), add (V, u);}intDfn[m], dfn=0, fa[m]; inlinevoidDfsintXintfat) {    ++DFN; FA[X] =fat; DFN[DFN]=x;  for(intI=HEAD[X]; I I=Nxt[i])if(To[i]! =fat) DFS (To[i], x); }intMain () { while(Cin >>N) {memset (head,0,sizeofHead); tot =0; DFN=0; memset (s),0,sizeofs); Memset is,0,sizeof  is); memset (FA,0,sizeofFA);  for(intI=1, u, v; i<n; ++i) {scanf ("%d%d", &u, &v);          Adde (U, v); } DFS (1,1);  for(intI=n; i>=1; --i) {intx =Dfn[i]; if(!S[x]) {                if(! is[Fa[x]]) is[Fa[x]] =1; S[X]=1; S[FA[X]] =1; S[FA[FA[X]] =1; }        }        intAns =0;  for(intI=1; i<=n; ++i) ans + = is[i]; if(n = =1) puts ("1"); Elseprintf"%d\n", ans); }    return 0;}
View Code

Practice 3383:cell Phone Network

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.