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 并查集的更多相关文章

  1. 【BZOJ2959】长跑(Link-Cut Tree,并查集)

    [BZOJ2959]长跑(Link-Cut Tree,并查集) 题面 BZOJ 题解 如果保证不出现环的话 妥妥的\(LCT\)傻逼题 现在可能会出现环 环有什么影响? 那就可以沿着环把所有点全部走一 ...

  2. 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 ...

  3. Is It A Tree?(并查集)

    Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...

  4. POJ1308:Is It A Tree?(并查集)

    Is It A Tree? 题目链接:http://poj.org/problem?id=1308 Description: A tree is a well-known data structure ...

  5. Codeforces 915F Imbalance Value of a Tree(并查集)

    题目链接  Imbalance Value of a Tree 题意  给定一棵树.求树上所有简单路径中的最大权值与最小权值的差值的和. 首先考虑求所有简单路径中的最大权值和. 对所有点按照权值大小升 ...

  6. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

  7. CodeForces - 828C String Reconstruction 并查集(next跳)

    String Reconstruction Ivan had string s consisting of small English letters. However, his friend Jul ...

  8. hdu1325 Is It A Tree? 基础并查集

    #include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...

  9. POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28838   Accepted: 9843 -& ...

随机推荐

  1. HNU 13101 The Triangle Division of the Convex Polygon 组合数的因式分解求法

    题意: 求第n-2个Catalan数 模上 m. 思路: Catalan数公式: Catalan[n] = C(n, 2n)/(n+1) = (2n)!/[(n+1)!n!] 因为m是在输入中给的,所 ...

  2. yum下载的rpm包离线安装

    #修改yum设置,让rpm包缓存到本地 vi /etc/yum.conf #修改keepcache为1 keepcache=1 #清空yum缓存 yum clean all #安装你要离线安装的rpm ...

  3. 和同事合作开发,使用局域网 git创建本地仓库

    转自原文 和同事合作开发,使用局域网 git创建本地仓库 1.仓库 建一个空文件夹来做仓库,例如建为 cangku 1.1 cd 到 cangku目录下 创建远程仓库容器 mkdir  mycangk ...

  4. android将String转化为MD5的方法+一些String经常使用的方法

    public class StringUtils { public static String MD5Encode(String origin) { String resultString = nul ...

  5. storm trident function函数

    package cn.crxy.trident; import java.util.List; import backtype.storm.Config; import backtype.storm. ...

  6. Html学习(三) 分类学习

    代码: <h1>这是一级分类吗</h1> <h2>这是二级分类吗</h2> <h3>这是三级分类吗 </h3> 效果: 介绍: ...

  7. Find or Query Data with C# Driver

    https://docs.mongodb.com/getting-started/csharp/query/ Overview You can use the Find and FindAsync m ...

  8. 面向对象(OOP)五大基本原则

    书单 <Object-Oriented Analysis & Design with Application>:Grady Booch, 下载地址:object-oriented- ...

  9. Metasploit渗透测试实验报告

    Metasploit渗透测试实验报告

  10. dom4j组装xml 以及解析xml

    dom4j组装xml 以及解析xml: 1.下载dom4j的jar包,地址:https://dom4j.github.io/ 2.java代码: package test; import java.i ...