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;
}