hdoj--2767--Proving Equivalences (scc+缩点)
Proving Equivalences
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
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?
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.
2
4 0
3 2
1 2
1 3
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+缩点)的更多相关文章
- hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 2767 Proving Equivalences 强连通缩点
给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...
- HDU 2767 Proving Equivalences (强联通)
pid=2767">http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 40 ...
- hdu 2767 Proving Equivalences
Proving Equivalences 题意:输入一个有向图(强连通图就是定义在有向图上的),有n(1 ≤ n ≤ 20000)个节点和m(0 ≤ m ≤ 50000)条有向边:问添加几条边可使图变 ...
- HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 2767 Proving Equivalences (Tarjan)
Proving Equivalences Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other ...
- hdu 2767 Proving Equivalences(tarjan缩点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:问最少加多少边可以让所有点都相互连通. 题解:如果强连通分量就1个直接输出0,否者输出入度 ...
- HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)
Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...
- hdu2767 Proving Equivalences Tarjan缩点
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- UVALive 4287 Proving Equivalences(缩点)
等价性问题,给出的样例为 a->b的形式,问要实现全部等价(即任意两个可以互相推出),至少要加多少个形如 a->b的条件. 容易想到用强连通缩点,把已经实现等价的子图缩掉,最后剩余DAG. ...
随机推荐
- C# 多线程系列(三)
线程池 创建线程需要时间,如果有不同的小任务要完成,就可以事先创建许多线程,在应完成这些任务时发出请求.这个线程数最好在需要更多线程时增加,在需要释放资源时减少. 不需要自己创建这样的一个列表.该列表 ...
- lua 10进制转换成其它进制table表示
-- params@num integer -- ~) 默认为10 -- NOTE:先不输出符号 function NumberToArray(num, radix) if type(num) ~= ...
- HTTPS的中那些加密算法
密码学在计算机科学中使用非常广泛,HTTPS就是建立在密码学基础之上的一种安全的通信协议.HTTPS早在1994年由网景公司首次提出,而如今在众多互联网厂商的推广之下HTTPS已经被广泛使用在各种大小 ...
- mysql数据库之存储过程入门
引用:百度百科 存储过程 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存 ...
- 【PostgreSQL-9.6.3】函数(2)--字符型函数
在上一篇博文中我们交流了数值型函数,这篇我们将讨论PostgreSQL中的字符型函数. 1. reverse(string) reverse函数可以将string字符串的字母显示顺序颠倒. test= ...
- VS2012 +OpenCv2.4.4配置
使用OpenCV少了数据读取.填充.存储的麻烦. 转载于opencv官网:对于2010和2.43的配置可以直接挪用到新配置环境 http://www.opencv.org.cn/index.php/V ...
- (转)Bootstrap 之 Metronic 模板的学习之路 - (5)主题&布局配置
https://segmentfault.com/a/1190000006736457 Theme Setup 主题配置 Metronic comes with 6 color themes,defa ...
- LeetCode -- 1038. Binary Search Tree to Greater Sum Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 洛谷 P1068 分数线划定【排序+模拟】
世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,AA市对 所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试.面试分数线根 据计划录取人数的150\%150%划 ...
- Linux挂载NAS共享文件夹
[root@ftp:/mnt] > mount -o username=user01,password=1234567890 //192.168.31.20/share /mnt/nas [ro ...