链接
http://acm.hdu.edu.cn/showproblem.php?pid=5963
题解
如果和根节点相连的边中,边权为
1
1
1的有奇数个,那就女孩嬴,否则男孩赢
如果一条和根相连的边边权为
1
1
1,因为最终它要变成
0
0
0,所以这子树中肯定要操作奇数次;如果一条边的边权为
0
0
0,那么肯定操作偶数次
那么两个人操作次数的奇偶性是一定的,谁输谁赢不就决定了吗?
代码
#include <bits/stdc++.h>
#define maxn 40010
using namespace std;
typedef pair<int,int> pii;
int cnt[maxn];
set<pii> e[2];
int main()
{
ios::sync_with_stdio(false);
int T, n, m, i, type, x, y, z;
cin>>T;
while(T--)
{
memset(cnt,0,sizeof(cnt));
e[0].clear(), e[1].clear();
cin>>n>>m;
for(i=1;i<n;i++)
{
cin>>x>>y>>z;
if(x>y)swap(x,y);
e[z].emplace(pii(x,y));
cnt[x]^=z, cnt[y]^=z;
}
while(m--)
{
cin>>type;
if(type==0)
{
cin>>x;
if(cnt[x])cout<<"Girls win!\n";
else cout<<"Boys win!\n";
}
else
{
cin>>x>>y>>z;
if(x>y)swap(x,y);
auto it=e[!z].find(pii(x,y));
if(it!=e[!z].end())
{
cnt[x]^=1;
cnt[y]^=1;
e[!z].erase(it);
e[z].emplace(pii(x,y));
}
}
}
}
return 0;
}