Communication

题目描述

The Ministry of Communication has an extremely wonderful message system, designed by the President himself. For maximum efficiency, each office in the Ministry can send a message directly to some, but not necessarily all, other offices. These connections are not always two-way, because sometimes one office should have the power to send a message to another office, but the other office should not have the power to send one back. This may seem unfair, or even illogical, to uneducated people, but this is the will of the President.

There are so many offices now that the situation has become rather confusing, so the President has decided to reorganize the Ministry to be better adapted to the message system.

The President will divide the Ministry into new departments based on two simple principles:

  1. If A and B are in the same department then A can transmit a message to B, and B can transmit a message to A.
  2. If A can transmit a message to B, and B can transmit a message to A, then A and B are in the same department.

    How many departments will the reorganized Ministry have?

输入

Input starts with a line containing a single integer N, with 0 < N ≤ 100. This tells you how many test cases there will be.

Each following pair lines contains a single test case. The first line of each test case contains an integer n, with 1 < n ≤ 200. This is the number of offices the Ministry has. The second line starts with an integer e with 0 < e <n2/4. This tells you how many individual direct (and directed) connections the Ministry has. Following this are e pairs of integers a, b, with 0 ≤a < b ≤ n − 1. These pairs indicate that there is a connection from a to b. There is at most one direct connection going out from one office and into another.

输出

Each line of output is an integer saying how many departments the Ministry corresponding to that test case will have.

样例输入

3
6
2 0 5 5 0
5
7 0 1 0 2 1 0 1 3 2 4 3 1 4 2
3
4 0 1 0 2 1 0 1 2

样例输出

5
2
2

题意

求有几个能够互相联系的部门(即连通块数量)

题解

并查集 ,但是因为有出现环的情况,所以需要用到floyd判环 ,注意floyd内使用min函数会超时

思路参考

先标记那两个点之间有关系 因为想使用并查集 但并查集的边是无方向的 所以必须满足两点之间可以相互通话才使用并查集连接

分两种情况对待1、2的判断条件:

1、如果f[i][j]=1&&f[j][i]=1说明他们符合条件1并且是双向的边 那就可以把它们放进并查集

2、条件是成环 所以用floyd先判断是否成环:如果成环 则dis[i][j]和dis[j][i]一定是小于初始化的值的 也把他们放进并查集

最后跑for 判断有几个点的pre[i]==i 输出个数即可

这题也可以用Tarjan,但是我还不会这个算法- -先挖个坑

代码

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define scac(x) scanf("%c",&x)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pri3(x,y,z) printf("%d %d %d\n",x,y,z)
#define prl(x) printf("%lld\n",x)
#define prl2(x,y) printf("%lld %lld\n",x,y)
#define prl3(x,y,z) printf("%lld %lld %lld\n",x,y,z)
#define ll long long
#define LL long long
#define pb push_back
#define mp make_pair
#define P pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(1.0)
#define eps 1e-6
#define inf 1e17
#define INF 0x3f3f3f3f
#define N 5005
const int maxn = 2000005;
int t,n,m,u,v,ans;
int e[N][N],dis[N][N];
int fa[N];
P a[N];
void init()
{
rep(i,0,N)
{
fa[i] = i;
rep(j,0,N)
{
e[i][j] = 0;
dis[i][j] = INF;
}
}
}
void add(int i,int u,int v)
{
a[i] = mp(u,v);
e[u][v] = 1;
dis[u][v] = 1;
}
void floyd(int n)
{
rep(k,0,n) rep(i,0,n) rep(j,0,n)
if(dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k]+dis[k][j] ;
}
int find(int x){return x == fa[x] ? x : fa[x] = find(fa[x]);}
void join(int x,int y)
{
if(find(x)!=find(y))
fa[find(y)] = find(x);
}
int main()
{
sca(t);
while(t--)
{
init();
sca2(n,m);
rep(i,0,m)
{
sca2(u,v);
add(i,u,v);
}
floyd(n);
rep(i,0,m)
{
u = a[i].first, v = a[i].second; if(e[v][u] || (dis[u][v] != INF && dis[v][u] != INF))
join(u,v);
}
ans = 0;
rep(i,0,n)
{
if(fa[i] == i)
ans++;
}
pri(ans);
}
}

