Uestc_ Qiu Teacher's brain residue UESTC Training for Graph Theory<problem d>

Source: Internet
Author: User

D-Miss Qiu's Brain Powder Time limit:12000/4000ms (java/others) Memory limit:65535/65535kb (java/others)SubmitStatus

Miss Qiu's fans are numerous, every day Qiu teachers have to face the countless invitations to fans, Qiu teacher alone can not handle, so I would like to ask you to help.

Suppose that one day there are N fans want to and Miss Qiu, once a fan has successfully asked Miss Qiu, she will tweet and friends show off. Once a fan finds information about Yuchu's teacher on Weibo, she will forward it.

If a fan of Miss Qiu was found to have other fans and Miss Qiu's date in the microblog, she would be embarrassed and Miss Qiu would be embarrassed. In order to avoid this embarrassing situation, Miss Qiu can only choose from N fans a part of the day.

But in order to satisfy the wishes of more fans, Miss Qiu can select up to two fans from the date fans and confuse her to stop using Weibo (neither viewing the message nor forwarding the message), and the fans who are dating Miss Qiu seem to have changed a lot. But which two fans will be chosen to make the biggest number of fans who can date Miss Qiu?

Input

First row two integersN,M, indicating that the day hasn fans want to have a date with Miss Qiu and fans have a Span class= "Mathjax_preview" >m The bar relationship. (1≤n,< Span id= "mathjax-span-929" class= "Mi" >m≤ 5000 )

Next m line, each line represents two numbersu,v Represents the firstu fans and v The fan Weibo is a mutual concern. (1≤u,< Span id= "mathjax-span-949" class= "Mi" >v≤ n )

(Mutual concern refers to: U message < Span id= "mathjax-span-955" class= "math" > v Can see, V message < Span id= "mathjax-span-961" class= "math" > u can see)

Data guarantees no heavy edges and self-loops

Output

Output how many fans miss Qiu can date the day.

Sample Input and output
Sample Input Sample Output
5 41 2 1 31 44 5
5
Hint

In the example, if you don't confuse a fan, Miss Qiu can only date with 1 fans on the day (such as a date with 2nd fans), because 2nd fans will send Weibo show, 1th fans will be forwarded after the notice, so that the 3rd fans and 4th fans see the forwarding of 1th will also be forwarded, so that the 5th fans also from the 4th fans saw the 2nd hair of the Weibo.

But if Miss Qiu puzzled 1th fans and 4th fans to let them stop using Weibo the same day, Qiu will be able to date with 5 fans the same day spicy!

Problem Solving Report:

The data is very water, the subject of data is very water, the data is very water, important things say three times, you can try two direct greed, can be about 2ms

The subject is a Tarjan algorithm.

First we need to enumerate the first point, why? Consider the following scenario:

This figure is both the side of the two connected, but also the point of double connectivity, the first point of the program is not able to determine which is better.

So our first point takes the form of an enumeration.

So what's the second point?

Notice that after the second point we can not continue to operate, so the 2nd choice we use greed.

In this way, the algorithm framework comes out:

    1. Enumerate each possible first point
    2. To cut this point after the figure run Tarjan, here I use is cut bridge, if U–v is cut bridge, then Val[u] + +, Val[v] + +
    3. We remove the point with the largest Val value, then run the connected component and update the answer

Complexity: O (n * (n + M) * 2)

#include <iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<vector>#definePB Push_backusing namespacestd;Const intMAXN = 5e3 + -; Vector<int>E[MAXN];intN,M,T,NEW_TIME[MAXN],LOW[MAXN];intIS_CUT[MAXN], cut_vertx1, cut_vertx2, ans =0, PTR;BOOLVIS[MAXN];//Dye CollectionintTarjan_dfs (intCurintpre) {New_time[cur]= Low[cur] = t++;  for(inti =0; I < e[cur].size (); ++i) {intNextNode =E[cur][i]; if(NextNode = = cut_vertx1)Continue;//The first removed point of the enumerationPTR =cur; if(!new_time[nextnode])//Tree Side         {             intLOWV =Tarjan_dfs (nextnode,cur); if(Lowv >New_time[cur]) {Is_cut[cur]+ +, Is_cut[nextnode] + + ; } Low[cur]=min (LOW[CUR],LOWV); }        Else if(New_time[nextnode] < New_time[cur] && nextnode! = Pre)//Reverse EdgeLow[cur] =min (Low[cur],new_time[nextnode]); }   returnlow[cur];}voidColour_map (intcur) {Vis[cur]=true;  for(inti =0; I < e[cur].size (); ++i) {intNextNode =E[cur][i]; if(!vis[nextnode] && nextnode! = cut_vertx1 && NextNode! =cut_vertx2) Colour_map (NextNode); }}voidUpdata_ans (intXinty) {cut_vertx1= x, cut_vertx2 =y; inttot =0; memset (Vis,false,sizeof(VIS));  for(inti =1; I <= N; ++i)if(!Vis[i]) {         if(I! = cut_vertx1 && I! =cut_vertx2) Colour_map (i); Tot++; } ans=Max (Ans,tot);}voidSlove () { for(inttt =1; TT <= N; ++TT) {cut_vertx1=tt; memset (Is_cut,0,sizeof(Is_cut)); memset (New_time,0,sizeof(New_time)); T=1; if(TT = =1) PTR=2; Elseptr=1;  for(inti =1; I <= N; ++i)if(!new_time[i] && i! =cut_vertx1) Tarjan_dfs (i,0); intMaxLen =0;  for(inti =1; I <= N; ++i)if(Is_cut[i] >=maxlen) {MaxLen=Is_cut[i]; PTR=i;   } Updata_ans (TT,PTR); }}intMainintargcChar*argv[]) {scanf ("%d%d",&n,&m);  for(inti =1; I <= m; ++i) {intu,v; scanf ("%d%d",&u,&v);   E[U].PB (v), E[V].PB (U);  } slove (); printf ("%d\n", ans); return 0;}

Uestc_ Qiu Teacher's brain residue UESTC Training for Graph Theory<problem d>

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.