hdu 3836 Equivalent Sets

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=3836  

Equivalent Sets

Description

To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent.
You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets.
Now you want to know the minimum steps needed to get the problem proved.

Input

The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000.
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.

Output

For each case, output a single integer: the minimum steps needed.

Sample Input

4 0
3 2
1 2
1 3

Sample Output

4
3

题目大意:给你一张有向图要求最少加多少条边时该图变成强连通图。
Tarjan缩点。。

#include<bits/stdc++.h>
using namespace std;
const int N = 20100;
struct Tarjan_scc {stack<int> s;bool instack[N];struct edge { int to, next; }G[N * 3];int idx, scc, tot, in[N], out[N], dfn[N], low[N], head[N], sccnum[N];inline void init(int n) {idx = scc = tot = 0;while (!s.empty()) s.pop();for (int i = 0; i < n + 2; i++) {head[i] = -1;instack[i] = false;in[i] = out[i] = dfn[i] = low[i] = sccnum[i] = 0;}}inline void add_edge(int u, int v) {G[tot].to = v, G[tot].next = head[u], head[u] = tot++;}inline void built(int m) {int u, v;while (m--) {scanf("%d %d", &u, &v);add_edge(u, v);}}inline void tarjan(int u) {dfn[u] = low[u] = ++idx;instack[u] = true;s.push(u);for (int i = head[u]; ~i; i = G[i].next) {int &v = G[i].to;if (!dfn[v]) {tarjan(v);low[u] = min(low[u], low[v]);} else if (instack[v] && dfn[v] < low[u]) {low[u] = dfn[v];}}if (dfn[u] == low[u]) {int v = 0;scc++;do {v = s.top(); s.pop();instack[v] = false;sccnum[v] = scc;} while (u != v);}}inline void solve(int n, int m) {init(n);built(m);for (int i = 1; i <= n; i++) {if (!dfn[i]) tarjan(i);}int x1 = 0, x2 = 0;for (int u = 1; u <= n; u++) {for (int i = head[u]; ~i; i = G[i].next) {int v = G[i].to;if (sccnum[u] != sccnum[v]) {in[sccnum[v]]++;out[sccnum[u]]++;}}}for (int i = 1; i <= scc; i++) {if (!in[i]) x1++;if (!out[i]) x2++;}printf("%d\n", 1 == scc ? 0 : max(x1, x2));}
}go;
int main() {
#ifdef LOCALfreopen("in.txt", "r", stdin);freopen("out.txt", "w+", stdout);
#endifint n, m;while (~scanf("%d %d", &n, &m)) {go.solve(n, m);}return 0;
}

 

转载于:https://www.cnblogs.com/GadyPu/p/5003447.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/574150.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql-5.7.24 linux下载_Linux下安装mysql-5.7.24

本文档讲解安装版本为mysql-5.7.24&#xff0c;对于5.7.24之后的版本&#xff0c;不适用此说明文档&#xff0c;主要原因在于之后版本的mysql配置文件的目录位置和结构有所改变&#xff0c;使用此说明可能会出现找不到配置文件或者配置后不生效的情况。安装过程中务必保证文件路…

Spring MVC拦截器+注解方式实现防止表单重复提交

原理&#xff1a;在新建页面中Session保存token随机码&#xff0c;当保存时验证&#xff0c;通过后删除&#xff0c;当再次点击保存时由于服务器端的Session中已经不存在了&#xff0c;所有无法验证通过。 注&#xff0c;如果是集群的方式&#xff0c;则需要将token放入到缓存中…

JavaScript学习笔记(一)—— 数据类型

一、基本数据类型&#xff1a;1、js中基本数据类型:String、Boolean、Number、Null、undefined&#xff1b;复合数据类型&#xff1a;object对象类型、Array数组类型特殊对象&#xff1a;function函数类型基本数据类型① String —字符串。若使用var声明变量&#xff0c;var x…

mysql分组失效_请教MySql中使用表子查询时,试着先排序后分组,出现排序失效的原因?...

1,今天试着码了一下教程里的题目,是找出每一个班级的身高最高的学生,用的是先order by降序排序所有学生升高,再用group by分组每一个班级取第一个值,却发现当使用子查询时,得到的仍旧是未排序时的结果,即没有取到最高身高,想问下为何使用子查询时,order by会失效,谢谢!↑这是建…

MySQL后初次密码_mysql第一次安装成功后初始化密码操作步骤

把文件解压到一个目录下这是解压后的目录将my.ini文件考进去双击打开my.ini找到这两行更改成自己的解压路径保存右键此电脑属性找到高级系统设置配置环境变量环境变量 新建 变量值是解压文件的路径Path 单击path编辑新建之后 用管理员身份打开cmd进入文件路径打开命令行窗口&am…

JQuery 操作 radio 被坑一例

.removeAttr(checked); .prop(checked,false); .prop(checked,true); 与 .attr("checked",true); .attr("checked",true); 有本质区别&#xff0c;上边的方法效果理想。转载于:https://www.cnblogs.com/rgqancy/p/5017357.html

mysql技术简介_mysql的简介和使用

mysql简介数据的所有存储&#xff0c;检索&#xff0c;管理和处理实际上是由数据库软件--DBMS(数据库管理系统)完成的mysql是一种DBMS&#xff0c;即它是一种数据库软件mysql工具mysql是一个客户机-服务器的DBMS。因此为了使用mysql&#xff0c;需要一个客户机&#xff0c;即提…

