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

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
 
Source
 
Recommend
lcy
 
理解一下题意,经过奇奇怪怪的转化以后,得出核心题意:给一个有向图,问最少加几条边可使其成为强连通图。
tarjan缩点以后,统计入度为0和出度为0的点个数,取最大值就是答案。
 
这题总感觉以前做过好多次?
 
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int mxn=;
int top,stack[mxn];
bool inst[mxn];
int cnt,dnow;
int dfn[mxn],low[mxn];
int belone[mxn],in[mxn],out[mxn];
vector<int> e[mxn];
void clear(){
cnt=;dnow=;top=;
memset(dfn,-,sizeof(dfn));
memset(inst,false,sizeof(inst));
memset(in,,sizeof in);
memset(out,,sizeof out);
for(int i=;i<mxn;i++) e[i].clear();
}
int n,m;
void tarjan(int s){
int v=,i;
dfn[s]=++dnow;
low[s]=dfn[s];
inst[s]=true;
stack[++top]=s;
int si=e[s].size();
for(i=;i<si;i++){
v=e[s][i];
if(dfn[v]==-){
tarjan(v);
low[s]=min(low[v],low[s]);
}
else if(inst[v]){
low[s]=min(dfn[v],low[s]);
}
}
if(dfn[s]==low[s]){
cnt++;
do{
v=stack[top--];
belone[v]=cnt;
inst[v]=false;
}while(s!=v);
}
return;
}
void calc(){
if(cnt==){
printf("0\n");return;
}
int i,j;
for(i=;i<=n;i++){
for(j=;j<e[i].size();j++){
int v=e[i][j];
if(belone[i]!=belone[v]){
in[belone[v]]++;
out[belone[i]]++;
}
}
}
int idg=,odg=;
for(i=;i<=cnt;i++){
if(!in[i])idg++;
if(!out[i])odg++;
}
printf("%d\n",max(idg,odg));
return;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
if(!m){
if(n==)printf("0\n");
else printf("%d\n",n);
continue;
}
clear();
int i,j;
int u,v;
for(i=;i<=m;i++){
scanf("%d%d",&u,&v);
e[u].push_back(v);
}
for(i=;i<=n;i++){
if(dfn[i]==-)tarjan(i);
}
calc();
}
return ;
}

HDU2767 Proving Equivalences的更多相关文章

  1. HDU2767 Proving Equivalences(加边变为强联通图)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. hdu2767 Proving Equivalences Tarjan缩点

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  3. hdu2767 Proving Equivalences --- 强连通

    给一个图,问至少加入�多少条有向边能够使图变成强连通的. 原图是有环的,缩点建图,在该DAG图上我们能够发现,要使该图变成强连通图必须连成环 而加入�最少的边连成环,就是把图上入度为0和出度为0的点连 ...

  4. hdu2767 Proving Equivalences,有向图强联通,Kosaraju算法

    点击打开链接 有向图强联通,Kosaraju算法 缩点后分别入度和出度为0的点的个数 answer = max(a, b); scc_cnt = 1; answer = 0 #include<c ...

  5. hdu 2767 Proving Equivalences

    Proving Equivalences 题意:输入一个有向图(强连通图就是定义在有向图上的),有n(1 ≤ n ≤ 20000)个节点和m(0 ≤ m ≤ 50000)条有向边:问添加几条边可使图变 ...

  6. hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. Proving Equivalences(加多少边使其强联通)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. UVALive - 4287 - Proving Equivalences(强连通分量)

    Problem   UVALive - 4287 - Proving Equivalences Time Limit: 3000 mSec Problem Description Input Outp ...

  9. HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

随机推荐

  1. G++ 编译多个源文件

    g++ -c *.cpp g++ graph.o maxflow.o test.o -o test  // 链接顺序必须写对

  2. webpack的配置处理

    1.webpack对脚本的处理 1.Js用什么loader加载? 1>webpack 本身就支持js的加载, 2>通过babel-loader ES2015 加载js,再用 babel-p ...

  3. thinkphp3.2.3如何只改变地址url中的某一个分隔符,其它保持不变

    今天教大家一个关于使用thinkphp3.2.3改变只改变地址url中的某一个分隔符的方法,首先大家来看看这个地址! 它的原始地址应该是/Home/Index/index/page/2.html,那我 ...

  4. Python知识点进阶——生成器

    生成器 为什么要将列表转化为迭代器? 因为列表太大的话用内存太大,做成迭代器可以节省空间,用的时候再拿出部分. 生成器是不会把结果保存在一个系列中,而是保存生成器的状态,在每次进行迭代时返回一个值,知 ...

  5. WPF实现QQ群文件列表动画(二)

    上篇(WPF实现QQ群文件列表动画(一))介绍了WPF实现QQ群文件列表动画的大致思路,结合我之前讲过的WPF里ItemsControl的分组实现,实现起来问题不大,以下是效果图: 其实就是个List ...

  6. RDLC Reporting in Visual Studio 2017

    原文:RDLC Reporting in Visual Studio 2017 Visual Studio 2017 中可以使用 RDLC Reporting 插件来设计报表,SAP Crystal ...

  7. 分治 - 计算几何 - BZOJ2458,[BeiJing2011]最小三角形

    http://www.lydsy.com/JudgeOnline/problem.php?id=2458 [BeiJing2011]最小三角形 描述 Frisk现在遇到了一个有趣的问题. 平面上有N个 ...

  8. laravel5.2总结--服务容器(依赖注入,控制反转)

    1.依赖 我们定义两个类:class Supperman 和 class Power,现在我们要使用Supperman ,而Supperman 依赖了Power class Supperman { p ...

  9. Python框架之Django学习笔记(十七)

    Django框架之表单(续二) 今天的这篇博客将是Django学习笔记博客的最后一篇,基本每周最少一篇的Django框架学习,坚持到今天也实属不易,当然了,这个框架的学习仅仅是Django框架的基础部 ...

  10. Web 安全问题 rel="noopener nofollw"

    1. noopener 如果你需要用 a 标签打开一个标签页时,你会使用 target='_blank' 这个属性,此时你需要添加 rel='noreferrer noopener' 当你使用 tar ...