题意:给你一个连通图,问你最多加多少条边,还能保证该图不是强连通图。

对整个图求强连通分量,然后对图缩点,记录一下缩点之后每隔点包含的原来的点的个数,找出最少的那个点,然后对这个点建成完全图,对另外的所有点建成完全图。然后+两个点建边-所有原来的遍就好了。

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635

 #include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stack> using namespace std;
const int maxn = ; struct edge
{
int u,v,val;
};
int dfn[],low[],belong[],inst[];
int pnum[];
int in[];
int inside[];
int out[];
vector<edge> edges,es;
vector<int>g[maxn];
vector<int>ng[maxn];
stack<int>st;
int bcnt,cnt,dfsclock;
int max(int a,int b)
{
if(a > b)
return a; return b;
}
void init(int n)
{
int i;
for(i =;i <= n;i++)
g[i].clear(); edges.clear(); es.clear();
dfsclock = ;bcnt = cnt = ;
return ;
}
void addedge(int u,int v,int val)
{
edges.push_back((edge){u,v,});
g[u].push_back(cnt);
cnt++; return ;
}
void tarjan(int i)
{
int j;
dfn[i] = low[i] = ++dfsclock;
inst[i] = ;
st.push(i); for(j = ;j < g[i].size();j++)
{
edge e;
e = edges[g[i][j]];
int v;
v = e.v;
if(!dfn[v])
{
tarjan(v);
low[i] = min(low[i],low[v]);
}
else if(inst[v] && dfn[v] < low[i])
low[i] = dfn[v];
}
if(dfn[i] == low[i])
{
bcnt++;
do
{
j = st.top();
st.pop();
inst[j] = ;
belong[j] = bcnt; }
while(j != i);
} }
void tarjans(int n)
{
int i;
bcnt = dfsclock = ;
while(!st.empty())st.pop();
memset(dfn,,sizeof(dfn)); memset(inst,,sizeof(inst));
memset(belong,,sizeof(belong));
for(i = ;i <= n;i++)
if(!dfn[i])tarjan(i);
}
int main()
{
int n,m;
int t; scanf("%d",&t);
int cas = ;
while(t--)
{
scanf("%d %d",&n,&m);
int a,b,i;
init(n);
for(i = ;i < m;i++)
{
scanf("%d %d",&a,&b);
addedge(a,b,);
struct edge x;
x.u = a;
x.v = b;
x.val = ;
es.push_back(x); }
tarjans(n); printf("Case %d: ",++cas);
if(bcnt == )
{
puts("-1");
continue;
}
int pnum[];
int in[];
int out[];
memset(pnum,,sizeof(pnum));
memset(in,,sizeof(in));
memset(out,,sizeof(in));
memset(inside,,sizeof(inside));
for(i = ;i <= n;i++)
{
pnum[belong[i]]++;
} for(i = ;i < m;i++)
{
int u,v;
u = es[i].u;
v = es[i].v;
if(belong[u] != belong[v])
{
out[belong[u]]++;
in[belong[v]]++;
}
}
__int64 ans;
ans = n; for(i = ;i <= bcnt;i++)
{
if(in[i] == || out[i] == )
{
if(ans > pnum[i])
ans = pnum[i];
}
} ans = (__int64)(n-ans)* (__int64)(n-ans-)+ (__int64)ans* (__int64)(ans-)+ (__int64)ans*(__int64)(n-ans)- (__int64)(m);
// printf("bcnt %d p %d ams %d ansi %d\n",bcnt,pnum[ansi],ans,ansi); printf("%I64d\n",ans); }
return ;
}

Strongly connected 挺简单的tarjan的更多相关文章

  1. HDU 4635 Strongly connected (Tarjan+一点数学分析)

    Strongly connected Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  2. PTA Strongly Connected Components

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

  3. algorithm@ Strongly Connected Component

    Strongly Connected Components A directed graph is strongly connected if there is a path between all ...

  4. Strongly connected(hdu4635(强连通分量))

    /* http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/ ...

  5. HDU4625:Strongly connected(思维+强连通分量)

    Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. HDU 4635 Strongly connected (2013多校4 1004 有向图的强连通分量)

    Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. HDU 4635 Strongly connected(强连通)经典

    Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. cf475B Strongly Connected City

    B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  9. 【CF913F】Strongly Connected Tournament 概率神题

    [CF913F]Strongly Connected Tournament 题意:有n个人进行如下锦标赛: 1.所有人都和所有其他的人进行一场比赛,其中标号为i的人打赢标号为j的人(i<j)的概 ...

随机推荐

  1. jQuery1.9.1源码分析--Events模块

    var rformElems = /^(?:input|select|textarea)$/i, rkeyEvent = /^key/, rmouseEvent = /^(?:mouse|contex ...

  2. Sqli-labs less 25a

    Less-25a 不同于25关的是sql语句中对于id,没有''的包含,同时没有输出错误项,报错注入不能用.其余基本上和25示例没有差别.此处采取两种方式:延时注入和联合注入. http://127. ...

  3. 10个jQuery插件分享

    原文:http://www.shejidaren.com/10-jquery-plugins.html blur.js blur.js是一个很有意思的插件,它能实现像WIN7 AERO效果的JS插件, ...

  4. vi编辑器的常见使用技巧

    光标移动 在普通模式下, 1.按 h 向左移动光标 按 h + 数字n 可以向右移动 n个字符   比如 h + 5  就是向左移动5个字符 2.按j向下移动光标 3.按k向上移动光标 4.按 l 向 ...

  5. (转) C++ static、const和static const 以及它们的初始化

    const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. static表示的是静态的.类的静态成员函数.静态成员变量是和类相关的,而不是和类的 ...

  6. (转)c语言随机数srandom( )

    转自:http://zhidao.baidu.com/question/334364810.html调用随机数函数 rand()() 的时候, 实际得到的这个随机数并不是绝对随机的,它是以一个初始值, ...

  7. WCF分布式开发步步为赢(10):请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back).

    WCF除了支持经典的请求应答(Request-Reply)模式外,还提供了什么操作调用模式,他们有什么不同以及我们如何在开发中使用这些操作调用模式.今天本节文章里会详细介绍.WCF分布式开发步步为赢( ...

  8. node中的模块

    模块 编写稍大一点的程序时一般都会将代码模块化.在NodeJS中,一般将代码合理拆分到不同的JS文件中,每一个文件就是一个模块,而文件路径就是模块名. 在编写每个模块时,都有require.expor ...

  9. hibernate初次配置问题

    1.自动创建表结构 在hibernate.cfg.xml配置文件中修改 <property name="hibernate.hbm2ddl.auto">update&l ...

  10. lintcode:两个数组的交

    题目 返回两个数组的交 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2]. 解题 排序后,两指针找相等元素,注意要去除相同的元素 public class ...