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的更多相关文章

  1. [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), ...

  2. PTA Strongly Connected Components

    Write a program to find the strongly connected components in a digraph. Format of functions: void St ...

  3. poj 1737 Connected Graph

    // poj 1737 Connected Graph // // 题目大意: // // 带标号的连通分量计数 // // 解题思路: // // 设f(n)为连通图的数量,g(n)为非连通图的数量 ...

  4. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  5. Windows Phone 8 解锁提示IpOverUsbSvc问题——IpOverUsbEnum返回No connected partners found解决方案

    我的1520之前总是无法解锁,提示:IpOverUsbSvc服务没有开启什么的. 根据网上网友的各种解决方案: 1. 把手机时间设置为当前时间,并且关闭“自动设置” 2. 确保手机接入了互联网 3.确 ...

  6. POJ1737 Connected Graph

    Connected Graph Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3156   Accepted: 1533 D ...

  7. [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 ...

  8. Supporting Connected Routes to Subnet Zero

    Supporting Connected Routes to Subnet Zero IOS allows the network engineer to tell a router to eithe ...

  9. lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素

    题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...

随机推荐

  1. ASP.Net MVC中JSON处理。

    实体数据Model [Serializable] public class UserModel { //public UserModel(string name, string classname, ...

  2. 修改zepto源代码,使支持wp8的ie10

    注意:当前1.1.3版本的zepto,已经有模块来支持wp8 原先的zepto,通过__proto__赋值,来使dom继承到$.fn方法, 无奈IE11之前的IE10,IE9不支持这种写法, 所以我们 ...

  3. 深入mongoDB(1)--mongod的线程模型与网络框架

    最近工作需要开始研究mongoDB,我准备从其源代码角度,对于mongod和mongos服务的架构.sharding策略. replicaset策略.数据同步容灾.索引等机制做一个本质性的了解.其代码 ...

  4. [译] ASP.NET 生命周期 – ASP.NET 请求生命周期(四)

    不使用特殊方法来处理请求生命周期事件 HttpApplication 类是全局应用类的基类,定义了可以直接使用的一般 C# 事件.那么使用标准 C# 事件还是特殊方法那就是个人偏好的问题了,如果喜欢, ...

  5. 关于count(1) 和 count(*)

    Q:What is the difference between count(1) and count(*) in a sql queryeg.select count(1) from emp; an ...

  6. [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)(转)

    转载自:http://blog.csdn.net/onlyou930/article/details/9299169 分类: Objective-C2013-07-11 11:54 3420人阅读 评 ...

  7. UITableView自定义单元格

    随手笔记: RootViewController代码 #import "RootViewController.h" #import "AddressContact.h&q ...

  8. 使用struts的模型驱动注意的问题

    注意实体对象的属性命名一定要规范, 例如: private String fName; 添加时模型驱动取不到值 private String fname; 这个可以

  9. oracle安装完成后解锁scott用户

    需要以管理员的身份 进行 解锁scott alter user scott account unlock;

  10. Mac 使用phpMyAdmin

    1 把phpMyAdmin-4.6.5.2-all-languages.zip文件解压到“/Library/WebServer/Documents/”中,并改名为phpmyadmin. 2 复制“/L ...