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 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...
随机推荐
- th:each
<tr th:each="user,userStat:${users}">userStat是状态变量,有 index,count,size,current,even,o ...
- 批量修改数据sql
--insert into P_ZPROMOTION_DOC_ITEMS (AKTNR,MATNR,MINGROSS,MCRANK,MCUPRICE,MAXBAKTNR,MAXBPAMONT,MAXB ...
- LAMP(Ubuntu+apache+mysql+php)+Zend Studio 新手の PHP的开发环境搭建
因为工作需要,就从c#转型过来研究PHP.可是没想到从一开始就遇上了问题,环境配置方面的问题足足令我头疼了两天.因为博主本人对于linux的接触非常少,所以在解决这个问题的时候也学到了不少东西, 非常 ...
- table如何在过宽的时候添加滚动条
在页面中,往往由于一个table的列过于多,导致页面放不下,然后内容各种挤变形. 这里说一个解决方法,.先用一个DIV把TABLE包围起来.然后给这个DIV设置宽度,并且设置overflow:auto ...
- JavaScript console 用法大全
对于前端开发者来说,在开发过程中需要监控某些表达式或变量的值的时候,用 debugger 会显得过于笨重,取而代之则是会将值输出到控制台上方便调试.最常用的语句就是console.log(expres ...
- SQLSERVER 更改默认端口号
最近这几天,服务器的数据库(SQLSERVER)老是遭受到攻击,有人不断地轮训想登陆数据库,从SQL的日志里可以看出来,一开始我是通过本地安全策略禁用了对应的几个攻击ip,同时把数据库的sa账号给禁用 ...
- 【学习总结】Info.plist和pch文件的作用
Info.plist 建立一个工程后,会在Supporting files文件夹下看到一个“Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除 项目中其他Plis ...
- linux驱动系列之s3c2440内存布局
刚开始学习linux在2440上面 linux内核分配标志可以分为三类:行为修饰符.区修饰符.类型. 区修饰符表示从哪儿分配内存,内核把物理内存分为多个区,每个区用于不同的目的. 内存中缓冲区存在的原 ...
- 简单的SQL Server在线查看和执行系统
在大的公司里面,往往数据库和表都非常的多,一张表的字段也是会有几十个.如果这么多的表和字段没有说明注释的话,查看起来会一头雾水,新来的或者别的部门的同事看到表和字段会完全不知道是干嘛的,只能靠名称来猜 ...
- Dapper使用方法
这里记下Dapper容易忘的使用方法: 返回的数据可以有相同名称的列,会分配到不同的类上,默认使用Id这个列作为分割列 connection.Open(); //手动打开的话会保持长连接,否则每次查 ...