HDU 2767 Proving Equivalences (Tarjan)
Proving Equivalences
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
* 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
* 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
#include<bits/stdc++.h>
using namespace std;
int n,T,m,index,team_num;
int low[],dfn[],team[],in[],out[];
bool instack[];
vector<int> mp[];
stack<int> S;
void Tarjan ( int u )
{
dfn[u]=low[u]=++index;
S.push(u);
instack[u]=;
for ( int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
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]) //构成强连通分量
{
team_num++; //组数
while () //同一组标号
{
int v=S.top(); S.pop();
instack[v]=;
team[v]=team_num;
if (v==u) break;
}
}
} void dfs()
{
memset(team,,sizeof(team));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(instack,,sizeof(instack));
team_num=;
index=;
for(int i=;i<=n;i++)
if (!dfn[i]) Tarjan(i);
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) mp[i].clear();
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x].push_back(y);
}
dfs(); //缩点
/*for(int i=1;i<=n;i++)
printf("%d:%d\n",i,team[i]);*/ for(int i=;i<=team_num;i++) in[i]=out[i]=;
for(int i=;i<=n;i++)
for(int j=;j<mp[i].size();j++)
{
if (team[i]!=team[mp[i][j]])
{
out[ team[i] ]++;
in[ team[mp[i][j]] ]++;
}
}
int innum=,outnum=;
for(int i=;i<=team_num;i++)
{
if (!in[i]) innum++;
if (!out[i]) outnum++;
}
if (team_num==) printf("0\n");
else printf("%d\n",max(innum,outnum));
}
return ;
}
HDU 2767 Proving Equivalences (Tarjan)的更多相关文章
- hdu 2767 Proving Equivalences(tarjan缩点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:问最少加多少边可以让所有点都相互连通. 题解:如果强连通分量就1个直接输出0,否者输出入度 ...
- 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+缩点)
Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...
- 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(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:给出n个点m条边,问在m条边的基础上,最小再添加多少条边可以让图变成强连通.思路:强连通分量缩点后找 ...
- HDU 2767:Proving Equivalences(强连通)
题意: 一个有向图,问最少加几条边,能让它强连通 方法: 1:tarjan 缩点 2:采用如下构造法: 缩点后的图找到所有头结点和尾结点,那么,可以这么构造:把所有的尾结点连一条边到头结点,就必然可以 ...
- hdu 4635 Strongly connected (tarjan)
题意:给一个n个顶点m条弧的简单有向图(无环无重边),求最多能够加入多少条弧使得加入后的有向图仍为简单有向图且不是一个强连通图.假设给的简单有向图本来就是强连通图,那么输出-1. 分析: 1.用tar ...
- hdu 2767 Proving Equivalences 强连通缩点
给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...
随机推荐
- script 加载顺序问题的延展研究
今天群里有人问为什么会出现脚本的加载顺序与定义脚本顺序不一致的问题,这个问题引起了我的好奇,经过一番调研,有了这篇文章. 这是一个伪命题吗? 首先,W3C 推荐 script 脚本应该被立即加载和执行 ...
- Python3基础 try-except else进行配合
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Notepad++7.5.4 设置主题,使用插件
首先官网下载 Notepad++7.5.4 默认英文转换成中文 下面设置主题: 设置-->语言格式设置 选择主题Obsidian,字体选择等宽字体Consolas,大小为11,选择全局字体,使用 ...
- hdu 1671 Phone List(字典树)题解
题意:给一连串数字,如果有前缀重复给出NO,否则给出YES 思路:这道题要delete否则爆内存,之前想的直接在insert()里解决查询有错误,所以先保存数据再查询. 代码: #include< ...
- system.data.sqlite的源代码下载
帮助文档 http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki 历史版本https://system.data.sqlit ...
- 51nod 1103 N的倍数
1103 N的倍数 一个长度为N的数组A,从A中选出若干个数,使得这些数的和是N的倍数. 例如:N = 8,数组A包括:2 5 6 3 18 7 11 19,可以选2 6,因为2 + 6 = 8, ...
- CodeForces Round #516 Div2 题解
A. Make a triangle! 暴力... 就是给你三个数,你每次可以选一个加1,问最少加多少次能构成三角形 #include <bits/stdc++.h> #define ll ...
- C#下载歌词文件
前段时间写了一篇c#解析Lrc歌词文件,对lrc文件进行解析,支持多个时间段合并.本文借下载歌词文件来探讨一下同步和异步方法. Lrc文件在网络上随处可见,我们可以通过一些方法获取,最简单的就是别人的 ...
- 网页图片提取助手(支持背景图、选择dom范围)
网页图片提取助手(支持背景图.选择dom范围) 网页图片下载工具.网页图片批量保存. 使用场景: 作为web前端开发首——学习小生的你我,仿学在线页面是常有的事,但是一些在线资源,比如图片,图片有im ...
- UVa 10003 切木棍(区间DP+最优矩阵链乘)
https://vjudge.net/problem/UVA-10003 题意: 有一根长度为L的棍子,还有n个切割点的位置.你的任务是在这些切割点的位置处把棍子切成n+1部分,使得总切割费用最小.每 ...