Strongly connected 挺简单的tarjan
题意:给你一个连通图,问你最多加多少条边,还能保证该图不是强连通图。
对整个图求强连通分量,然后对图缩点,记录一下缩点之后每隔点包含的原来的点的个数,找出最少的那个点,然后对这个点建成完全图,对另外的所有点建成完全图。然后+两个点建边-所有原来的遍就好了。
链接: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的更多相关文章
- HDU 4635 Strongly connected (Tarjan+一点数学分析)
Strongly connected Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- PTA Strongly Connected Components
Write a program to find the strongly connected components in a digraph. Format of functions: void St ...
- algorithm@ Strongly Connected Component
Strongly Connected Components A directed graph is strongly connected if there is a path between all ...
- Strongly connected(hdu4635(强连通分量))
/* http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/ ...
- HDU4625:Strongly connected(思维+强连通分量)
Strongly connected Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 4635 Strongly connected (2013多校4 1004 有向图的强连通分量)
Strongly connected Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 4635 Strongly connected(强连通)经典
Strongly connected Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- cf475B Strongly Connected City
B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- 【CF913F】Strongly Connected Tournament 概率神题
[CF913F]Strongly Connected Tournament 题意:有n个人进行如下锦标赛: 1.所有人都和所有其他的人进行一场比赛,其中标号为i的人打赢标号为j的人(i<j)的概 ...
随机推荐
- POJ 1548 Robots (Dilworth)
Robots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3621 Accepted: 1643 Description Yo ...
- Winform设置相关
>> Winform查找根目录 1) AppDomain.CurrentDomain.BaseDirectory 地址为: d:\MyProject\Bin\ Application. ...
- 用 VIPER 构建 iOS 应用架构(1)
[编者按]本篇文章由 Jeff Gilbert 和 Conrad Stoll 共同编写,通过构建一个基础示例应用,深入了解 VIPER,并从视图.交互器等多个部件理清 VIPER 的整体布局及思路.通 ...
- HDU 1671 Phone List(字符处理)
题目 用字典树可以过,可是我写的字典树一直各种错误,,, 所以,我用了别的更简便的方法.. //去你妹的一直有问题的字典树!!! ////字典树,树的根是空的 // ////#include<i ...
- Wamp Mysql错误消息 语言设置
Wamp Mysql错误消息 语言设置 http://my.oschina.net/wandershi/blog/264347 打开my.ini 找到 [wampmysqld] port = 33 ...
- 《Thinking in C++》学习笔记(二)【第三章】
第三章 C++中的C 3.4.4 指针简介 ‘&’运算符:只要在标识符前加上‘&’,就会得出标识符的地址. C和C++有一个专门存放地址的变量类型.这个变量类型叫做指针(pointer ...
- sqoop导入hdfs上的数据到oracle
/opt/sqoop-/bin/sqoop export --table mytablename --connect jdbc:oracle:thin:@**.**.**.**:***:dbasena ...
- C#调用脚本语言(三)-- IronJS 与 IronLua 简单方法性能比较
1. 测试环境 1.1. 硬件环境 CPU:intel Core i7-740QM 内存:8GDDR3 Memory 1.2. 系统 系统:Windows 8 Enterprise 开发工具:Vs ...
- (1)搭建opencv-android环境
前言: 本文目的是指导在windows平台搭建一个opencv for android 的开发环境,作者参考了很多网上的教程,本文所使用的各种软件.插件都是截止到写这篇文章的最新版本,作者在实际搭建环 ...
- spring_150803_service
实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...