Communication【floyd判环+并查集】
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
题意:
T组数据 每组先输入一个数n表示共n个点 然后是m和m组关系 (a,b)表示a可以给b发消息 如果满足以下条件的一种 则可以将他们划分到一组:
1、1-->2 && 2-->1 (相互之间可以直接通信)
2、1-->2 && 2-->3 && 3-->1 (成环)(题意中没说明 只考虑1WA了所以想到可能有这种情况)
最后输出可以分成几组
思路:
先标记那两个点之间有关系 因为想使用并查集 但并查集的边是无方向的 所以必须满足两点之间可以相互通话才使用并查集连接
分两种情况对待1、2的判断条件:
1、如果f[i][j]=1&&f[j][i]=1说明他们符合条件1并且是双向的边 那就可以把它们放进并查集
2、条件是成环 所以用floyd先判断是否成环:如果成环 则dis[i][j]和dis[j][i]一定是小于初始化的值的 也把他们放进并查集
最后跑for 判断有几个点的pre[i]==i 输出个数即可
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int MAX=2e2;
const int INF=0x3f3f3f3f;
int n,m;
int f[MAX+5][MAX+5];
int pre[MAX+5];
int dis[MAX+5][MAX+5];
void floyd()
{
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(dis[i][j]>dis[i][k]+dis[k][j]){
dis[i][j]=dis[i][k]+dis[k][j];
}
}
}
}
}
int judge(int i, int j)
{
if(dis[i][j]<INF&&dis[j][i]<INF){
return 1;
}
return 0;
}
int fd(int x)
{
int r=x;
while(pre[r]!=r){
r=pre[r];
}
return r;
}
void mix(int x,int y)
{
int fx=fd(x),fy=fd(y);
if(fx!=fy){
pre[fx]=fy;
}
}
void init()
{
for(int i=0;i<MAX;i++){
pre[i]=i;
for(int j=0;j<MAX;j++){
f[i][j]=0;
dis[i][j]=INF;
}
}
}
struct node{
int u;
int v;
}a[MAX+5];
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
for(int i=0;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
a[i].u=u,a[i].v=v;
f[u][v]=1;
dis[u][v]=1;
}
floyd();
for(int i=0;i<m;i++){
int u=a[i].u,v=a[i].v;
if(f[v][u]==1||judge(u,v)){
mix(u,v);
}
}
int ans=0;
for(int i=0;i<n;i++){
if(pre[i]==i){
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
Communication【floyd判环+并查集】的更多相关文章
- HDU1811 拓扑排序判环+并查集
HDU Rank of Tetris 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1811 题意:中文问题就不解释题意了. 这道题其实就是一个拓扑排序判圈 ...
- floyd判环算法(龟兔赛跑算法)
floyd判环算法(龟兔赛跑算法) 注意,这个算法是用来判断一条链+一条环的图,环的长度或者环与链的交界处的,所以此floyd非彼floyd(虽然都是一个人想出来的). (图不是我的) 如果只要求环的 ...
- 蓝桥杯 试题 历届试题 发现环 并查集+dfs
问题描述 小明的实验室有N台电脑,编号1~N.原本这N台电脑之间有N-1条数据链接相连,恰好构成一个树形网络.在树形网络上,任意两台电脑之间有唯一的路径相连. 不过在最近一次维护网络时,管理员误操作使 ...
- hdu1625 Numbering Paths (floyd判环)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
- “美登杯”上海市高校大学生程序设计 C. 小花梨判连通 (并查集+map)
Problem C C . 小 花梨 判连通 时间限制:2000ms 空间限制:512MB Description 小花梨给出
- HDU 3342 Legal or Not(有向图判环 拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- upc组队赛14 Communication【并查集+floyd /Tarjan】
Communication 题目描述 The Ministry of Communication has an extremely wonderful message system, designed ...
- 做运动(Dijkstra+并查集+MST)
上面的题解是这样,这道题我真的脑残,其实打代码的时候就意识到了许多,可以用Dfs+Dij+二分,这样还可以卡一卡 但是我打了spfa+spfa+二分,这个显然很慢,类似的题目我好像还做过一道的,就是在 ...
- BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)
BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...
随机推荐
- 实验十四 Swing图形界面组件
实验十四 Swing图形界面组件 实验时间 20178-11-29 1.实验目的与要求 (1) 掌握GUI布局管理器用法: (2) 掌握各类Java Swing组件用途及常用API: 2.实验内容和 ...
- zookeeper实现分布式锁总结,看这一篇足矣(设计模式应用实战)
分布式锁纵观网络各种各样的帖子层出不穷,笔者查阅很多资料发现一个问题,有些文章只写原理并没有具体实现,有些文章虽然写了实现但是并不全面 借这个周末给大家做一个总结,代码拿来就可以用并且每一种实现都经过 ...
- Java——多线程基础知识
多线程进程和线程的区别:每一个进程拥有自己的一整套变量,而线程则共享数据.java.lang.Thread static void sleep(long millis) 线程休眠给定的毫秒数,用 ...
- json字符串转List集合
public List<Map<String,PendingInfo>> jsonToList(String json){//PendingInfo可以换成其它bean类 Li ...
- protocbuf的简单理解
之前通信协议替换为protocbuf!新老交替,很多不同看法,也提出来一些负面因数: 1.老的内部通信协议体已经有一段时间了,稳定熟悉! 2.通过通信结构体进行交互,实际上并没有序列化和反序列化的过程 ...
- SD.Team颜色代码大全
EEEEEE FFCCFF FF66FF FF00FF DDDDDD FFCCCC FF66CC FF00CC CCCCCC FFCC99 FF6699 FF0099 BBBBBB FFCC66 FF ...
- Typora 使用 Markdown 嵌入 LaTeX 数学公式符号语法
博客园不支持渲染 LaTeX 数学公式,需要用到什么公式,请复制到您所用的支持 LaTeX 的编辑器中查看实现效果.Typora 可以渲染 LaTeX 数学公式. 目录 行内与独行 行内公式 独行公式 ...
- Rocket - diplomacy - DUEB参数模型分析
https://mp.weixin.qq.com/s/533bJxcPRgO4W2gf_OEhEw 分析DUEB参数模型中各种参数类型的可能性. 1. 节点类型 根据参数的传播方向,可 ...
- Spring Boot笔记(一) springboot 集成 swagger-ui
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.添加依赖 <!--SpringBoot整合Swagger-ui--> <depen ...
- Java实现 LeetCode 785 判断二分图(分析题)
785. 判断二分图 给定一个无向图graph,当这个图为二分图时返回true. 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我 ...