UVALive 6907 Body Building
题目链接:https://vjudge.net/problem/UVALive-6907
题意: 给出一张图,判断这张图中有多少个哑铃,哑铃判断的条件是,对于一个连通图:如果找到一条边连接这两个点的个数相同的完全图,那么这个联通图是哑铃状的。输出哑铃的个数。
举几个例子:

A、B、C都是哑铃状的图;
思路:很简单的一道题,首先我们对一个联通块进行树上DFS,说白了是就是那个TarJan树,求出每个以某个节点为根的树的大小,这样DFS只有桥连接的两个点的size 是对的,碰巧,我们也是只需要这两个点的size。顺便我们来求出这个来联通块的边的个数。
那么我们再进行Tarjan找到桥,即u - - > v,我们只需要判断张图是否是完全图的就行了。、
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
int T, N, M;
int ans = ;
struct Edge
{
int to, next;
Edge(int to = , int next = ): to(to), next(next) {}
} E[];
int head[maxn], tot;
void initedge()
{
for(int i = ; i <= N; i++) head[i] = -;
tot = ;
}
void addedge(int u, int v)
{
E[tot] = Edge(v, head[u]);
head[u] = tot++;
}
int pre[maxn], low[maxn], dfsclock;
int sz[maxn], edg;
int DFS(int u, int fa)
{
sz[u] = ;
int ret = ;
for(int k = head[u]; ~k; k = E[k].next)
{
ret += ;
int v = E[k].to;
if(v == fa || sz[v]) continue;
ret += DFS(v, u);
sz[u] += sz[v];
}
return ret;
}
bool Tarjan(int u, int fa)
{
pre[u] = low[u] = ++dfsclock;
for(int k = head[u]; ~k; k = E[k].next)
{
int v = E[k].to;
if(v == fa) continue;
if(!pre[v])
{
if(Tarjan(v, u)) return true;
low[u] = min(low[u], low[v]);
if(low[v] > pre[u])
{
int sum = sz[v] * (sz[v] - ) + ;
if(sum == edg) return true;
}
}
else
low[u] = min(low[u], pre[v]);
}
return false;
}
void TarjanInit()
{
for(int i = ; i <= N; i++)
pre[i] = sz[i] = ;
dfsclock = ans = ;
for(int i = ; i <= N; i++)
{
if(!pre[i])
{
edg = DFS(i, i);
edg /= ;
if(Tarjan(i, i)) ans++;
}
}
}
int main ()
{
int ic = ;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &N, &M);
initedge();
for(int i = ; i <= M; i++)
{
int u, v;
scanf("%d %d", &u, &v);
addedge(u, v);
addedge(v, u);
}
TarjanInit();
printf("Case #%d: %d\n", ++ic, ans);
}
return ;
}
UVALive 6907 Body Building的更多相关文章
- UVALive 6907 Body Building tarjan
Body Building 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8 ...
- UvaLive 5026 Building Roads
传送门 Time Limit: 3000MS Description There is a magic planet in the space. There is a magical country ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive 5066 Fire Drill BFS+背包
H - Fire Drill Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
- Building the Testing Pipeline
This essay is a part of my knowledge sharing session slides which are shared for development and qua ...
- BZOJ 4742: [Usaco2016 Dec]Team Building
4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 21 Solved: 16[Su ...
- Building OpenCASCADE on Debian
Building OpenCASCADE on Debian eryar@163.com Abstract. When you are familiar with OpenCASCADE on Win ...
- Building third-party products of OpenCascade
Building third-party products of OpenCascade eryar@163.com Available distributives of third-party pr ...
- Building OpenCascade on Windows with Visual Studio
Building OpenCascade on Windows with Visual Studio eryar@163.com 摘要Abstract:详细说明OpenCascade的编译配置过程,希 ...
随机推荐
- ASP.Net Cache(缓存)—ASP.NET细枝末节(2)
概述 1.意义 把数据放到Cache中,在指定的时间内,可以直接从Cache中获取,避免对数据库等的压力. 2.做法 设置: HttpRuntime.Cache.Insert(CacheKey, ob ...
- 去除UITableView多余的seperator
UIView *v = [[UIView alloc] initWithFrame:CGRectZero]; [tableView setTableFooterView:v]; [v release] ...
- 【洛谷 P3227】 [HNOI2013]切糕(最小割)
题目链接 每层每个位置向下一层这个位置连边,流量为下一层这个位置的\(f\),源点向第一层连,流量第一层每个位置的费用,最后一层向汇点连,流量\(INF\). 这样就得到了\(P*Q\)条链,不考虑\ ...
- 免杀后门(四)之shellter注入绕过
文中提及的部分技术可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使用 Shellter 是一款动态 shellcode 注入工具.利用它,我们可以将shell注入到其他的可执行程序上,从而躲避安 ...
- python并发编程之multiprocessing进程(二)
python的multiprocessing模块是用来创建多进程的,下面对multiprocessing总结一下使用记录. 系列文章 python并发编程之threading线程(一) python并 ...
- python设计模式之常用创建模式总结(二)
前言 设计模式的创建模式终极目标是如何使用最少量最少需要修改的代码,传递最少的参数,消耗系统最少的资源创建可用的类的实例对象. 系列文章 python设计模式之单例模式(一) python设计模式之常 ...
- uboot makefile构建分析-续
前言 这篇博文是 uboot makefile构建分析的续篇,继续分析uboot构建u-boot.bin的过程 构建u-boot.bin过程分析 makefile一开始,就是确定链接脚本.在构建ubo ...
- 用Centos7搭建小微企业Samba文件共享服务器【转】
转自 用Centos7搭建小微企业Samba文件共享服务器 - 今日头条(www.toutiao.com)http://www.toutiao.com/i6436937837660078593/ 最近 ...
- eclipse 常见问题之字体更改、添加注释模板
有些同学可能会和我有一样的困扰,每次想要更改字体大小.背景颜色等,都需要百度一下才知道怎么去做...不知道有没有这种情况的孩子,反正我经常遇到,老是记不住,今天写下来,顺带自己忘记的时候可以查看一下. ...
- Mysql自带profiling性能分析工具使用分享
1. show variables like '%profiling%';(查看profiling信息) 2. set profiling=1;(开启profiling) 3. 执行S ...