sgu 321 The Spy Network (dfs+貪心)

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   io   strong   for   ar   

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 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?

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 = 

protected

, the channel is protected and if pi = 

almost protected

, the channel is almost protected.

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.

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

 

 

題意:一棵結點數為n的樹,給出n-1條邊,每條邊有一個屬性,0(almost protect)和1(protect),修改某些邊的屬性使得滿足樹上每一個結點到根節點的路徑上,屬性為1的邊的數量大於等於屬性為0的邊的數量,求最小運算元

 

思路:首先要修改的邊,應該離根節點越近越好,越近的話,對其他結點的貢獻越多。所以用dfs(u)表示u的所有子節點中最多需要的1的邊的數量,當這個數量大於u的深度的時候,就表示u前面全部修改了也不夠滿足條件,這時候就看u和下一個結點串連的邊的屬性是1還是0,是0的話,就要修改這條邊,加進答案裡面。dfs掃一遍就行了

 

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#include <map>#include <utility>#include <queue>#include <stack>using namespace std;const int INF=1<<30;const double eps=1e-6;const int N = 200010;struct _edge{    int u,v,next;    bool w;};_edge e[N];int first[N],n;vector<int> ans;int dfs(int u,int dep){    int res = (dep+1)/2;    for(int i=first[u];i!=-1;i=e[i].next)    {        int v = e[i].v;        int tmp = dfs(v,dep+1);        if(e[i].w) tmp--;        else if(tmp>dep)        {            ans.push_back(i);            tmp--;        }        res = max(res,tmp);    }    return res;}void run(){    int u,v;    char s[20];    memset(first,-1,sizeof(first));    for(int i=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);        else            e[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(int i=1;i<ans.size();i++)        printf(" %d",ans[i]);    puts("");}int main(){    freopen("case.txt","r",stdin);    while(scanf("%d",&n)!=EOF)        run();    return 0;}

 

sgu 321 The Spy Network (dfs+貪心)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.