test20181019 B君的第一题

题意

test20181019 B君的第一题

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\n",val[i]%2);
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
上一篇:[算法 笔记]2014年去哪儿网 开发笔试(续)第一题BUG修正


下一篇:ASP.NET MVC 下载列表