1 solutions
-
0
#include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); #define getchar() p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++ char buf[1000000], *p1 = buf, *p2 = buf; template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -f; for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T, typename... Args> void read(T &x, Args &...y) { read(x); read(y...); } int n,q; vector<int>v[200005]; int a[200005]; int f[200005][8],g[200005][4]; int F[200005][8],G[200005][4]; int sum[200005]; void dfs(int x){ for(auto i:v[x])dfs(i); REP(i,0,8)f[x][i]=g[x][i%4]=F[x][i]=G[x][i%4]=0; for(auto i:v[x]){ REP(j,0,4)g[x][(j+a[x])%4]+=g[i][j]; REP(j,0,8){ if(a[x]%2==0)g[x][(j+a[x])%4]+=f[i][j]; else f[x][((j&1? 5*a[x]:a[x])+j)%8]+=f[i][j]; } } sum[x]=f[x][3]+g[x][3]; if(a[x]&1)++f[x][a[x]]; else ++g[x][a[x]%4]; } void dfs2(int x){ sum[x]+=F[x][3]+G[x][3]; REP(i,0,8)F[x][i]+=f[x][i]; REP(i,0,4)G[x][i]+=g[x][i]; int V=a[x]; for(auto i:v[x]){ int V2=a[i]; vector<int>a(8,0),b(4,0); REP(j,0,8)a[j]=F[x][j]; REP(j,0,4)b[j]=G[x][j]; REP(j,0,4)b[(j+V)%4]-=g[i][j]; REP(j,0,8){ if(V%2==0)b[(j+V)%4]-=f[i][j]; else a[((j&1? 5*V:V)+j)%8]-=f[i][j]; } REP(j,0,4)G[i][(j+V2)%4]+=b[j]; REP(j,0,8)if(V2%2==0)G[i][(j+V2)%4]+=a[j]; else F[i][((j&1? 5*V2:V2)+j)%8]+=a[j]; dfs2(i); } } void Main() { read(n); REP(i,0,n)v[i].clear(); REP(i,0,n){ int x; read(x,a[i]); --x; if(i)v[x].pb(i); } REP(i,0,n)sum[i]=0; dfs(0); dfs2(0); int ans=0; REP(i,0,n)ans+=sum[i]*(n-sum[i]-1); ans/=2; cout<<n*(n-1)*(n-2)/6-ans<<endl; } void TC() { int tc=1; read(tc); while(tc--){ Main(); cout.flush(); } } signed main() {freopen("game.in","r",stdin);freopen("game.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 3070
- Time
- 4000ms
- Memory
- 512MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 1
- Accepted
- 1
- Uploaded By