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 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...
随机推荐
- jquery Loading图片延迟加载特效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 使用FileResult导出txtl数据文件
public FileResult ExportMobileNoTxt(SearchClientModel model){ var sbTxt = new StringBuilder(); ; i & ...
- $.ligerDialog 操作
//关闭 $.ligerDialog.open 打开的弹窗 frameElement.dialog.close(); //关闭父窗口 parent.$.ligerDialog.close(); //关 ...
- Spark机器学习 Day1 机器学习概述
Spark机器学习 Day1 机器学习概述 今天主要讨论个问题:Spark机器学习的本质是什么,其内部构成到底是什么. 简单来说,机器学习是数据+算法. 数据 在Spark中做机器学习,肯定有数据来源 ...
- Inside of Jemalloc
INSIDE OF JEMALLOCThe Algorithm and Implementation of Jemalloc author: vector03mail: mmzsmm@163.co ...
- Oracle中的USEREVN()
Oracle中的USEREVN() 1.USEREVN() 返回当前用户环境的信息,opt可以是: ENTRYID,SESSIONID,TERMINAL,ISDBA,LABLE,LANGUAGE,CL ...
- Unity3d读取.csv文件
原文地址:http://blog.csdn.net/dingkun520wy/article/details/26594991 (一)文件路径 需要把csv文件放在StreamingAssets这个文 ...
- 回顾Ado.Net
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...
- 自己开发开源jquery插件--给jquery.treeview加上checkbox
很多时候需要把树状的数据显示除来,比如分类,中国省份.城市信息,等,因此这方面的javascript插件也有很多.比如性能优异的jquery.treeview和国人开发的功能强大的zTree. 我最近 ...
- 然爸读书笔记(2014-10)----Bootsrap用户手册
本来其实这方面的知识,可以直接在网站上看,但是自己是web前端的小白.基本不懂.所以,还是买一本书来看,更好些.体现出我对前端的重视. Bootstrap是快速开发Web应用程序的前端工具包.它是一个 ...