hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】
Proving Equivalences
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4263 Accepted Submission(s):
1510
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?
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.
* One line with the minimum number
of additional implications that need to be proved in order to prove that all
statements are equivalent.
#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】的更多相关文章
- 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(tarjan缩点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:问最少加多少边可以让所有点都相互连通. 题解:如果强连通分量就1个直接输出0,否者输出入度 ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- 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 ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13800 Accepted: 55 ...
- hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- CentOS 6.4 通过Yum给Chrome安装Adobe Flash Player
方法一:安装 Flash Player yum install flash-plugin 安装好后,重新启动chrome,在地址栏输入[chrome://plugins/]确定 Shockware F ...
- Mysql 与 php动态网站开发 入门教程
这个系列的教程由表单开始写,因为表单可以把数据库和web 之间的交互表现得很明显.提交表单 ,数据库记录注册信息. 本教程属于基础教程.大神请略过. 对于php和mysql之间的稳固性很 ...
- “\n”与“\r”的区别
ASCII中“\n”代表着换行,“\r”代表着将光标移动到当前显示行的最左边.
- makefile debug
1. 使用warning指令 warning 是个不错的命令,可以打印出消息,来判断makefile执行的流程 2.使用ifeq ifneq 当makefile被多次调用到的时候,如果都输出warni ...
- HTML5之本地文件系统API - File System API
HTML5之本地文件系统API - File System API 新的HTML5标准给我们带来了大量的新特性和惊喜,例如,画图的画布Canvas,多媒体的audio和video等等.除了上面我们提到 ...
- 《Linux命令行大全》系列(三、Linux 系统)
在<Linux命令行大全>一书中,第3章名称是 Linux 系统. 概念太大,不过该节内容却是 Linux 系统最为核心的基础——查看 Linux 系统. ls 命令 显示目录自身信息或目 ...
- Hbase案例分析(一)
Hbase应用场景: 1 随机读或者写 2 大数据上的高并发操作,比如每秒对PB级数据进行上千次操作.(查询,删除等操作) 3 读写均是非常简单的操作,比如没有join操作 Hbase Schema设 ...
- webservice 的权限验证
1. http://blog.csdn.net/jaune161/article/details/25602655 2. http://wcp88888888.iteye.com/blog/13993 ...
- PHP错误Warning: Cannot modify header information - headers already sent by解决方法
这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下 今天在 ...
- Word续上表
选中表格的下半部分(全部下半部分),然后按ctrl+shift+enter,这样就换行了.