Of Groups and Rights Gym - 100247E ——树上dfs

Computer security specialist Alexey is setting up the access rights for the secret information about a certain programming contest. Security system is organized so that users are united into the groups numbered from 1 to n. Some groups can include the others. Each group can be directly included only in one other group, but one group can include several other groups. Group 1 includes all other groups directly or indirectly. If the group does not includes any other group, then it’s actually a user. And vice versa, if the group is a user, it can’t include any other groups.

For each group the access to the secret information can be allowed, denied or not set. If the access for the group is not set, it’s inherited from the group that includes this one. If access is not set for that group too, it’s inherited again and so on. If the access is not set even for the group 1, it’s considered denied for the specified group.

Alexey has instructions about the groups, the rights for them and how the groups include each other. He wants to optimize his data structure in order that only the information about the allowed rights is stored, and for other groups it’s considered not set. At the same time all users’ access rights should be the same as followed from the initial instructions. Also Alexey wants new data structure to require less memory, so there should be minimal number of the permissions records. From all such structures he wants to select that one, for which the sum of the nesting levels for all stored groups is minimal.

Input
The first line contains an integer n (1?≤?n?≤?200000) — the number of groups.

Then the string of n characters ?+?, ?-?, ?0? follows. Character ?+? on the i-th position means the access is allowed for the i-th group, ?-? — that the access is denied, and ?0? — that it’s not set.

Then n?-?1 lines follow. Each of them contains two integers separated by a space: aiand bi (1?≤?ai,?bi?≤?n). Each of these records means that group ai includes the group bi.

Output
In the first line output the only integer m — minimal number of the groups, about which allowed access information should be stored.

In the second line output m integers separated by spaces — numbers of these groups in the ascending order.

Examples
Input
10
+-+000++-+
1 2
1 5
1 9
2 6
2 8
5 4
5 10
9 3
9 7
Output
3
5 8 9
刚开始的dfs不够优秀,应该把找答案放最后。

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int to,next;
}e[200005];
int head[200005],cnt,root,leave;
void add(int x,int y)
{
    e[cnt].to=y;
    e[cnt].next=head[x];
    head[x]=cnt++;
}
char s[200005];
int ans[200005],ctans,flag[200005];
int dfs(int x,int fa)
{
    if(s[x]=='0')
        s[x]=s[fa];
    int ff=0;
    for(int i=head[x];~i;i=e[i].next)
    {
        ff+=dfs(e[i].to,x);
    }
    if(head[x]==-1&&s[x]=='-')
    {
        ff++;
    }
    if(!ff)
        flag[x]=1;
   return ff;
}
void dfs1(int x)
{
    if(flag[x]==1)
    {
        ans[++ctans]=x;
        return ;
    }
    for(int i=head[x];~i;i=e[i].next)
    {
        dfs1(e[i].to);
    }
}
int main()
{
    int n;
    memset(head,-1,sizeof(head));
    scanf("%d",&n);
    scanf("%s",s+1);
    int x,y;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        add(x,y);
    }
    root=1;
    if(s[root]=='0')
        s[root]='-';
    dfs(root,root);
    dfs1(1);
    printf("%d\n",ctans);
    sort(ans+1,ans+ctans+1);
    for(int i=1;i<=ctans;i++)
    {
        printf("%d ",ans[i]);
    }
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值