Proving Equivalences

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
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

/*刚开始是强连通小白,现在稍微明白一点了,入度与出度有点难理解;
现在看来,应该是这样的,入度表示当前的scc上边有几个scc,出度是
当前scc下边有几个scc,如果上边或者下边没有点,这就说明,缩点后
这是一个叶子节点或者根节点。对于这道题来说,我们应该分别统计一下
根节点和叶子节点的个数,取其最大值*/
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 50010
int in[MAX],out[MAX],sumin,sumout;
vector<int>G[MAX];
vector<int>scc[MAX];
struct node
{
int u,v;
int next;
}edge[MAX];
int head[MAX],cnt,scc_cnt,dfs_clock;
int sccno[MAX],low[MAX],dfn[MAX];
bool Instack[MAX];
int m,n;
stack<int>s;
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void getmap()
{
int a,b;
while(m--)
{
scanf("%d%d",&a,&b);
add(a,b);
}
}
void suodian()
{
for(int i=1;i<=scc_cnt;i++)
G[i].clear(),in[i]=0,out[i]=0;//缩点时清空G[i],存放每一个scc
for(int i=0;i<cnt;i++)
{
int u=sccno[edge[i].u];
int v=sccno[edge[i].v];
if(u!=v)
//原图中的u--v现在变成了新图中的u--v,这条路变成了两个scc的链接
G[u].push_back(v),out[u]++,in[v]++;
}
}
void tarjan(int u,int fa)
{
int v;
low[u]=dfn[u]=++dfs_clock;//更新时间戳
s.push(u);//标记u进栈
Instack[u]=true;
for(int i=head[u];i!=-1;i=edge[i].next)
{//遍历u下边的每一个点,同时判断是否遍历过,是否在栈里
v=edge[i].v;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(Instack[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
//新的scc出现的标志,不好理解的话,可以想象一下一个普通的叶子节点
{
scc_cnt++;//由此可以看出,scc的编号从1开始
scc[scc_cnt].clear();//存放新发现的scc
for(;;)
{
v=s.top();
s.pop();//取出当前scc中的每一个点,放入vector
Instack[v]=false;
sccno[v]=scc_cnt;
scc[scc_cnt].push_back(v);
if(u==v) break;
}
}
}
void solve()
{
sumin=sumout=0;
if(scc_cnt==1)
{
printf("0\n");
}
else
{
for(int i=1;i<=scc_cnt;i++)
{
if(in[i]==0) sumin++;
if(out[i]==0) sumout++;
}
int sum=max(sumin,sumout);
printf("%d\n",sum);
}
}
void find(int l,int r)
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(sccno,0,sizeof(sccno));
dfs_clock=scc_cnt=0;
for(int i=l;i<=r;i++)
if(!dfn[i])
tarjan(i,-1);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
getmap();
find(1,n);
suodian();
solve();
}
return 0;
}

hdoj--2767--Proving Equivalences (scc+缩点)的更多相关文章

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

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

  2. hdu 2767 Proving Equivalences 强连通缩点

    给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...

  3. HDU 2767 Proving Equivalences (强联通)

    pid=2767">http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 40 ...

  4. hdu 2767 Proving Equivalences

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

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

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

  6. HDU 2767 Proving Equivalences (Tarjan)

    Proving Equivalences Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other ...

  7. hdu 2767 Proving Equivalences(tarjan缩点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:问最少加多少边可以让所有点都相互连通. 题解:如果强连通分量就1个直接输出0,否者输出入度 ...

  8. HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)

    Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...

  9. hdu2767 Proving Equivalences Tarjan缩点

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

  10. UVALive 4287 Proving Equivalences(缩点)

    等价性问题,给出的样例为 a->b的形式,问要实现全部等价(即任意两个可以互相推出),至少要加多少个形如 a->b的条件. 容易想到用强连通缩点,把已经实现等价的子图缩掉,最后剩余DAG. ...

随机推荐

  1. HTML5 postMessage 和 localStorage 实现窗口间通信

    LocalStorage(不能跨域) 基本思想:通过localStorage的标准事件storage来实现跨页面通信,即页面A通过写入特定数据触发页面B的storage事件,页面B响应之后再写入数据通 ...

  2. Java冒泡,快速,插入,选择排序^_^+二分算法查找

    这段时间在学Java,期间学到了一些排序和查找方法.特此写来和大家交流,也方便自己的日后查看与复习. 1.下边是Java的主类: public class Get { public static vo ...

  3. 英语之ASIC

    ASIC(Application Specific Integrated Circuit) = 专用集成电路 assign = [计][数] 赋值 assign a value 分配一个值:赋值

  4. Java多线程中常见的几个问题

    我们都知道,在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 1.进程和线程的区别是什么? 进程是执行着的应用程序,而线程是进程内部的一个执行序列. ...

  5. 计算laws的matlab代码

    很简单的代码:不过花了codeforge上的10个点,自己写也早写出来了; 代码如下: 文件:calLaws.m function [y,h_v,h_h]=calLaws(x,id,LocalEner ...

  6. OpenWRT 常用软件安装

    root@Jack:/tmp/opkg-lists# opkg--help opkg:unrecognized option `--help' opkgmust have one sub-comman ...

  7. java rsa加密解密

  8. PHP stream_socket_server

    stream_socket_server - 创建一个Internet或Unix域服务器套接字 描述 资源stream_socket_server(字符串local_socket [摘要和错误号[,串 ...

  9. 【剑指Offer】19、顺时针打印矩阵

      题目描述:   输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依 ...

  10. codeforces 427D Match & Catch(后缀数组,字符串)

    题目 参考:http://blog.csdn.net/xiefubao/article/details/24934617 题意:给两个字符串,求一个最短的子串.使得这个子串在两个字符串中出现的次数都等 ...