zoukankan      html  css  js  c++  java
  • test20181019 B君的第一题

    题意


    分析

    考场做法同标解。

    画图模拟分析发现,无论操作顺序怎样,操作数的奇偶性是不变的。
    所以等同求出,以每点为根的操作数奇偶性。

    (f(x))表示x及其子树中的边,包括x到它fa的边,将他们全部置0的操作数。
    (f(x))(sum_{y in son(x)}f(y))的奇偶性有关,但是分4种情况讨论又可以发现,其实f(x)只跟x到fa这条边有关。

    那么每点换成根之后,总操作数的奇偶性只跟根节点周围的边的奇偶性有关,于是可以(O(n))做。

    代码

    #include<cstdlib>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<list>
    #include<deque>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<bitset>
    #include<algorithm>
    #include<complex>
    #define rg register
    #define il inline
    #define co const
    #pragma GCC optimize ("O0")
    using namespace std;
    template<class T> il T read(T&x)
    {
        T data=0;
    	int w=1;
        char ch=getchar();
        while(!isdigit(ch))
        {
    		if(ch=='-')
    			w=-1;
    		ch=getchar();
    	}
        while(isdigit(ch))
            data=10*data+ch-'0',ch=getchar();
        return x=data*w;
    }
    typedef long long ll;
    const int INF=0x7fffffff;
    
    const int MAXN=1e5+7;
    int val[MAXN];
    
    int main()
    {
      freopen("hohhot.in","r",stdin);
      freopen("hohhot.out","w",stdout);
    	int n;
    	read(n);
    	for(int i=1;i<n;++i)
    	{
    		int x,y,w;
    		read(x);read(y);read(w);
    		val[x]+=w;
    		val[y]+=w;
    	}
    	for(int i=1;i<=n;++i)
    	{
    		printf("%d
    ",val[i]%2);
    	}
    //  fclose(stdin);
    //  fclose(stdout);
        return 0;
    }
    
    静渊以有谋,疏通而知事。
  • 相关阅读:
    第3章 神经网络
    OpenCV基础(一)---图像卷积运算
    OpenCV-自定义harris检测
    C++类型转换
    剑指offer之【二叉搜索树与双向链表】
    剑指offer之【复杂链表的复制】
    剑指offer之【二叉树中和为某一值的路径】
    剑指offer之【二叉搜索树的后序遍历序列】
    剑指offer之【从上往下打印二叉树】
    剑指offer之【栈的压入、弹出序列】
  • 原文地址:https://www.cnblogs.com/autoint/p/9818273.html
Copyright © 2011-2022 走看看