First on the topic:
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: Give you a tree, the edge of the tree is a direction, each side has a state 0 or 1, now tell you that the root is 1, ask you at least how many sides need to change the state to make each point to reach the path of more than half of the edge is 1, and the number of outputs and the number of these edges.
Procedure: First Dfs from the root node, the number of nodes required to record each node 1 and the node and the node in its subtree requires a maximum of 1 nodes need 1. Then at the end of the DFS from the root node again, this time the purpose is for the current state of 0 side we can consider whether to convert it to 1, because for this conversion, the transfer depth of the edge of the tree is more than the impact of the large transfer depth of the effect of the tree, so we if a subtrees tree nodes also need 1, A small edge is converted to a smaller depth. In other words, this is in line with the greedy mind.
On the code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMAX 2000025 using namespacestd;6 7typedefstruct {8 intId,u,v,w,next;9 } Edge;Ten One Edge E[max]; A Charch[ -]; - intP[max],need[max],am[max]; - intTot,top; the -InlinevoidAddintIdintUintVintW) { -E[tot].id=ID; -e[tot].u=u; +e[tot].v=v; -e[tot].w=W; +e[tot].next=P[u]; Ap[u]=tot++; at } - - voidDFS1 (intRintDepinta) { -Am[r]= (dep+1)/2-A; - for(intI=P[R]; i!=-1; I=E[i].next) { -DFS1 (e[i].v,dep+1, A +E[I].W); inam[r]=Max (Am[e[i].v],am[r]); - } to } + - voidDFS2 (intRintnum) { the for(intI=P[R]; i!=-1; I=E[i].next) { * if(num<AM[E[I].V]) { $ if(e[i].w==0) {Panax Notoginsengneed[top++]=e[i].id; -DFS2 (e[i].v,num+1); the}ElseDFS2 (e[i].v,num); + } A } the } + - intMain () { $ intn,u,v; $ //freopen ("Data.txt", "R", stdin); - while(~SCANF ("%d",&N)) { -memset (p,-1,sizeof(P)); thetot=top=0; - for(intI=1; i<n; i++) {Wuyiscanf"%d%d%s",&v,&u,ch); the if(ch[0]=='a') { -scanf"%s", ch); WuAdd (I,u,v,0); -}Else { AboutAdd (I,u,v,1); $ } - } -DFS1 (1,0,0); -DFS2 (1,0); Aprintf"%d\n", top); + for(intI=0; i<top; i++) { the if(i) printf (" "); -printf"%d", Need[i]); $ } theprintf"\ n"); the } the return 0; the}
/*321*/
Sgu-321-the Spy Network