unity3d 随机物体生成器 工具

根据输入的物体生成随机成组的物体. 1 /// <summary>2 /// 本脚本可以由输入的预制 生成以本物体为中心的随机预制,支持预制打组3 /// 随机物体生成器(尤其试用于场景中静态物体随机摆放)4 /// </summary>5 using UnityEngine;6 using System.Collections;7 using …

mysql 大分页查询优化_Mysql骚操作:优化大分页查询

背景系统结构如上图。经过排查是因为系统B拉取数据时间太长导致的推送超时。系统B拉取数据的方法是根据_tiemstamp(数据操作时间)分页查询系统A的接口,即&#xff1a;1SELECT 字段名2FROM 表名3WHERE _timestamp > beginTime AND _timestamp < endTime 4LIMIT n, m;由于该…

python --动态类型

转自&#xff1a;http://www.cnblogs.com/vamei/archive/2012/07/10/2582795.html 动态类型(dynamic typing)是Python另一个重要的核心概念。我们之前说过&#xff0c;Python的变量(variable)不需要声明&#xff0c;而在赋值时&#xff0c;变量可以重新赋值为任意值。这些都与动…

mysql 判断等于空字符串_mysql 判断null 和 空字符串

1.在mysql中null 不能使用任何运算符与其他字段或者变量(函数、存储过程)进行运算。若使用运算数据就可能会有问题。2.对null 的判断&#xff1a;创建一个user表&#xff1a;id 主健 name 可以为空select * from user;insert into user values(33,null); ##创建一条name为空的…

JS组件系列——Bootstrap Table 表格行拖拽

JS组件系列——Bootstrap Table 表格行拖拽 原文:JS组件系列——Bootstrap Table 表格行拖拽前言&#xff1a;之前一直在研究DDD相关知识&#xff0c;好久没更新JS系列文章了。这两天做了一个简单的业务需求&#xff0c;觉得效果还可以&#xff0c;今天在这里分享给大家&#x…

mysql 值到99999后不增值了_MySQL必知必会3

创建和操纵表创建表输入CREATE TABLE customers(cust_id int NOT NULL AUTO_INCREMENT,cust_name char(50) NOT NULL ,cust_address char(50) NULL ,cust_city char(50) NULL ,cust_state char(5) NULL ,cust_zip char(10) NULL ,cust_country char(50) NULL ,cust_contact cha…

Linux install innotop

innotop是mysql监控工具 1&#xff0c;下载 weget http://nchc.dl.sourceforge.net/project/innotop/innotop/1.6.0/innotop-1.6.0.zip 2&#xff0c;安装依赖包 yum install perl-DBI perl-DBD-MySQL perl-TermReadKey perl -y 3,安装innotop perl Makefile.PL make make ins…

mysql udp服务器_netty学习:UDP服务器与Spring整合(2)

上一篇文章中&#xff0c;介绍了netty实现UDP服务器的栗子。本文将会对UDP服务器与spring boot整合起来&#xff0c;并使用RedisTemplate的操作类访问Redis和使用Spring DATA JPA链接MySQL数据库&#xff0c;其中会使用多线程、异步等知识。只公布了一个框架&#xff0c;需要的…

1-4 TCP/IP协议族

网络协议是在内核中实现的&#xff0c;socket是对tcp/ip协议的系统调用&#xff0c;提供以下两点功能&#xff1a; 1. 将应用撑血数据从用户缓冲区中复制到TCP/UDP内核发送缓冲区&#xff0c;以交付内核发送来的数据&#xff08;比如send&#xff09;&#xff0c;或者从内核TCP…

python将数据写入excel_【Python】将数据库中的数据查询出来自动写入excel文档

近期每天都要监控一个数据。第一个版本是这样的&#xff1a;每天新增一个文档来汇总这个数据。这样搞了几天之后&#xff0c;过了一个周末&#xff0c;过来突然发现数据变多了很多&#xff0c;这个时候要调整策略&#xff0c;直接一个文档汇总出要的数据就可以了。这样查了两天…

OC 继承

一、基本概念 程序的世界和人类的“对象”世界在思想上是没有设么区别的&#xff0c;富二代继承了父母&#xff0c;自然就拥有了父母拥有的所有资源&#xff0c;子类继承了父类同样就拥有了父类所有的方法和属性&#xff08;成员变量&#xff09;。 在这里动物是猫类和狗类的父…

吃西瓜 最大子矩阵 三维的。 rqnoj93

真的好苦逼&#xff0c;这道神题&#xff0c;交了几十次都是70分&#xff0c;剩下的都TLE了&#xff0c;崩溃。 没办法&#xff0c;只能这样了。在网上看了别人的算法&#xff0c;大概思想是把三维的先压成两维&#xff0c;再把二维的弄成一维&#xff0c;这样就很容易了。 首先…

mysql 两张表差集_mysql中两张表使用left join on 求差集详解

1.表结构mysql> select * from allStudents;-----------| id | name |-----------| 1 | ???? || 2 | ???? || 3 | ???| 4 | four |-----------4 rows in set (0.00 sec)mysql> select * from currentStudents;------------| id | name |------------| 1 | luo…