【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 ...
随机推荐
- php对于url提交数据的获取办法
$url = Request::getUri();//获取当前的url $arr = parse_url($url); //$arr_query = convertUrlQuery($arr['que ...
- laravel 查询指定字段的值
$this->model->where('id',$id)->value('user');
- C# [IPA]IOS In App Purchase(内购)验证(asp.net 版本)
之前没有做过IOS 内购服务器验证这块,所以找了不少参考资料,网上大多php和java版本,然后自己搞了一个C#版本,希望能给大家一些参考,下面步入正题 在客户端向苹果购买成功之后,我们需要进行二次验 ...
- 字符与字符串3——char 的大小
字符变量占用内存的大小,也就是char类型声明的变量,这个变量占多少字节. 一字节 char c = 'A'; printf("%d,%d\n", sizeof(c),sizeof ...
- C++标准库头文件名字和C语言头文件名字的区别
1.C++版本的C标准库头文件,一般是cname,而C语言头文件一般是name.h 2.命名为cname的头文件中定义的名字都是从std中来的,而如果是name.h则不是这样的. 3.与是用name. ...
- :适配器模式:Adapter
#ifndef __ADAPTER_H__ #define __ADAPTER_H__ #include <iostream> using namespace std; class Duc ...
- Android system :灯光系统_HAL_lights
一.android灯光系统框架: Java: frameworks/base/services/core/java/com/android/server/lights/LightsService.ja ...
- 【重大更新】DevExpress WinForms v18.2新版亮点(七)
买 DevExpress Universal Subscription 免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...
- 获取表单内元素组装成对象类型,方便datagrid的load取参数
/** * 获取表单数据,并将其转换为对象 */ function getFormObj(formId) { var formObj = {}; var inputs = $('#'+formId). ...
- android 应用程序中执行Linux 命令
ADB 无线调试命令son = "setprop service.adb.tcp.port 5555\n" + "stop adbd\n" + "st ...