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;
}
静渊以有谋,疏通而知事。
原文地址:https://www.cnblogs.com/autoint/p/9818273.html