【HDOJ4635】【Tarjan缩点+思维】【经典】
http://acm.hdu.edu.cn/showproblem.php?pid=4635
Strongly connected
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3821 Accepted Submission(s): 1510
A simple directed graph is a directed graph having no multiple edges or graph loops.
A strongly connected digraph is a directed graph in which it is possible to reach any node starting from any other node by traversing edges in the direction(s) in which they point.
Then T cases follow, each case starts of two numbers N and M, 1<=N<=100000, 1<=M<=100000, representing the number of nodes and the number of edges, then M lines follow. Each line contains two integers x and y, means that there is a edge from x to y.
If the original graph is strongly connected, just output -1.
3 3
1 2
2 3
3 1
3 3
1 2
2 3
1 3
6 6
1 2
2 3
3 1
4 5
5 6
6 4
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int maxn=;//边的最大值
const int maxn1=;//顶点最大值
struct edge{
int from;
int to;
int next;
}EDGE[maxn];
vector<int>vc[maxn1];
int head[maxn1],dfn[maxn1],vis[maxn1],low[maxn1],col[maxn1],out[maxn1],in[maxn1],en[maxn1],stk[maxn1];//各个变量的意义可参照上篇博客
int edge_cnt=,tot1=,tot2=,scc_cnt=,tot0=;
int n,m;
void add(int x,int y)
{
EDGE[edge_cnt].from=x;
EDGE[edge_cnt].to=y;
EDGE[edge_cnt].next=head[x];
head[x]=edge_cnt++;
}
void Tarjan(int u)
{
low[u]=dfn[u]=++tot1;//注意tot1的初值必须是1【因为dfn必须为正数】,所以这里使用++tot1而不用tot1++;
vis[u]=;
stk[++tot2]=u;
for(int i = head[u]; i != - ; i = EDGE[i].next)
{
if(!dfn[EDGE[i].to]){
Tarjan(EDGE[i].to);
low[u]=min(low[u],low[EDGE[i].to]);
}
else if(vis[EDGE[i].to]){
low[u]=min(low[u],low[EDGE[i].to]);
}
}
if(low[u]==dfn[u]){
int xx;
scc_cnt++;//注意scc_cnt也是从1开始的,因为要染色,区别于为染色的0
do{
xx=stk[tot2--];
vc[scc_cnt].push_back(xx);
col[xx]=scc_cnt;
vis[xx]=;
}while(xx!=u);
}
}
void INIT()
{
for(int i = ; i < maxn1 ; i++)
vc[i].clear();
edge_cnt=,tot1=,tot2=,scc_cnt=,tot0=;
memset(head,-,sizeof(head));
memset(stk,,sizeof(stk));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(col,,sizeof(col));
}
void suodian()//缩点
{
for(int i = ; i < edge_cnt ; i++)
{
if(col[EDGE[i].from]!=col[EDGE[i].to])
{
in[col[EDGE[i].to]]++;//缩点
out[col[EDGE[i].from]]++;
}
}
}
int main()
{
int t;
scanf("%d",&t);
int case1=;
while(t--)
{
INIT();
scanf("%d%d",&n,&m);
int M=m;
while(M--)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
for(int i = ; i <= n ; i++)
{
if(!dfn[i]){
Tarjan(i);
}
} printf("Case %d: ",case1++);
if(scc_cnt==)
{
printf("-1\n");
}
else
{
suodian();
int minn=-;
for(int i = ; i <= scc_cnt ; i++)
{
if(in[i]==||out[i]==)
{
int asd=vc[i].size();
if(minn==-||minn>asd)
{
minn=asd;
}
}
}
cout << (n-minn)*(n-minn-)+minn*(minn-)+minn*(n-minn)-m<<endl;
}
}
return ;
}
【HDOJ4635】【Tarjan缩点+思维】【经典】的更多相关文章
- POJ1236:Network of Schools (思维+Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24880 Accepted: 99 ...
- hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- King's Quest —— POJ1904(ZOJ2470)Tarjan缩点
King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...
- 【BZOJ-2438】杀人游戏 Tarjan + 缩点 + 概率
2438: [中山市选2011]杀人游戏 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1638 Solved: 433[Submit][Statu ...
- 【BZOJ-1924】所驼门王的宝藏 Tarjan缩点(+拓扑排序) + 拓扑图DP
1924: [Sdoi2010]所驼门王的宝藏 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 787 Solved: 318[Submit][Stat ...
- 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点
1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1685 Solved: 724[Submit] ...
- BZOJ 1051 受欢迎的牛(Tarjan缩点)
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4573 Solved: 2428 [Submit][S ...
- HDU4612+Tarjan缩点+BFS求树的直径
tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...
随机推荐
- webpack分离打包css和less
github仓库:https://github.com/llcMite/webpack.git 为什么要分离打包? 答:刚开始学webpack的时候就很郁闷,明明没几个文件,打包出来体积特 ...
- linux:ssh远程调用tomcat脚本时候出错
我们都知道,使用ssh在另一台机子执行一个ssh文件的语句是酱紫的 ssh root@1.9.7.56 "chmod 777 /opt/script/tomcatStop.sh ; sh / ...
- maven项目配置findbugs插件 使用git钩子控制代码的提交
maven项目配置findbugs插件对代码进行静态检测 当发现代码有bug时,就不让用户commit代码到远程仓库里 没有bug时才可以commit到远程仓库中 (1)新建maven项目 ,配置fi ...
- OPENWRT安装配置指南之 17.01.4 LEDE
简介 这个东西,需要看简介的就不要看下去了. 下面已刚刷进去,路由IP地址为192.168.1.1为例开始配置. 浏览器访问192.168.1.1,无密码. 一:配置上网 不管你是什么方式上网,请根据 ...
- PC/FORTH 编辑程序
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- core net 实现post 跟get
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...
- Java 利用poi生成excel表格
所需jar包,如下所示 写一个excel工具类 ExcelUtils .java import java.lang.reflect.Field; import java.util.Iterator; ...
- ASP.NET使用文件上传控件上传图片
ASPX代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default. ...
- Alpha冲刺1
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/9948330.html 作业博客:https://edu.cnblogs.com/campus/ ...
- NSHashTable NSPointerArray
NSHashTable和NSMapTable能够对持有的对象做strong或weak存储,弱持有weak引用对象,当weak对象释放后会自动从表中移除 http://blog.csdn.net ...