UVALIVE 4287 Proving Equivalences (强连通分量+缩点)
题意:给定一个图,问至少加入多少条边能够使这个图强连通。
思路:首先求出这个图的强连通分量。然后把每个强连通分量缩成一个点。那么这个图变成了一个DAG,求出全部点的入度和出度,由于强连通图中每个节点的入度和出度至少为1。那么我们求出入度为零的节点数量和出度为零的节点数量。答案取最大值,由于在一个DAG中加入这么多边一定能够使这个图强连通。注意当这个图本身强连通时要特判一下,答案为零。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define pii (pair<int, int>)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; const int maxn = 30000;
//const int INF = 0x3f3f3f3f;
int n, m; //强连通分量
vector<int> G[maxn];
int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt;
stack<int> S;
void dfs(int u) {
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if(!pre[v]) {
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
} else if(!sccno[v]) {
lowlink[u] = min(lowlink[u], pre[v]);
}
}
if(lowlink[u] == pre[u]) {
scc_cnt++;
for(;;) {
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
}
void find_scc(int n) {
dfs_clock = scc_cnt = 0;
memset(sccno, 0, sizeof(sccno));
memset(pre, 0, sizeof(pre));
for(int i = 1; i <= n; i++)
if(!pre[i]) dfs(i);
}
int in[maxn], out[maxn];
int main() {
//freopen("input.txt", "r", stdin);
int T; cin >> T;
while(T--) {
cin >> n >> m;
for(int i = 1; i <= n; i++) G[i].clear();
for(int i = 0; i < m; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
}
find_scc(n);
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
for(int i = 1; i <= n; i++) {
for(int j = 0; j < G[i].size(); j++) {
if(sccno[i] != sccno[G[i][j]]) out[sccno[i]]++, in[sccno[G[i][j]]]++;
}
}
int sin = 0, sout = 0;
for(int i = 1; i <= scc_cnt; i++) {
if(!in[i]) sin++;
if(!out[i]) sout++;
}
int ans = scc_cnt==1 ? 0 : max(sin, sout);
cout << ans << endl;
}
return 0;
}
UVALIVE 4287 Proving Equivalences (强连通分量+缩点)的更多相关文章
- UvaLive 4287 Proving Equivalences 强连通缩点
原题链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive - 4287 - Proving Equivalences(强连通分量)
Problem UVALive - 4287 - Proving Equivalences Time Limit: 3000 mSec Problem Description Input Outp ...
- UVALive Proving Equivalences (强连通分量,常规)
题意: 给一个有向图,问添加几条边可以使其强连通. 思路: tarjan算法求强连通分量,然后缩点求各个强连通分量的出入度,答案是max(入度为0的缩点个数,出度为0的缩点个数). #include ...
- HDU2767Proving Equivalences[强连通分量 缩点]
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- UVALive - 4287 Proving Equivalences
给定n个命题之间的已经证明的关系如 a b表示已经证明蕴含式a→b,要求还需要再作多少次证明使得所有的命题都是等价的.将每个命题看成一个点,已经证明的命题之间连一条边,问题转化为添加多少条单向边使得图 ...
- UVALive 4287 Proving Equivalences(缩点)
等价性问题,给出的样例为 a->b的形式,问要实现全部等价(即任意两个可以互相推出),至少要加多少个形如 a->b的条件. 容易想到用强连通缩点,把已经实现等价的子图缩掉,最后剩余DAG. ...
- HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)
Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...
- 训练指南 UVALive - 4287 (强连通分量+缩点)
layout: post title: 训练指南 UVALive - 4287 (强连通分量+缩点) author: "luowentaoaa" catalog: true mat ...
- HD2767Proving Equivalences(有向图强连通分量+缩点)
题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...
随机推荐
- React 中的 refs的应用
React Refs React 支持一种非常特殊的属性 Ref ,你可以用来绑定到 render() 输出的任何组件上. 这个特殊的属性允许你引用 render() 返回的相应的支撑实例( back ...
- Jquery学习总结(3)——Jquery获取当前城市的天气信
Jquery代码: function findWeather() { var cityUrl = 'http://int.dpool.sina.com.cn/iplookup/iplookup ...
- HAVING使用子查询
HAVING使用子查询 //查询各部门平均工资,显示平均工资大于 //公司整体平均工资的记录 select deptno,avg(sal) from emp group by ...
- 关于python的hashlib md5的报错处理
1.报错信息是:TypeError: Unicode-objects must be encoded before hashing 2.报错信息是:TypeError: object supporti ...
- node--20 moogose demo2
db.js /** * Created by Danny on 2015/9/28 16:44. */ //引包 var mongoose = require('mongoose'); //创建数据库 ...
- Stop being a perfectionist
节选自 7 Things You Need To Stop Doing To Be More Productive, Backed By Science “We found that perfecti ...
- ES什么时候会平衡分片
"cluster.routing.allocation.balance.shard":"0.45f",//定义分配在该节点的分片数的因子 阈值=因子*(当前节点 ...
- mybatis使用注解替代xml配置,动态生成Sql
mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...
- Linux mount挂载umount卸载
mount/umount挂载/卸载 对于Linux用户来讲,不论有几个分区,分别分给哪一个目录使用,它总归就是一个根目录.一个独立且唯一的文件结构 Linux中每个分区都是用来组成整个文件系统的一部分 ...
- jquery的clone()引发的问题,下拉框点击没有反应
此段代码是对某块元素的移位:上移.下移.对比修改前后的两段代码: 修改前: //点击移位 function move(obj,posi){ var al=$(obj).parent('li').par ...