CF862B
题目简化和分析:
这是一道较为经典的二分图染色题。
但这题让我们求得是完全二分图。
\(cnt_{1}\) 表示染成颜色种类为 \(1\) 的个数。
\(cnt_{2}\) 表示染成颜色种类为 \(2\) 的个数。
因为是完全二分图所以它的节点个数为:
\[cnt_{1} \times cnt_{2}
\]
我们已经有题目给出的 \(n\) 个点,所以答案为:
\[cnt_{1} \times cnt_{2} -n+1
\]
注意不开 long long 见祖宗。
Solution:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
const int N=1e5+50;
int n;
struct edge{
int to,next;
}e[N<<1];
int head[N<<1],cnt;
void add_edge(int u,int v){
++cnt;
e[cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
int res[3],col[N<<1];
void dfs(int n,int c){
col[n]=c;
res[c]++;
for(int i=head[n];i;i=e[i].next){
int y=e[i].to;
if(!col[y]){
dfs(y,3-c);
}
}
}
signed main()
{
scanf("%d",&n);
for(int i=1;i<n;++i){
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
dfs(1,1);
printf("%lld\n",res[1]*res[2]-n+1);
}

浙公网安备 33010602011771号