Problem Description
dingyeye loves play stone game with you.
dingyeye has an n -point tree.The nodes are numbered from 0 to n−1 ,while the root is numbered 0 .Initially,there are a[i] stones on the i -th node.The game is in turns.When one move,he can choose a node and move some(this number cannot be 0 ) of the stones on it to its father.One loses the game if he can't do anything when he moves.
You always move first.You want to know whether you can win the game if you play optimally.
dingyeye has an n -point tree.The nodes are numbered from 0 to n−1 ,while the root is numbered 0 .Initially,there are a[i] stones on the i -th node.The game is in turns.When one move,he can choose a node and move some(this number cannot be 0 ) of the stones on it to its father.One loses the game if he can't do anything when he moves.
You always move first.You want to know whether you can win the game if you play optimally.
Input
In the first line, there is an integer
T
indicating the number of test cases.
In each test case,the first line contains one integer n refers to the number of nodes.
The next line contains n−1 integers fa[1]⋯fa[n−1] ,which describe the father of nodes 1⋯n−1 (node 0 is the root).It is guaranteed that 0≤fa[i]<i .
The next line contains n integers a[0]⋯a[n−1] ,which describe the initial stones on each nodes.It is guaranteed that 0≤a[i]<134217728 .
1≤T≤100 , 1≤n≤100000 .
It is guaranteed that there is at most 7 test cases such that n>100 .
In each test case,the first line contains one integer n refers to the number of nodes.
The next line contains n−1 integers fa[1]⋯fa[n−1] ,which describe the father of nodes 1⋯n−1 (node 0 is the root).It is guaranteed that 0≤fa[i]<i .
The next line contains n integers a[0]⋯a[n−1] ,which describe the initial stones on each nodes.It is guaranteed that 0≤a[i]<134217728 .
1≤T≤100 , 1≤n≤100000 .
It is guaranteed that there is at most 7 test cases such that n>100 .
Output
For each test case output one line.If you can win the game,print "win".Ohterwise,print "lose".
Sample Input
2 2 0 1000 1 4 0 1 0 2 3 3 3
Sample Output
win lose
Source
有一群羊和一些关系。公羊m只,母羊n只
关系是公羊和母羊的
问有多少种方法选出四只羊a,b,c,d,使得a,b b,c 和 c,d都有关系
我们发现b,c肯定是一公一母
那么我们枚举所有关系,计算出两端点有多少相关的关系
假设为x1,x2
那么对答案的贡献就是(x1-1)*(x2-1)*2
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
long long deg1[100001],deg2[100001];
int s[100001],t[100001];
int main()
{
int T;
scanf("%d",&T);
while(T>0)
{
T--;
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
memset(deg1,0,sizeof(deg1));
memset(deg2,0,sizeof(deg2));
int i;
for(i=1;i<=k;i++)
{
scanf("%d%d",&s[i],&t[i]);
deg1[s[i]]++;
deg2[t[i]]++;
}
long long ans=0;
for(i=1;i<=k;i++)
ans+=(deg1[s[i]]-1LL)*(deg2[t[i]]-1LL);
printf("%lld\n",ans*2LL);
}
return 0;
}