upc组队赛14 Communication【并查集+floyd /Tarjan】的更多相关文章

  1. [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)

    题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来 ...

  2. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  3. codeforces 400 D Dima and Bacteria【并查集 Floyd】

    题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路 这道题目有两个地方不会处理, 先是n个点,分别属于k个集合,该怎么记录下来这里 ...

  4. 数据结构《14》----并查集 Union-Find

    描述: 并查集是一种描述解决等价关系.能够方便地描述不相交的多个集合. 支持如下操作    1. 建立包含元素 x 的集合  MakeSet(x) 2. 查找给定元素所在的集合 Find(x), 返回 ...

  5. Wannafly挑战赛14 - E 并查集维护线性基区间

    给一个1-base数组{a},有N次操作,每次操作会使一个位置无效.一个区间的权值定义为这个区间里选出一些数的异或和的最大值.求在每次操作前,所有不包含无效位置的区间的权值的最大值. 线性基删除不知道 ...

  6. 并查集实现Tarjan算法

    本文是对http://noalgo.info/476.html的一点理解,特别是对其中 int father[mx]: //节点的父亲 int ancestor[mx]; //已访问节点集合的祖先 这 ...

  7. 最短路径-并查集+Floyd[转载]

    题目描述 N个城市,标号从0到N-1,M条道路,第K条道路(K从0开始)的长度为2^K,求编号为0的城市到其他城市的最短距离 输入描述: 第一行两个正整数N(2<=N<=100)M(M&l ...

  8. upc组队赛14 As rich as Crassus【扩展中国剩余定理】

    As rich as Crassus 题目链接 题目描述 Crassus, the richest man in the world, invested some of his money with ...

  9. upc组队赛14 Floating-Point Hazard【求导】

    Floating-Point Hazard 题目描述 Given the value of low, high you will have to find the value of the follo ...

随机推荐

  1. Codeforces 1114B (贪心)

    题面 传送门 分析 答案很好看出,显然是选最大的m*k个数 那么如何构造方案呢 我们把最大的m*k个数的位置标记为1,其他标记为0 从左到右维护一个ptr,记录有标记的数的个数,如果当前有m个有标记的 ...

  2. 详解Twitter开源分布式自增ID算法snowflake(附演算验证过程)

    详解Twitter开源分布式自增ID算法snowflake,附演算验证过程 2017年01月22日 14:44:40 url: http://blog.csdn.net/li396864285/art ...

  3. JVM(5)之 GC之标记

    开发十年,就只剩下这套架构体系了! >>>     堆分为年轻代和年老代.永久代是非堆内存,它又叫做方法区(一般的说法),主要存储已被加载的类信息.常量.静态变量.而该区域在java ...

  4. 微信小程序(18)-- 自定义头部导航栏

    最近做的项目涉及相应的页面显示相应的顶部标题,所以就需要自定义头部导航了. 首先新建一个顶部导航公用组件topnav,导航高度怎么计算? 1.wx.getSystemInfo 和 wx.getSyst ...

  5. QT + openssl + VS2015静态编译

    从http://slproweb.com/products/Win32OpenSSL.html下载已经编译好的openssl,一路next 我将OpenSSL-Win32\lib\VC目录下的libe ...

  6. Windows 搭建MongoDB分片集群(一)

    一.角色说明 要构建一个MongoDB分片集群,需要三个角色: shard server  即存储实际数据得分片,每个shard 可以是一个Mongod实例,也可以是一组mongod实例构成得Repl ...

  7. linux中设置虚拟域名

    一.打开tomcat安装目录下conf/server.xml这个文件在server.xml文档中找到 </Engine></Service> 接着添加上面添加以下内容(暂时先说 ...

  8. jmeter 参数化5_Count 计数器

    如果需要引用的数据量较大,且要求不能重复或者需要自增,那么可以使用计数器来实现. 计数器(counter):允许用户创建一个在线程组之内都可以被引用的计数器. 计数器允许用户配置一个起点,一个最大值, ...

  9. 新增16条设计规约!阿里巴巴Java开发手册(详尽版)开放下载!

    <阿里巴巴Java开发手册>是阿里内部Java工程师所遵循的开发规范,涵盖编程规约.单元测试规约.异常日志规约.MySQL规约.工程规约.安全规约等,这是近万名阿里Java技术精英的经验总 ...

  10. objc_setAssociatedObject 关联对象

    使用场景:在分类中,不允许创建实例变量,这里就解决了此问题 参考: https://www.cnblogs.com/someonelikeyou/p/7162613.html 属性的实质:就是实例变量 ...