HDU 4635 Strongly connected (Tarjan+一点数学分析)
Strongly connected
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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.
Input
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.
Output
If the original graph is strongly connected, just output -1.
Sample Input
3
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
Sample Output
Case 1: -1
Case 2: 1
Case 3: 15
Source
#include <bits/stdc++.h>
using namespace std;
const int N=+;
int dfn[N],low[N],team[N],num[N],in[N],out[N];
bool instack[N];
int n,T,m,index,team_num;
vector<int> mp[N];
stack<int> S;
void Tarjan(int u)
{
low[u]=dfn[u]=++index;
S.push(u);
instack[u]=;
for(int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if (!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if (instack[v]) low[u]=min(low[u],dfn[v]);
}
if (dfn[u]==low[u])
{
team_num++;
while()
{
int v=S.top(); S.pop();
instack[v]=;
team[v]=team_num; // v点是第几组
num[team_num]++; //第i组的点个数
if (v==u) break;
}
}
}
void dfs()
{
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(instack,,sizeof(instack));
memset(team,,sizeof(team));
memset(num,,sizeof(num));
team_num=;
index=;
for(int i=;i<=n;i++)
if (!dfn[i]) Tarjan(i);
}
int main()
{
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) mp[i].clear();
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x].push_back(y);
}
dfs(); //缩点,求出各个组的点数
printf("Case %d: ",cas);
for(int i=;i<=team_num;i++) in[i]=out[i]=;
for(int i=;i<=n;i++)
for(int j=;j<mp[i].size();j++)
{
if (team[i]!=team[mp[i][j]])
{
in[team[mp[i][j]]]++;
out[team[i]]++;
}
}
//统计入度数和出度数
int minn=;
for(int i=;i<=team_num;i++)
if (in[i]== || out[i]==) minn=min(minn,num[i]);
//求出入度=0或者出度=0的点数最小的组
if (team_num==) printf("-1\n");
else printf("%lld\n",(long long)n*n-n-(long long)minn*(n-minn)-m);
}
return ;
}
HDU 4635 Strongly connected (Tarjan+一点数学分析)的更多相关文章
- hdu 4635 Strongly connected(Tarjan)
做完后,看了解题报告,思路是一样的.我就直接粘过来吧 最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到X的边,那么要使得边数尽可能的多,则X部肯定是一个完全图,Y部也是,同时X部 ...
- HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】
Strongly connected Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 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 强连通缩点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...
- HDU 4635 Strongly connected(强连通)经典
Strongly connected Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 4635 Strongly connected (tarjan)
题意:给一个n个顶点m条弧的简单有向图(无环无重边),求最多能够加入多少条弧使得加入后的有向图仍为简单有向图且不是一个强连通图.假设给的简单有向图本来就是强连通图,那么输出-1. 分析: 1.用tar ...
- hdu 4635 Strongly connected
http://acm.hdu.edu.cn/showproblem.php?pid=4635 我们把缩点后的新图(实际编码中可以不建新图 只是为了概念上好理解)中的每一个点都赋一个值 表示是由多少个点 ...
- HDU 4635 Strongly connected ——(强连通分量)
好久没写tarjan了,写起来有点手生,还好1A了- -. 题意:给定一个有向图,问最多添加多少条边,让它依然不是强连通图. 分析:不妨考虑最大时候的临界状态(即再添加一条边就是强连通图的状态),假设 ...
- hdu 4635 Strongly connected(强连通)
考强连通缩点,算模板题吧,比赛的时候又想多了,大概是不自信吧,才开始认真搞图论,把题目想复杂了. 题意就是给你任意图,保证是simple directed graph,问最多加多少条边能使图仍然是si ...
随机推荐
- Python3基础 os listdir curdir pardir 查看工作目录及其上一级目录的所有文件名
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- jz2440-linux3.4.2-kernel移植【学习笔记】【原创】
平台:jz2440 作者:庄泽彬(欢迎转载,请注明作者) 说明:韦东山二期视频学习笔记 交叉编译工具:arm-linux-gcc (GCC)4.3.2 linux:linu3.4.2 PC环境:ubu ...
- Linux deepin 中Jetbrain Idea等软件中文显示异常
解决方案:安装常用的中文字体 # 文鼎宋体[推荐] sudo apt install fonts-arphic-uming # 文鼎楷体[推荐] sudo apt install fonts-arph ...
- YOLO(Darknet官方)训练分类器
目录 1. 分类数据准备 2. Darknet配置 3. Darknet命令使用 4. cifar-10 使用示例 1. 分类数据准备 需要的文件列表: 1. train.list : 训练的图片的绝 ...
- Goroutines和Channels(五)
Channels也可以用于将多个goroutine连接在一起,一个Channel的输出作为下一个Channel的输入.这种串联的Channels就是所谓的管道(pipeline).下面的程序用两个ch ...
- [STL][C++]LIST
参考:http://blog.csdn.net/whz_zb/article/details/6831817 list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素.在STL中,list ...
- RabbitMQ入门_09_TTL
参考资料:https://www.rabbitmq.com/ttl.html A. 为队列设置消息TTL TTL 是 Time-To-Live 的缩写,指的是存活时间.RabbitMQ 可以为每一个队 ...
- ScriptableObjec 的简单使用
1.ScriptableObject的创建(一): using System.Collections; using System.Collections.Generic; using UnityEng ...
- 滑动窗口解决Substring Search Problem
2018-07-18 11:19:19 一.Minimum Window Substring 问题描述: 问题求解: public String minWindow(String s, String ...
- 013 - 关于GC root: Native Stack | MAT分析
Question: I have some third library code that I run and after some time I run into OutOfMemoryEr ...