BZOJ 4243 交朋友
来源:互联网 发布:2017网络流行词汇大全 编辑:程序博客网 时间:2024/04/28 11:57
并查集
#include<cstdio>#include<queue>#define N 200005using namespace std;namespace runzhe2000{ typedef long long ll; bool vis[N]; int last[N], n, m, ecnt, f[N], siz[N]; struct edge{int next, to;}e[N]; void addedge(int a, int b){e[++ecnt]=(edge){last[a], b}; last[a] = ecnt;} int find(int x){return f[x] == x ? x : f[x] = find(f[x]);} void main() { scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++) f[i] = i, siz[i] = 1; for(int i = 1; i <= m; i++) { int a, b; scanf("%d%d",&a,&b); addedge(a, b); } for(int i = 1; i <= n; i++) { int head = 0; for(int j = last[i]; j; j = e[j].next) { if(!head) head = find(e[j].to); else { int f1 = find(e[j].to); if(f1 != head) f[f1] = head, siz[head] += siz[f1]; } } } queue<int> q; for(int i = 1; i <= n; i++) if(siz[find(i)] > 1) q.push(i), vis[i] = 1; for(; !q.empty(); ) { int x = q.front(); q.pop(); for(int i = last[x]; i; i = e[i].next) { int y = e[i].to; if(find(y) != find(x)) { int f1 = find(x), f2 = find(y); f[f2] = f1; siz[f1] += siz[f2]; if(!vis[y]) q.push(y), vis[y] = 1; } } } ll ans = 0; for(int i = 1; i <= n; i++) { for(int j = last[i]; j; j = e[j].next) if(find(i) != find(e[j].to)) { ans++; } if(find(i) == i) ans += (ll)siz[find(i)] * (siz[find(i)] - 1); } printf("%lld\n",ans); }}int main(){ runzhe2000::main();}
0 0
- bzoj 4243: 交朋友 bfs
- bzoj 4243: 交朋友
- BZOJ 4243 交朋友
- bzoj 4243: 交朋友
- [并查集] BZOJ 4243 交朋友
- 交朋友
- 交朋友
- 交朋友
- 交朋友
- 交朋友
- 真心交朋友
- 2009交朋友
- 交朋友了
- 交朋友了!
- bzoj4243: 交朋友
- 欢迎与我交朋友
- 交朋友之道
- To rub elbows 交朋友
- ZJOI 2010 贪吃的老鼠 网络流
- HDU1095 A+B for Input-Output Practice (VII)
- [R语言入门]R 语言数据操作总结
- HDU1096 A+B for Input-Output Practice (VIII)
- react native ios打包到真机
- BZOJ 4243 交朋友
- 浅谈 JSP 技术
- 机器学习方向数学基础教材推荐
- stl_vector去重方法
- 算法训练 矩阵乘法
- php处理敏感词时遇到的相关编码问题
- Spring核心之IoC
- Round E APAC Test 2017 Problem B. Beautiful Numbers
- Hibernate 一对多双向关联