Proving Equivalences

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

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
题意:n个点m条边的有向图,问最少增加多少边使图强连通。
题解:求每个scc的入度和出度,然后分别求出入度中0的个数in和出度out,取in和out中较大的一个; 
因为入度或出度为0证明这个scc和别的scc未相连,需要用一条边相连,这条边就是要加入的边,又因为一个scc可能连接多个scc,即只考虑入度或者只考虑出度都不准确
 
和昨天做的那道题一模一样,今天再做一遍 就当练练手吧
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stack>
#define MAX 50010
#define INF 0x3f3f3f
using namespace std;
struct node
{
int beg,end,next;
}edge[MAX];
int low[MAX],dfn[MAX];
int n,m,ans;
int sccno[MAX],instack[MAX];
int dfsclock,scccnt;
vector<int>newmap[MAX];
vector<int>scc[MAX];
int head[MAX];
int in[MAX],out[MAX];
stack<int>s;
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[ans].beg=u;
edge[ans].end=v;
edge[ans].next=head[u];
head[u]=ans++;
}
void getmap()
{
int a,b,i;
while(m--)
{
scanf("%d%d",&a,&b);
add(a,b);
}
}
void tarjan(int u)
{
int v,i,j;
s.push(u);
instack[u]=1;
dfn[u]=low[u]=++dfsclock;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
scccnt++;
while(1)
{
v=s.top();
s.pop();
instack[v]=0;
sccno[v]=scccnt;
if(v==u)
break;
}
}
}
void find(int l,int r)
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(sccno,0,sizeof(sccno));
dfsclock=scccnt=0;
for(int i=l;i<=r;i++)
{
if(!dfn[i])
tarjan(i);
}
}
void suodian()
{
int i;
for(i=1;i<=scccnt;i++)
{
newmap[i].clear();
in[i]=0;out[i]=0;
}
for(i=0;i<ans;i++)
{
int u=sccno[edge[i].beg];
int v=sccno[edge[i].end];
if(u!=v)
{
newmap[u].push_back(v);
in[v]++;
out[u]++;
}
}
}
void solve()
{
int i;
if(scccnt==1)
{
printf("0\n");
return ;
}
else
{
int inn=0;
int outt=0;
for(i=1;i<=scccnt;i++)
{
if(!in[i]) inn++;
if(!out[i]) outt++;
}
printf("%d\n",max(inn,outt));
}
}
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&&缩点】【求最少添加多少条边使这个图成为一个scc】的更多相关文章

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

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

  2. hdu 2767 Proving Equivalences(tarjan缩点)

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

  3. poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 503 ...

  4. hdu 2767 Proving Equivalences 强连通缩点

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

  5. HDU 2767 Proving Equivalences (强联通)

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

  6. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

  7. poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13800   Accepted: 55 ...

  8. hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

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

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

随机推荐

  1. PL/SQL学习(四)存储过程和函数

    原文参考:http://plsql-tutorial.com/ PL/SQL存储过程 存储过程相当于一个有名字的PL/SQL块,经过第一次编译后再次调用时不需要再次编译 创建格式: CREATE [O ...

  2. svn-主副分支使用

    主改bug 副加功能, :主合并到副(在副中切换主分支),副调试成功,合并回主(在主切换回副分支) 奇葩的实现了需求 主改bug 副加功能, :主合并到副(在副中切换主分支),副调试成功,合并回主(在 ...

  3. SPA架构

    databus ajax处理得到得数据 service 对databus做缓存,以及业务(如评论列表,评论详情) component 组件 html+css+js组成 evenbus 组件之间通讯,数 ...

  4. ODBC方式连接Informix数据库

    公司某个报表系统使用Informix数据库,在谋划使用Perl语言写数据采集程序后,花费了很多时间建立Perl访问Informix连接.恰巧Windows下ActivePerl的CPAN中又没有DBD ...

  5. git push用法和常见问题分析

    在使用git 处理对android的修改的过程之中总结的.但不完善 Git push $ git push origin test:master         // 提交本地test分支作为远程的m ...

  6. jquery mobile页面跳转后,必须重新刷新页面js方可有效

    最近在做个项目,用到jquery mobile,很陌生对他,问题一个个的来,那就要一个个解决,找了一天这个问题,放可明白:首先明白jqm里面页面跳转默认都是通过ajax请求的,必须重新刷新页面js方可 ...

  7. 如何利用SecureCRT连接Ubuntu12.0.4

    环境描述:虚拟机网络选择NAT连接方式,Ubuntu的版本是Ubuntu12.0.4 1. 先做一个测试,假设现在系统还没有装ssh,用secureCRT连接Ubuntu是出现下面的界面. 实际上,这 ...

  8. MyEclipse配置多个WEB容器

    MyEclipse支持多个同版本WEB容器同时运行 打开 然后按下图操作 咱们就得到了 下面需要配置新增加WEB容器的启动路径,在新增加的WEB容器上点击右键,选择箭头指向的菜单 打开的窗口如图,可以 ...

  9. AndroidStudio Gradle版本不匹配问题

    报错信息: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } Error:(1, 1) A problem occurr ...

  10. Web应用的组件化(二)

    管控平台 在上一篇中我们提到了组件化的大致思路,这一篇主要讲述在这么做之后,我们需要哪些外围手段去管控整个开发过程.从各种角度看,面对较大规模前端开发团队,都有必要建立这么一个开发阶段的协作平台. 在 ...