1 solutions

  • 1
    @ 2025-8-25 9:13:53
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    vector<int> g[N];
    bool w[N];
    int s[N]; 
    void dfs(int u,int now) //遍历以u为根的子树 
    { 
    	now+=s[u]; //累加操作次数 
    	if(now&1) w[u]=!w[u]; //操作了奇数次 
    	for(auto x:g[u]) //遍历所有的孩子节点 
    	{
    		dfs(x,now);
    	}
    }
    int main()
    {
    	int n;
    	cin>>n;
    	for(int i=1;i<n;i++) //构建树 
    	{
    		int x;
    		cin>>x;
    		g[x].push_back(i+1);
    	}
    	for(int i=1;i<=n;i++) //获取节点颜色 
    	{
    		char ch;
    		cin>>ch;
    		w[i]=ch-'0';
    	}
    	int q;
    	cin>>q;
    	while(q--) //处理每个操作 
    	{
    		int x;
    		cin>>x;
    		s[x]++;
    	}
    	dfs(1,0);//开始遍历 
    	for(int i=1;i<=n;i++)
    	{
    		cout<<w[i];
    	}
    	return 0;
    }
    
    
    • 1

    Information

    ID
    2188
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    # Submissions
    13
    Accepted
    2
    Uploaded By