hdu4635Strongly connected
http://acm.hdu.edu.cn/showproblem.php?pid=4635
tarjan缩点 统计缩点后每个结点的出度入度 将那个包含原来点数最少的 且出度或者入度为0的大节点看作一个整体内部连边n*(n-1)个 连全部的; 其它的点为一整体连全部的 再两者连一同向的边 保证它的出度或者入度依旧为0的情况下任意连 最后减去原来的边M
#include <iostream>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include<stack>
using namespace std;
#define N 100010
#define M 100010
#define INF 0xfffffff
int a[][];
stack<int>s;
struct node
{
int u,v,next;
}ed[M*];
int scc,sccno[N],head[N],lowlink[N],pre[N],dep,num[N],x[M],y[M],din[N],dout[N],o;
void init()
{
o = ;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
ed[o].u = u;
ed[o].v = v;
ed[o].next = head[u];
head[u] = o++;
}
void dfs(int u)
{
int i;
lowlink[u] = pre[u] = ++dep;
s.push(u);
for( i = head[u] ; i != - ; i = ed[i].next)
{
int v = ed[i].v;
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++;
for(;;)
{
int x = s.top();
s.pop();
sccno[x] = scc;
if(x==u) break;
}
}
}
void find_scc(int n)
{
for(int i = ; i <= n ;i++)
if(!pre[i])
dfs(i);
}
int main()
{
int i,t,n,m,a,b,kk=;
cin>>t;
while(t--)
{
init();kk++;
memset(num,,sizeof(num));
memset(pre,,sizeof(pre));
memset(din,,sizeof(din));
memset(dout,,sizeof(dout));
memset(sccno,,sizeof(sccno));
scanf("%d%d",&n,&m);
for(i = ; i <= m ; i++)
{
scanf("%d%d",&a,&b);
x[i] = a;y[i] = b;
add(a,b);
}
scc=;dep=;
find_scc(n);
printf("Case %d: ",kk);
if(scc==)
{
cout<<"-1\n";
continue;
}
for(i = ; i <= n ; i++)
num[sccno[i]]++;
for(i = ; i <= m ;i++)
{
if(sccno[x[i]]!=sccno[y[i]])
{
dout[sccno[x[i]]]++;
din[sccno[y[i]]]++;
}
}
long long minz = INF;
for(i = ;i <= scc ; i++)
if(minz>num[i]&&(din[i]==||dout[i]==))
minz = num[i];
long long ss=;
ss = minz*(minz-)+(n-minz)*(n-minz-)+minz*(n-minz);
cout<<ss-m<<endl;
}
return ;
}
hdu4635Strongly connected的更多相关文章
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- PTA Strongly Connected Components
Write a program to find the strongly connected components in a digraph. Format of functions: void St ...
- poj 1737 Connected Graph
// poj 1737 Connected Graph // // 题目大意: // // 带标号的连通分量计数 // // 解题思路: // // 设f(n)为连通图的数量,g(n)为非连通图的数量 ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- Windows Phone 8 解锁提示IpOverUsbSvc问题——IpOverUsbEnum返回No connected partners found解决方案
我的1520之前总是无法解锁,提示:IpOverUsbSvc服务没有开启什么的. 根据网上网友的各种解决方案: 1. 把手机时间设置为当前时间,并且关闭“自动设置” 2. 确保手机接入了互联网 3.确 ...
- POJ1737 Connected Graph
Connected Graph Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3156 Accepted: 1533 D ...
- [LintCode] Find the Weak Connected Component in the Directed Graph
Find the number Weak Connected Component in the directed graph. Each node in the graph contains a ...
- Supporting Connected Routes to Subnet Zero
Supporting Connected Routes to Subnet Zero IOS allows the network engineer to tell a router to eithe ...
- lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素
题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...
随机推荐
- NativeExcel 读取文件
class function T_EShopDataBill.ImportData(const AFileName: String; AList: T_EShopDataModelList; var ...
- Python垃圾回收机制详解
一.垃圾回收机制 Python中的垃圾回收是以引用计数为主,分代收集为辅.引用计数的缺陷是循环引用的问题. 在Python中,如果一个对象的引用数为0,Python虚拟机就会回收这个对象的内存. #e ...
- MyEclipse的一些配置
1.配置默认编码 配置整个MyEclipse的默认编码 window--->Preferences-->General-->WorkSpace--->TextFileEncod ...
- oracle 导入报错 ORA-00959: tablespace 'HB' does not exist
导入oracle 时发现有几张表导入时一直报错: 报错信息:IMP-00003: ORACLE error 959 encountered ORA-00959: tablespace 'HB' d ...
- WPF常用数据绑定控件集合
1.怎么用ListView控件把XML中的数据在界面上显示出来? <?xml version="1.0" encoding="utf-8" ?> & ...
- To get TaskID's Integer ID value from the GUID in SharePoint workflow
list.GetItemByUniqueId(guid).ID int itemID = spList.Items[new Guid("")].ID;
- linux编程之指针
这个是数组指针.指针数组.二维数组之间相互转换的代码 #include<stdio.h> void main() { ][]={,,,,,,,}; int *b=NULL; int **c ...
- c++ 私有函数 头文件设计
clock.h #ifndef CLOCK_H_INCLUDED #define CLOCK_H_INCLUDED class Clock {public: static void HandleExd ...
- uva 10791
还算比较水的一个数学题 求因子的最小和 总是用小的数去除 注意特判 是用int不行哦........ #include <cstdio> #include <cmath> ...
- spoj 247
不管行列 总是先切割切割费用大的 代码比较烂 ...... #include <iostream> #include <cstdio> #include <cstr ...