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 -& ...
随机推荐
- root of factory hierarchy
项目编译错误! project---->clean
- poj1284 && caioj 1159 欧拉函数1:原根
这道题不知道这个定理很难做出来. 除非暴力找规律. 我原本找的时候出了问题 暴力找出的从13及以上的答案就有问题了 因为13的12次方会溢出 那么该怎么做? 快速幂派上用场. 把前几个素数的答案找出来 ...
- Adobe Flex迷你教程 —Flex4全屏显示
应用场景 1.播放器 我们经常看视频的时候,需要全屏显示,(在flex中这个视频初始化的时候是嵌入到html的iframe中). 2.监控 如下图所示,大多时候我们的监控用的是flex,而树形菜单和标 ...
- maven跳过单元测试-maven.test.skip和skipTests的区别以及部分常用命令
-DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下. -Dmaven.test.skip=true,不执行测试用例,也不编译测试 ...
- 洛谷——P1455 搭配购买
https://www.luogu.org/problem/show?pid=1455 题目描述 明天就是母亲节了,电脑组的小朋友们在忙碌的课业之余挖空心思想着该送什么礼物来表达自己的心意呢?听说在某 ...
- LeetCode 232: Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Linux 文件系统初步
在Linux系统中,假设我们想要知道一个文件的详细信息,那么最简便的方法自然就是ls命令了.例如以下图所看到的:当在shell输入命令"ls -l old"时,在下方就会 ...
- hdu4089Activation 概率dp
//一条队列对于第一个人的情况 //概率p1:队列保持不变 //p2:第一个人到队尾 //p3:第一个人出队 //p4:系统崩溃 //队列中有N个人,Tomato 在第M位置,求系统崩溃.Tomato ...
- XMPP添加删除好友
在现阶段的通信服务中.各种标准都有,因此会出现无法实现相互连通,而XMPP(Extensible Message and presence Protocol)协议的出现,实现了整个及时通信服务协议的互 ...
- android之软件键盘
不弹出软件键盘 <activity android:name="PresCompleteActivity" android:windowSoftIn ...