SGU 321 the Spy Network (dfs+ greedy)

Source: Internet
Author: User

321. The Spy networktime limit per test:0.5 second (s)
Memory limit:65536 Kilobytesinput:standard
Output:standard




The network of Spies consists of N intelligence officers. They is numbered with the code numbers from 1 to N so, nobody could discover them. The number 1 belongs to the Radiowoman Kat. There is exactly N -1 communication channels between the spies. It is known this a message from any spy to Kat can reach her. All channels is unidirectional.

A Channel can has one of the types:protected and almost protected. It is known that a message won't be intercepted almost surely by the hostile security service if at least half of the C Hannels along the path to Radiowoman Kat is protected. What's the minimum number of channels to being made protected from almost protected, so, a message from any spy would n OT be intercepted almost surely? What is those channels?

Input

The first line of the input contains the integer number n (1≤ n ≤200000). The following N -1 lines contain the description of the communication channels. Each channel was described by a pair of the code numbers of the spies (the direction of the channel are from the first spy to th e second one) and the parameter pI. If PI =

Protected

, the channel is protected and if pI =

Almost protected

, the channel is almost protected.

Output

Write the number of channels to is converted to protected to the first line of the output. To the next line write numbers of channels to be made protected. If There is several solutions, choose any of them.

Example (s)
Sample input
Sample output
1 almost protected3 1 almost protected2 3 protected4 3 almost protected
21 2

Test instructions: A tree with a node number of N, gives N-1 edge, each edge has an attribute, 0 (almost protect) and 1 (protect), modify the properties of certain edges so that each node in the tree satisfies the path of the root node, the quantity of the edge with attribute 1 is greater than the number of edges equal to 0 of the property. To find the minimum number of operations

Idea: First to modify the edge, should be closer to the root node better, the closer the words, to other nodes contribute more. So with DFS (U) to represent all the child nodes of U up to 1 of the number of edges, when the number is greater than the depth of u, it means that you all modify the front is not enough to meet the conditions, this time you and the next node is connected to the edge of the property is 1 or 0, is 0, it is necessary to modify the side DFS sweeps it all over again.

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<map>#include<utility>#include<queue>#include<stack>using namespacestd;Const intinf=1<< -;Const Doubleeps=1e-6;Const intN =200010;struct_edge{intU,v,next; BOOLW;}; _edge E[n];intFirst[n],n;vector<int>ans;intDfsintUintDEP) {    intres = (dep+1)/2;  for(inti=first[u];i!=-1; i=E[i].next) {        intv =e[i].v; intTMP = DFS (v,dep+1); if(E[I].W) tmp--; Else if(tmp>DEP)            {Ans.push_back (i); TMP--; } Res=Max (res,tmp); }    returnRes;}voidrun () {intu,v; Chars[ -]; memset (First,-1,sizeof(first));  for(intI=1; i<n;i++) {scanf ("%d%d",&v,&u); E[I].U=u; E[I].V=v; scanf ("%s", s); if(s[0]=='a') E[I].W=0, scanf ("%s", s); ElseE[I].W=1; E[i].next=First[u]; First[u]=i; }//for (int i=1;i<n;i++)//cout<<e[i].u << ' << e[i].v << ' << e[i].w<<endl;ans.clear (); DFS (1,0); printf ("%d\n", Ans.size ()); if(!ans.empty ()) printf ("%d", ans[0]);  for(intI=1; I<ans.size (); i++) printf ("%d", Ans[i]); Puts ("");}intMain () {Freopen ("Case.txt","R", stdin);  while(SCANF ("%d", &n)! =EOF) run (); return 0;}

SGU 321 the Spy Network (dfs+ greedy)

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.