Mahmoud and Ehab and the bipartiteness CodeForces - 862B 并查集
2017-09-21 20:05
405 查看
传送门
题目大意:给定一个整数n(1 <= n <= 10^5) , 然后给n-1行, 表示u和v有一条边链接, 所有的数分成两部分,在同一侧的数字不能链接,问除了题目给定的链接外还有多少链接。
解题思路:主要是求解两侧分别有几个数字,然后结果就是 a × b - n +1; a和b分别为两边的数量。分类用并查集很简单,可以参考经典并查集题目:食物连。分成两类后遍历数一下看看几个即可。
AC代码:
#include <iostream>
#include <cstdio>
using namespace std;
const int max_n = 100050;
int par[max_n+max_n];
int Rank[max_n+max_n];
void init_par()
{
for(int i=0; i<max_n*2; i++)
{
par[i] = i;
Rank[i] = 0;
}
}
int find_par(int n)
{
if(par
== n) return n;
return par
= find_par(par
);
}
void unit_par(int a, int b)
{
a = find_par(a);
b = find_par(b);
if(a == b) return;
if(Rank[a] < Rank[b]) par[a] = b;
else
{
par[b] = a;
if(Rank[a] == Rank[b]) Rank[a]++;
}
}
bool same(int a, int b)
{
return find_par(a) == find_par(b);
}
int main()
{
int n, u, v;
scanf("%d", &n);
if(n == 1)
{
printf("0\n");
return 0;
}
init_par();
for(int i=1; i<n; i++)
{
scanf("%d%d", &u, &v);
unit_par(u, v+max_n);
unit_par(v, u+max_n);
}
int a = 0, b = 0;
for(int i=1; i<=n; i++)
{
if(same(u, i)) a++;
else if(same(u, i+max_n)) b++;
}
printf("%lld\n", (long long) a*b - n+1);
return 0;
}
题目大意:给定一个整数n(1 <= n <= 10^5) , 然后给n-1行, 表示u和v有一条边链接, 所有的数分成两部分,在同一侧的数字不能链接,问除了题目给定的链接外还有多少链接。
解题思路:主要是求解两侧分别有几个数字,然后结果就是 a × b - n +1; a和b分别为两边的数量。分类用并查集很简单,可以参考经典并查集题目:食物连。分成两类后遍历数一下看看几个即可。
AC代码:
#include <iostream>
#include <cstdio>
using namespace std;
const int max_n = 100050;
int par[max_n+max_n];
int Rank[max_n+max_n];
void init_par()
{
for(int i=0; i<max_n*2; i++)
{
par[i] = i;
Rank[i] = 0;
}
}
int find_par(int n)
{
if(par
== n) return n;
return par
= find_par(par
);
}
void unit_par(int a, int b)
{
a = find_par(a);
b = find_par(b);
if(a == b) return;
if(Rank[a] < Rank[b]) par[a] = b;
else
{
par[b] = a;
if(Rank[a] == Rank[b]) Rank[a]++;
}
}
bool same(int a, int b)
{
return find_par(a) == find_par(b);
}
int main()
{
int n, u, v;
scanf("%d", &n);
if(n == 1)
{
printf("0\n");
return 0;
}
init_par();
for(int i=1; i<n; i++)
{
scanf("%d%d", &u, &v);
unit_par(u, v+max_n);
unit_par(v, u+max_n);
}
int a = 0, b = 0;
for(int i=1; i<=n; i++)
{
if(same(u, i)) a++;
else if(same(u, i+max_n)) b++;
}
printf("%lld\n", (long long) a*b - n+1);
return 0;
}
相关文章推荐
- Mahmoud and Ehab and the binary string CodeForces - 862D 二分+交互题
- Mahmoud and Ehab and the xor CodeForces - 862C (异或性质) 4000
- codeforces 862B B. Mahmoud and Ehab and the bipartiteness
- Coderfroces 862 B . Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- Mahmoud and Ehab and the xor CodeForces - 862C 异或技巧
- Codeforces 862 B Mahmoud and Ehab and the bipartiteness(二分图染色)
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(补)
- Codeforces Round #435 (Div. 2)-B. Mahmoud and Ehab and the bipartiteness
- Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- 【Codeforces Round #435 (Div. 2) B】Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary【带权并查集】
- codeforces 862B - Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness