题目连接:
题意:
直接无脑树形dp即可 dp[u][0]表示u的子树内的节点到u一共有几个奇数路径 dp[u][1]表示状态u的子树内到u一共有几个偶数路径
或者直接dfs, 将间隔的点染成黑白色,那么相隔的点 连起来 一定就是偶数路径了
树形dp
// Created by Yishui
// Time on 2018/10/
// E-mail: Yishui_wyb@outlook
/*---------------------------------*/
#include <bits/stdc++.h>
using namespace std;
#define cpp_io() {ios::sync_with_stdio(false); cin.tie(NULL);}
#define rep(i,a,n) for (int i=a;i<n;i++)
#define repp(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define CLR(a,b) memset(a,(b),sizeof(a))
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ls o<<1
#define rs o<<1|1
typedef long long ll;
typedef vector<int> VI;
const int MAXN = (int)2e5+10;
const int INF = 0x3f3f3f3f;
const int mod = (int)1e9+7;
int dp[MAXN][2];
VI E[MAXN];
int a, b;
int cnt[MAXN];
void dfs(int u,int fa) {
cnt[u]=cnt[fa]+1;
if(cnt[u]&1) a++; else b++;
rep(i,0,SZ(E[u])) {
int v=E[u][i];
if(v==fa) continue;
dfs(v,u);
}
}
int main() {
cpp_io();
int n; cin>>n;
rep(i,1,n) {
int u,v; cin>>u>>v;
E[u].pb(v);
E[v].pb(u);
}
dfs(1,0);
cout<<1LL*a*(a-1)/2+1LL*b*(b-1)/2<<"\n";
return 0;
}
DFS
// Created by Yishui
// Time on 2018/10/
// E-mail: Yishui_wyb@outlook
/*---------------------------------*/
#include <bits/stdc++.h>
using namespace std;
#define cpp_io() {ios::sync_with_stdio(false); cin.tie(NULL);}
#define rep(i,a,n) for (int i=a;i<n;i++)
#define repp(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define CLR(a,b) memset(a,(b),sizeof(a))
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ls o<<1
#define rs o<<1|1
typedef long long ll;
typedef vector<int> VI;
const int MAXN = (int)2e5+10;
const int INF = 0x3f3f3f3f;
const int mod = (int)1e9+7;
int dp[MAXN][2];
VI E[MAXN];
int a, b;
int cnt[MAXN];
void dfs(int u,int fa) {
cnt[u]=cnt[fa]+1;
if(cnt[u]&1) a++; else b++;
rep(i,0,SZ(E[u])) {
int v=E[u][i];
if(v==fa) continue;
dfs(v,u);
}
}
int main() {
cpp_io();
int n; cin>>n;
rep(i,1,n) {
int u,v; cin>>u>>v;
E[u].pb(v);
E[v].pb(u);
}
dfs(1,0);
cout<<1LL*a*(a-1)/2+1LL*b*(b-1)/2<<"\n";
return 0;
}