Aizu - 2564 Tree Reconstruction 并查集
Aizu - 2564 Tree Reconstruction
题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边
思路:这题好像有一个定理之类的,对于每一个连通块,所需要的边数是 M-N(边数-点数) ,这个原理我还不是很清楚。
知道了这个之后,并查集求一下就完事了。
这题我大致看了一下官方 的提交记录,发现有好多种方法可以搞。23:48:40
#pragma comment(linker, "/STACK:1000000000")
#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
#define MAXN 505
vector<int> G[MAXN];
queue<int> Q;
int father[MAXN];
int find(int x){
if(father[x] == x) return x;
father[x] = find(father[x]);
return father[x];
}
int main(void)
{
//IN;
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++){
father[i] = i;
}
int x, y;
for(int i = ; i <= m; i++){
scanf("%d%d", &x, &y);
x = find(x);
y = find(y);
if(x != y){
father[x] = y;
}
}
int ans = m - n;
for(int i = ; i <= n; i++){
if(father[i] == i){
ans++;
}
}
printf("%d\n", ans);
}
Aizu - 2564 Tree Reconstruction 并查集的更多相关文章
- 【BZOJ2959】长跑(Link-Cut Tree,并查集)
[BZOJ2959]长跑(Link-Cut Tree,并查集) 题面 BZOJ 题解 如果保证不出现环的话 妥妥的\(LCT\)傻逼题 现在可能会出现环 环有什么影响? 那就可以沿着环把所有点全部走一 ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集
C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...
- Is It A Tree?(并查集)
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- POJ1308:Is It A Tree?(并查集)
Is It A Tree? 题目链接:http://poj.org/problem?id=1308 Description: A tree is a well-known data structure ...
- Codeforces 915F Imbalance Value of a Tree(并查集)
题目链接 Imbalance Value of a Tree 题意 给定一棵树.求树上所有简单路径中的最大权值与最小权值的差值的和. 首先考虑求所有简单路径中的最大权值和. 对所有点按照权值大小升 ...
- Codeforces - 828C String Reconstruction —— 并查集find()函数
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...
- CodeForces - 828C String Reconstruction 并查集(next跳)
String Reconstruction Ivan had string s consisting of small English letters. However, his friend Jul ...
- hdu1325 Is It A Tree? 基础并查集
#include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...
- POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28838 Accepted: 9843 -& ...
随机推荐
- [luogu] P1772 [ZJOI2006]物流运输(动态规划,最短路)
P1772 [ZJOI2006]物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线 ...
- Python+Appium来写app自动化脚本
1...........................我有空再补
- 【codeforces 239B】Easy Tape Programming
[题目链接]:http://codeforces.com/contest/239/problem/B [题意] 给你一个长度为n的字符串,只包括'<">'以及数字0到9; 给你q ...
- Mysql学习总结(27)——Mysql数据库字符串函数
注:sql的移植性比较强,函数的移植性不强,一般为数据库软件特有,例如mysql有mysql的函数,oracle有oracle的函数. 1.concat连接字符串: 从上图中可以看出,直接使用sele ...
- 几周内搞定Java的10个方法
不要将Java与JavaScript弄混了,Java的目标是“一次编译,到处调试”(呃,不对,是“到处运行”).简单来说,就是Java程序可以直接在任何设备上运行. Java语言是什么? 不管我们是否 ...
- TextView超链接
这里面涉及两个知识点--超链接和跳转.以下进行逐一解说: 1.实现超链接: 1.1形成超链接文本 public static SpannableString getUserlink(String us ...
- 更改linux文件的拥有者及用户组(chown和chgrp)
.使用chown命令更改文件拥有者 在 shell 中,能够使用chown命令来改变文件全部者.chown命令是change owner(改变拥有者)的缩写.须要要注意的是,用户必须是已经存在系统中的 ...
- Vue源代码笔记(一)数据绑定
VUE数据绑定介绍 数据绑定是vue的基础核心之一,本文以Vue对象(当然也包含VueComponent)里的data数据绑定为例,阐述整个绑定的过程. Vue的数据绑定由三部分组成, Observe ...
- less07 important
less .foo (@bg: #f5f5f5, @color: #900) { background: @bg; color: @color; font-size: 16px; font-weigh ...
- C#-汉字转拼音缩写
/// 〈summary〉 /// 汉字转拼音缩写 /// Code By MuseStudio@hotmail.com /// 2004-11-30 /// 〈/summary〉 /// 〈para ...