SGU - 321 - The Spy Network

先上題目:ide

321. The Spy Network

Time limit per test: 0.5 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard




The network of spies consists of N intelligence officers. They are numbered with the code numbers from 1 to N so that 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 that a message from any spy to Kat can reach her. All channels are unidirectional.

A channel can have one of two types: protected and almost protected. It is known that a message will not be intercepted almost surely by the hostile security service if at least half of the channels along the path to radiowoman Kat are protected. What is the minimum number of channels to be made protected from almost protected, so that any message from any spy will not be intercepted almost surely ? What are those channels?

spa

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 is described by a pair of the code numbers of spies (the direction of the channel is from the first spy to the second one) and the parameter pi. If pi = code

protected

, the channel is protected and if pi = blog

almost protected

, the channel is almost protected.

ip

Output

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

ci

Example(s)
sample input
sample output
5
5 1 almost protected
3 1 almost protected
2 3 protected
4 3 almost protected
2
1 2

 

 

 

    題意:給你一棵樹,樹的邊是有向的,每條邊有一個狀態0或者1,如今告訴你根是1,問你最少須要改變多少多少條邊的狀態才能使每一個點到達跟的路徑上有一半以上的邊是1,而且輸出數目和這些邊的編號。input

    作法:先從根節點dfs一次,對於記錄下每一個節點還須要多少個1以及這個節點以及它的子樹中的節點最多須要1的節點須要1的數目。而後在從根節點再dfs一次,此次的目的是對於當前狀態爲0的邊咱們能夠考慮是否將它轉化爲1,由於對於這種轉化來講,轉移深度小的邊對樹的影響比轉移深度大的邊對樹的影響更大,因此咱們若是一棵子樹裏面的節點還須要1的話,就轉換深度小的邊。也就是說這樣作符合貪心的思想。string

 

上代碼:it

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define MAX 200002
 5 using namespace std;
 6 
 7 typedef struct {
 8     int id,u,v,w,next;
 9 } Edge;
10 
11 Edge e[MAX];
12 char ch[100];
13 int p[MAX],need[MAX],am[MAX];
14 int tot,top;
15 
16 inline void add(int id,int u,int v,int w) {
17     e[tot].id=id;
18     e[tot].u=u;
19     e[tot].v=v;
20     e[tot].w=w;
21     e[tot].next=p[u];
22     p[u]=tot++;
23 }
24 
25 void dfs1(int r,int dep,int a) {
26     am[r]=(dep+1)/2 - a;
27     for(int i=p[r]; i!=-1; i=e[i].next) {
28         dfs1(e[i].v,dep+1,a+e[i].w);
29         am[r]=max(am[e[i].v],am[r]);
30     }
31 }
32 
33 void dfs2(int r,int num) {
34     for(int i=p[r]; i!=-1; i=e[i].next) {
35         if(num<am[e[i].v]) {
36             if(e[i].w==0) {
37                 need[top++]=e[i].id;
38                 dfs2(e[i].v,num+1);
39             } else dfs2(e[i].v,num);
40         }
41     }
42 }
43 
44 int main() {
45     int n,u,v;
46     //freopen("data.txt","r",stdin);
47     while(~scanf("%d",&n)) {
48         memset(p,-1,sizeof(p));
49         tot=top=0;
50         for(int i=1; i<n; i++) {
51             scanf("%d %d %s",&v,&u,ch);
52             if(ch[0]=='a') {
53                 scanf("%s",ch);
54                 add(i,u,v,0);
55             } else {
56                 add(i,u,v,1);
57             }
58         }
59         dfs1(1,0,0);
60         dfs2(1,0);
61         printf("%d\n",top);
62         for(int i=0; i<top; i++) {
63             if(i) printf(" ");
64             printf("%d",need[i]);
65         }
66         printf("\n");
67     }
68     return 0;
69 }
/*321*/
相關文章
相關標籤/搜索