Proving Equivalences (hdu 2767 强联通缩点)
Proving Equivalences
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3743 Accepted Submission(s): 1374
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.
2
4 0
3 2
1 2
1 3
4
2
pid=2773" target="_blank" style="color:rgb(26,92,200); text-decoration:none">2773
pid=2772" target="_blank" style="color:rgb(26,92,200); text-decoration:none">2772
题意:n个点m条边,问最少加入多少条边使得整个图联通。
思路:先Tarjan求强联通分量,缩点,再求缩点后的点的入度和出度,入读为0的点的个数为a。出度为0的点的个数为b,ans=max(a。b)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std; const int MAXN = 20050;//点数
const int MAXM = 500050;//边数 struct Edge
{
int to,next;
}edge[MAXM]; int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc
int Index,top;
int scc;//强联通分量的个数
bool Instack[MAXN];
int num[MAXN];//各个强联通分量包括的点的个数。数组编号为1~scc
//num数组不一定须要,结合实际情况 void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void Tarjan(int u)
{
int v;
Low[u]=DFN[u]=++Index;
Stack[top++]=u;
Instack[u]=true;
for (int i=head[u];i+1;i=edge[i].next)
{
v=edge[i].to;
if (!DFN[v])
{
Tarjan(v);
if (Low[u]>Low[v]) Low[u]=Low[v];
}
else if (Instack[v]&&Low[u]>DFN[v])
Low[u]=DFN[v];
}
if (Low[u]==DFN[u])
{
scc++;
do{
v=Stack[--top];
Instack[v]=false;
Belong[v]=scc;
num[scc]++;
}while (v!=u);
}
} void solve(int N)
{
memset(DFN,0,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
memset(num,0,sizeof(num));
Index=scc=top=0;
for (int i=1;i<=N;i++) //点的编号从1開始
if (!DFN[i])
Tarjan(i);
} void init()
{
tot=0;
memset(head,-1,sizeof(head));
} int n,m;
int in[MAXN],out[MAXN]; int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);
#endif
int i,j,u,v,t;
sf(t);
while (t--)
{
sff(n,m);
if(n==1){ //特判1(n==1,m==0)
printf("0\n");
continue;
}
if(m==0){ //特判2( n==? ,m==0)
printf("%d\n",n);
continue;
}
init();
for (i=0;i<m;i++)
{
sff(u,v);
addedge(u,v);
}
solve(n);
if(scc==1){ //假设强连通个数为1
printf("0\n");
continue;
}
mem(in,0);
mem(out,0);
for (int u=1;u<=n;u++)
{
for (i=head[u];i+1;i=edge[i].next)
{
int v=edge[i].to;
if (Belong[u]!=Belong[v])
{
out[Belong[u]]++;
in[Belong[v]]++;
}
}
}
int ans,a=0,b=0;
for (i=1;i<=scc;i++)
{
if (out[i]==0)
a++;
if (in[i]==0)
b++;
}
ans=max(a,b);
pf("%d\n",ans);
}
return 0;
}
Proving Equivalences (hdu 2767 强联通缩点)的更多相关文章
- Intelligence System (hdu 3072 强联通缩点+贪心)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu2767 Proving Equivalences,有向图强联通,Kosaraju算法
点击打开链接 有向图强联通,Kosaraju算法 缩点后分别入度和出度为0的点的个数 answer = max(a, b); scc_cnt = 1; answer = 0 #include<c ...
- HDU 2767-Proving Equivalences(强联通+缩点)
题目地址:pid=2767">HDU 2767 题意:给一张有向图.求最少加几条边使这个图强连通. 思路:先求这张图的强连通分量.假设为1.则输出0(证明该图不须要加边已经是强连通的了 ...
- HDU 6170 FFF at Valentine(强联通缩点+拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6165 题意:给你一个无环,无重边的有向图,问你任意两点,是否存在路径使得其中一点能到达另一点 解析:强 ...
- POJ 2186 Popular Cows(强联通+缩点)
Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...
- [bzoj 1093][ZJOI2007]最大半联通子图(强联通缩点+DP)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1093 分析: 首先肯定是先把强联通全部缩成一个点,然后成了一个DAG 下面要知道一点: ...
- POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)
[题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...
- HDU 5934 强联通分量
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 4612 双联通缩点+树形dp
#pragma comment(linker,"/STACK:102400000,102400000")//总是爆栈加上这个就么么哒了 #include<stdio.h> ...
随机推荐
- pat 甲级 1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PHP一维数组和二维数字排序整理
<?php /** 一维数组排序 sort() - 以升序对数组排序 rsort() - 以降序对数组排序 asort() - 根据值,以升序对关联数组进行排序 ksort() - 根据键,以升 ...
- Some lines about EF Code First migration.
Some lines about EF Code First migration: 一. 模型设计 1. 遵循EF标准,注意表关系配对 2. 数据模型里尽量把必须的属性和说明都写全 3. EF默认id ...
- 【HugeChm】HugeChm制作chm帮助文档
1.下载软件:HugeChm.exe 2.开始打包: 3.选择开始打包即可
- GPIO和门电路
1. GPIO 1.1 简介 GPIO, General Purpose I/O, 通用输入输出接口, 是最简单的数字输入输出引脚 - 作为输出可以有两种状态: 0和1 - 作为输入,它接收外面输入的 ...
- 深入Java数据类型
Java的数据类型分为两大类,一类是基本数据类型,还有一类就是引用数据类型. 1.基本数据类型 Java一共有8种基本数据类型,分别是byte,short,int,long,float,double, ...
- 使用Fiddle监听HTTPS网页
HTTPS相对于HTTP增加了安全性,但是仍然不能有效的防止中间人攻击(Man-in-the-MiddleAttack,简称“MITM攻击”) 这就使得Fiddle工具能够有效的监听HTTPS流量 一 ...
- [Math Review] Linear Algebra for Singular Value Decomposition (SVD)
Matrix and Determinant Let C be an M × N matrix with real-valued entries, i.e. C={cij}mxn Determinan ...
- 使用PreloadJS加载图片资源
一. 使用createjs里的LoadQueue函数实现异步加载图片,监听加载进度 1.实例对象LoadQueue加载队列对象 var queue = new createjs.LoadQueue(f ...
- 在delphi中,如何把十进制数转换为十六进制整形数。若用inttohex只能转化为十六进制字符串。
var b: Byte; s: string;begin s := '31'; //16进制字符串 b := StrToInt('$' + s);end; 不过要注意一点,如果在程序调试时想看b的值, ...