Cattle off 216 C small K doubts

Effect: Given the tree, find the number of triplets (i, j, k), satisfying dis (i, j) = dis (j, k) = dis (k, i).

 

Just began to think complicated, violence statistics of all cases.

#include <iostream>
#include <queue>
#include <cstdio>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
typedef long long ll;
 
const int N = 1e4+10;
int n, dep[N];
struct _ {int to,w;};
vector<_> g[N];
int dp[N][2];
ll ans;
 
void dfs(int x, int fa, int d) {
    dep[x] = d, ++ans;
    ll c00 = 0, c11 = 0;
    for (_ e:g[x]) if (e.to!=fa) {
        int y = e.to;
        dfs(y,x,d+e.w&1);
        ans += 6ll*c00*dp[y][0];
        ans += 6ll*c11*dp[y][1];
        ans += 6*dp[y][dep[x]];
        ans += 12ll*dp[x][dep[x]]*dp[y][dep[x]];
        ans += 6ll*dp[y][dep[x]]*(dp[y][dep[x]]-1)/2;
        ans += 6ll*dp[x][!dep[x]]*dp[y][!dep[x]];
        c00 += (ll)dp[y][0]*dp[x][0];
        c11 += (ll)dp[y][1]*dp[x][1];
        dp[x][0] += dp[y][0];
        dp[x][1] += dp[y][1];
    }
    for (_ e:g[x]) if (e.to!=fa) {
        int y = e.to;
        ans += 6ll*dp[y][0]*(dp[y][0]-1)/2*(dp[x][0]-dp[y][0]);
        ans += 6ll*dp[y][1]*(dp[y][1]-1)/2*(dp[x][1]-dp[y][1]);
    }
    ++dp[x][dep[x]];
}
 
int main() {
    scanf("%d", &n);
    REP(i,2,n) {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        g[u].push_back({v,in});
        g[v].push_back({u,w});
    }
    dfs(1,0,0);
    printf("%lld\n", ans);
}

 

 

In fact you can find all paths meet Qiqi Qi or even even even.

#include <iostream>
#include <queue>
#include <cstdio>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
typedef long long ll;
 
const int N = 1e4+10;
int n, dep[N];
struct _ {int to,w;};
vector<_> g[N];
int dp[N][2], ans[2];
 
void dfs(int x, int fa, int d) {
	++ans[d];
    for (_ e:g[x]) if (e.to!=fa) {
        int y = e.to;
        dfs(y,x,d+e.w&1);
    }
}
 
int main() {
    scanf("%d", &n);
    REP(i,2,n) {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        g[u].push_back({v,w});
        g[v].push_back({u,w});
    }
    dfs (1,0,0); 
	printf ( "% lld \ n", (ll) years [0] * years [0] * years [0] + (ll) years [1] * years [1] * years [1]); 
}

 

Guess you like

Origin www.cnblogs.com/uid001/p/10985385.html