HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)
Proving Equivalences
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9296 Accepted Submission(s): 3281
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?
* 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.
4 0
3 2
1 2
1 3
2
m=0,没有边,那么至少添加n条边
sig=1,该图本来就是强连通图,则输出0
统计新图入度为0和出度为0的点的个数
输出最大值,就是我们至少需要添加的边的条数
缩点的原因:
强连通分量内部是互相可达的,我们只有把这些强连通分量缩成一个点,然后使得这些点构成的新图变成强连通图就可以了
所以问题得到关键是:
怎么使得新图变成强连通图(新图中不存在强连通分量,本身也不是强连通图)
有向图没有构成环的话,肯定存在链
把链头和链尾安装某个方向连接起来
链就变成环了
所以看看链头和链尾的个数就好(即出度为0和入度为0的点)
输出链头和链尾个数的最大值
因为如果连最小值条边的话,新图不一定能够变成强连通图
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 20005
#define mem(a,x) memset(a,x,sizeof(a))
int vis[max_v];
int dfn[max_v];
int low[max_v];
int color[max_v];
int stk[max_v];
int indgree[max_v];
int outdgree[max_v];
vector<int> G[max_v];
int n,m;
int sig,cnt,sp; void init()
{
mem(indgree,);
mem(outdgree,);
mem(vis,);
mem(dfn,);
mem(low,);
mem(color,);
mem(stk,);
for(int i=;i<=n;i++)
G[i].clear();
sig=;
cnt=;
sp=-;
} void tarjan(int u)
{
vis[u]=;
low[u]=dfn[u]=cnt++;
stk[++sp]=u; for(int j=;j<G[u].size();j++)
{
int v=G[u][j];
if(vis[v]==)
tarjan(v);
if(vis[v]==)
low[u]=min(low[u],low[v]);
}
if(low[u]==dfn[u])
{
sig++;
do
{
vis[stk[sp]]=-;
color[stk[sp]]=sig;
}while(stk[sp--]!=u);
}
}
int main()
{
int t;
int x,y;
cin>>t;
while(t--)
{
scanf("%d %d",&n,&m);
if(m==)
{
printf("%d\n",n);
continue;
}
init();
for(int i=;i<=m;i++)
{
scanf("%d %d",&x,&y);
if(x==y)
continue;
if(count(G[x].begin(),G[x].end(),y)==)
G[x].push_back(y);
} for(int i=;i<=n;i++)
{
if(vis[i]==)
tarjan(i);
} if(sig==)
{
printf("0\n");
continue;
}
// printf("sig=%d\n",sig);
for(int i=;i<=n;i++)
{
for(int j=;j<G[i].size();j++)
{
int v=G[i][j];
if(color[i]!=color[v])
{
indgree[color[v]]++;
outdgree[color[i]]++;
}
}
}
LL ans=,ans1=,ans2=;
for(int i=;i<=sig;i++)
{
if(indgree[i]==)
ans1++;
if(outdgree[i]==)
ans2++;
}
ans=max(ans1,ans2);
printf("%d\n",ans);
}
return ;
}
/*
给你一个有向图,问你至少添加多少条边,使得该图变成一个强连通图 特判情况:
m=0,没有边,那么至少添加n条边
sig=1,该图本来就是强连通图,则输出0 tarjan求强连通分量,同时染色缩点得到新图
统计新图入度为0和出度为0的点的个数
输出最大值,就是我们至少需要添加的边的条数 原因:
缩点的原因:
强连通分量内部是互相可达的,我们只有把这些强连通分量缩成一个点,然后使得这些点构成的新图变成强连通图就可以了
所以问题得到关键是:
怎么使得新图变成强连通图(新图中不存在强连通分量,本身也不是强连通图)
有向图没有构成环的话,肯定存在链
把链头和链尾安装某个方向连接起来
链就变成环了
所以看看链头和链尾的个数就好(即出度为0和入度为0的点)
输出链头和链尾个数的最大值 为什么是最大值?
因为如果连最小值条边的话,新图不一定能够变成强连通图 */
HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)的更多相关文章
- 2767 Proving Equivalences 至少加几条边让全部图变成强连通模板题
#include<stdio.h> #include<string.h> #define N 21000 struct node { int u,v,next; }bian[N ...
- 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(强连通 Tarjan+缩点)
Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...
- HDU 2767 Proving Equivalences (Tarjan)
Proving Equivalences Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other ...
- hdu 2767 Proving Equivalences 强连通缩点
给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...
- hdu 2767 Proving Equivalences(tarjan缩点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:问最少加多少边可以让所有点都相互连通. 题解:如果强连通分量就1个直接输出0,否者输出入度 ...
- HDU 2767:Proving Equivalences(强连通)
题意: 一个有向图,问最少加几条边,能让它强连通 方法: 1:tarjan 缩点 2:采用如下构造法: 缩点后的图找到所有头结点和尾结点,那么,可以这么构造:把所有的尾结点连一条边到头结点,就必然可以 ...
- hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- HDU1160(KB12-J DP)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- SVG生成字体图标详解
- 使用ECharts画K线图
需引入echarts.js插件,开发环境建议选择源代码版本,该版本包含了常见的警告和错误提示.下载地址 http://echarts.baidu.com/download.html 下面是代码,注释很 ...
- 设计模式(13)--Chain of Responsibility(责任链模式)--行为型
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.模式定义: 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一 ...
- 微信小程序 折叠效果
<view class='help'> <view class='help_item'> <view class='title' data-index='1' catch ...
- 第三十天- 进程 Process模块 空间隔离
1.进程: 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实体: ...
- CSS应用的小问题总结
1.两个元素换行书写时,在实际的布局中展示为两个元素之间多了一个区间(这个区间通常是因为代码在换行时,解析会自动默认为一个空格字符),所以在实际应用时,如果想要将两个元素完全无缝隙的放置在一起并排显示 ...
- 如何删除PeopleSoft Process Definition
PeopleSoft没有在页面提供删除Process Definition的快捷方式. 可以通过AD创建一个新的project加入这个process definition,在upgrade tab选择 ...
- kafka-hadoop-consumer
写了一个工具,从kafka传输数据到hdfs,采用的api,可以消费指定的kafka topic 或者为了简便可以消费所有的topic中各个partition的数据. 地址:https://githu ...
- LeetCode题解之 Implement strStr()
1.题目描述 2.题目分析 字符串操作,注意边界条件即可. 3.代码 int strStr(string haystack, string needle) { int n = needle.size( ...