UVa10859 Placing street lights

Source: Internet
Author: User

placing lampposts

As a part of the mission? Beautification of Dhaka City, the government have decided to replace all the old lampposts with new expensive ones. Since the new ones is quite expensive and the budget was not up to the requirement, the Government have decided to buy the Minimum number of lampposts required to light the whole city.

Dhaka City can be modeled as a undirected graph with no cycles, multi-edges or loops. There is several roads and junctions. A lamppost can only is placed on junctions. These lampposts can emit light in all the directions, and which means a lamppost that's placed in a junction would light Al L The roads leading away from it.

The? Dhaka City Corporation? Have given you the road map of Dhaka City. You is hired to find the minimum number of lampposts, that'll be required to light, the whole city. These lampposts can then is placed on the required junctions to provide the service. There could is many combinations of placing these lampposts that would cover all the roads. In this case, you had to place them in such a, the the number of roads receiving light from the. Lampposts is maximize D.

Input

There'll is several cases in the input file. The first line of input would contain an integer T (t<=30) that would determine the number of test cases. Each case would start with a integers N (n<=1000) and M (m<n) that would indicate the number of June Ctions and roads respectively. The junctions is numbered from 0 to N-1. Each of the next M lines would contain, integers a and b, which implies there is a road from Junction a to b,
(0<=, b < N) and A! = B. There is a blank line separating the consecutive input sets.

Output

For each line of input, there'll be a line of output. Each output line would contain 3 integers, with one space separating and one consecutive numbers. The first of these integers would indicate the minimum number of lampposts required to light the whole city. The second integer would be is the number of roads that is receiving lights from the lampposts and the third integer would be The number of roads that is receiving light from only one lamppost.

Sample Input

2
4 3
0 1
1 2
2 3

5 4
0 1
0 2
0 3
0 4

Sample Output

2 1 2
1 0 4

Main topic: Given a non-circular diagram, the need to put a light on the point, if a point on the other, you can illuminate the side with it, now ask to put as little as possible, so that all the edges are illuminated, and the number of output lights, illuminated two times the number of sides (that is, the edge of the two endpoints are placed lights), is illuminated If you wait for the same number, follow the plan that is illuminated one side larger.

Problem Solving Ideas:

The optimization target has two, make the lamp number A as small as possible, make the number of sides of two lamps as much as possible

Can be converted here, is just a lamp illuminated by the number of sides C as little as possible

<IMPORTANT> when we need to optimize two variable a,c at the same time, it requires the first to meet the minimum of a case, make C as small as possible

We can introduce a value of X=ma+c, where M is a very large value, a value larger than the difference between the theoretical maximum of C and the minimum theoretical value of a.

Why? Because if two A is different, no matter how much C differs, it still plays a decisive role.

Our goal is to make the value of X as small as possible, then the integer part of the x/m is the number of lights, x%m is only lit by a lamp, m-x%m is illuminated by two lights

We make dp[i][j] represent the minimum value of x on the I node (j=1 means that the Father node lights up, 0 is the reverse)

So for I, there are two decisions

If I put the light on

Then dp[i][j] = SUM (dp[k][1]) +m,k is the child node of I. If j=0 and I are not root, also +1, because I and his father node this side is only illuminated by a lamp

If I do not put the lamp, must j=1 (otherwise the side will not be illuminated)

Then dp[i][j] = SUM (dp[k][0]), if I is not the root, add 1, because I and his father node this side is only illuminated by a lamp

The result can be obtained by the idea of the side DFS side DP.

Another diagram may have multiple connected components

The code is as follows:

#include <iostream>#include<cstdio>#include<vector>#include<cstring>#defineSize 1005#defineM 2000using namespacestd;intd[size][2];BOOLvis[size][2];vector<int>Edge[size];intn,m;intdpintIintJintf) {    if(vis[i][j]==true)returnD[i][j]; VIS[I][J]=true; intk=edge[i].size (); //Yes    int& ans=D[i][j]; Ans=l;  for(intCh=0; ch<k;ch++){        if(edge[i][ch]!=f) {ans+=DP (Edge[i][ch],1, i); }    }    if(j==0&&f!=-1) ans++; //No    if(j==1|| f==-1){            intAns2=0;  for(intCh=0; ch<k;ch++){            if(edge[i][ch]!=f) {Ans2+=DP (Edge[i][ch],0, i); }        }        if(f!=-1) ans2++; Ans=min (ans,ans2); }        returnans;}intMain () {Freopen ("30.in","R", stdin); intT Cin>>T;  while(t--){         for(intI=0; i<=n;i++) edge[i].clear (); memset (Vis,false,sizeof(VIS)); CIN>>n>>m; intb;  for(intI=0; i<m;i++) {cin>>a>>b;            Edge[a].push_back (b);        Edge[b].push_back (a); }                intans=0;  for(intI=0; i<n;i++){            if(vis[i][0]==false) {ans+=DP (I,0,-1);//1 means no parent node and I is the root node}} cout<<ans/M<<' '<<m-ans%M<<' '<<ans%M<<Endl;     } fclose (stdin); return 0; }

UVa10859 Placing street lights

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.