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

Proving Equivalences

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8605    Accepted Submission(s): 3063

Problem Description
Consider the following exercise, found in a generic linear algebra textbook.
Let A be an n × n matrix. Prove that the following statements are equivalent:
1. A is invertible.
2. Ax = b has exactly one solution for every n × 1 matrix b.
3. Ax = b is consistent for every n × 1 matrix b.
4. Ax = 0 has only the trivial solution x = 0. 
The typical way to solve such an exercise is to show a series of implications. For instance, one can proceed by showing that (a) implies (b), that (b) implies (c), that (c) implies (d), and finally that (d) implies (a). These four implications show that the four statements are equivalent.
Another way would be to show that (a) is equivalent to (b) (by proving that (a) implies (b) and that (b) implies (a)), that (b) is equivalent to (c), and that (c) is equivalent to (d). However, this way requires proving six implications, which is clearly a lot more work than just proving four implications!
I have been given some similar tasks, and have already started proving some implications. Now I wonder, how many more implications do I have to prove? Can you help me determine this?
 
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:
* One line containing two integers n (1 ≤ n ≤ 20000) and m (0 ≤ m ≤ 50000): the number of statements and the number of implications that have already been proved.
* m lines with two integers s1 and s2 (1 ≤ s1, s2 ≤ n and s1 ≠ s2) each, indicating that it has been proved that statement s1 implies statement s2.
 
Output
Per testcase:

* One line with the minimum number of additional implications that need to be proved in order to prove that all statements are equivalent.

 
Sample Input
2
4 0
3 2
1 2
1 3
 
Sample Output
4
2
题目大意:给一个有向图,问至少加几条边能使图成为强连通图。
题目分析:先用Tarjan缩点,然后统计每个强连通分量的入度与出度,统计入度为0的强连通分量的个数以及出度为0的强连通分量的个数,求两个数的最大值即可
【PS:注意强连通分量为1个时应进行特判,因为他的入度和出度都为0,但是不用加边就已经是强连通图了】
 //Wannafly挑战赛14 C https://www.nowcoder.com/acm/contest/81/C
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int maxn=;
struct edge{
int from;
int to;
int next;
}EDGE[maxn];
vector<int>vc[maxn];
int head[maxn],dfn[maxn],vis[maxn],low[maxn],col[maxn],in[maxn],out[maxn],en[maxn],stk[maxn];//各个变量的意义可参照上篇博客
int edge_cnt=,tot1=,tot2=,scc_cnt=,tot0=;
void add(int x,int y)
{
EDGE[edge_cnt].from=x;
EDGE[edge_cnt].to=y;
EDGE[edge_cnt].next=head[x];
head[x]=edge_cnt++;
}
void Tarjan(int u)
{
low[u]=dfn[u]=++tot1;//注意tot1的初值必须是1【因为dfn必须为正数】,所以这里使用++tot1而不用tot1++;
vis[u]=;
stk[++tot2]=u;
for(int i = head[u]; i != - ; i = EDGE[i].next)
{
if(!dfn[EDGE[i].to]){
Tarjan(EDGE[i].to);
low[u]=min(low[u],low[EDGE[i].to]);
}
else if(vis[EDGE[i].to]){
low[u]=min(low[u],low[EDGE[i].to]);
}
}
if(low[u]==dfn[u]){
int xx;
scc_cnt++;
do{
xx=stk[tot2--];
vc[scc_cnt].push_back(xx);
col[xx]=scc_cnt;
vis[xx]=;
}while(xx!=u);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
edge_cnt=,tot1=,tot2=,scc_cnt=,tot0=;
scc_cnt=;
int n,m;
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));
memset(stk,,sizeof(stk));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(col,,sizeof(col));
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
for(int i = ; i <= n; i++)
{
if(!dfn[i])Tarjan(i);
}
for(int i = ; i < edge_cnt ; i++)
{
if(col[EDGE[i].from]!=col[EDGE[i].to])
{
in[col[EDGE[i].to]]++;//缩点
out[col[EDGE[i].from]]++;
}
}
int sum1=,sum2=;
for(int i = ; i <= scc_cnt ; i++)
{
if(!in[i])
sum1++;
if(!out[i])
sum2++;
}
int mmax=max(sum1,sum2);
if(scc_cnt!=)
cout << mmax << endl;
else
cout << "" <<endl;
for(int i = ; i <= scc_cnt ; i++)
vc[i].clear();
}
return ;
}
/*4 5
1 3
2 4
4 2
1 4
2 1*/

【HDOJ2767】【Tarjan缩点】的更多相关文章

  1. hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)

    #1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...

  2. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  3. King's Quest —— POJ1904(ZOJ2470)Tarjan缩点

    King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...

  4. 【BZOJ-2438】杀人游戏 Tarjan + 缩点 + 概率

    2438: [中山市选2011]杀人游戏 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1638  Solved: 433[Submit][Statu ...

  5. 【BZOJ-1924】所驼门王的宝藏 Tarjan缩点(+拓扑排序) + 拓扑图DP

    1924: [Sdoi2010]所驼门王的宝藏 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 787  Solved: 318[Submit][Stat ...

  6. 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点

    1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1685  Solved: 724[Submit] ...

  7. BZOJ 1051 受欢迎的牛(Tarjan缩点)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4573  Solved: 2428 [Submit][S ...

  8. HDU4612+Tarjan缩点+BFS求树的直径

    tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...

  9. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

随机推荐

  1. react router @4 和 vue路由 详解(五)react怎么通过路由传参

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 7.react怎么通过路由传参? a.通配符传参(刷新页面数据不丢失) //在定义路由的 ...

  2. 学习Linux系统的方法经验

    Linux系统是一个开源的高效的以命令行为主的操作系统,主要用于服务器操作系统领域.对于Linux操作系统更多详细准确的解释大家可以网上找到<Linux就该这么学>的第0章介绍的比较详细: ...

  3. Java中的 JDK下载和环境配置(方式一)

    第一步:需要安装JDK. JDK下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 ...

  4. 读书笔记 C#委托的BeginInvoke、EndInvoke之浅析

    c#中有一种类型叫委托,它是一种引用类型.可以引用静态与非静态的方法,且这些方法的参数列表和返回值类型必须与所声明的委托一致. 委托引用的方法可以通过BeginInvoke和EndInvoke来异步进 ...

  5. 重载的方式写Python的post请求

    #encoding=utf-8#__author__="Lanyangyang" import unittestimport requestsimport json # This ...

  6. delete请求

    Action(){ int HttpRetCode; //定义一个变量,用于接收HTTP返回的状态码 web_add_header("Session-Id", "2e25 ...

  7. Centos7部署open-falcon 0.2

    参考: https://www.cnblogs.com/straycats/p/7199209.html http://book.open-falcon.org/zh_0_2/quick_instal ...

  8. Windows下安装Tensorflow报错 “DLL load failed:找不到指定的模块"

    Windows下安装完tensorflow后,在cmd下运行python后import tensorflow出现如下错误: Traceback (most recent call last):  Fi ...

  9. tf 模型保存

    tf用 tf.train.Saver类来实现神经网络模型的保存和读取.无论保存还是读取,都首先要创建saver对象. 用saver对象的save方法保存模型 保存的是所有变量 save( sess, ...

  10. 10.Python-第三方库requests详解(二)

    Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...