题目链接: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的更多相关文章

  1. UVALive 6907 Body Building tarjan

    Body Building 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8 ...

  2. UvaLive 5026 Building Roads

    传送门 Time Limit: 3000MS Description There is a magic planet in the space. There is a magical country ...

  3. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  4. UVALive 5066 Fire Drill BFS+背包

    H - Fire Drill Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

  5. Building the Testing Pipeline

    This essay is a part of my knowledge sharing session slides which are shared for development and qua ...

  6. BZOJ 4742: [Usaco2016 Dec]Team Building

    4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 16[Su ...

  7. Building OpenCASCADE on Debian

    Building OpenCASCADE on Debian eryar@163.com Abstract. When you are familiar with OpenCASCADE on Win ...

  8. Building third-party products of OpenCascade

    Building third-party products of OpenCascade eryar@163.com Available distributives of third-party pr ...

  9. Building OpenCascade on Windows with Visual Studio

    Building OpenCascade on Windows with Visual Studio eryar@163.com 摘要Abstract:详细说明OpenCascade的编译配置过程,希 ...

随机推荐

  1. 【BZOJ】1023: [SHOI2008]cactus仙人掌图 静态仙人掌(DFS树)

    [题意]给定仙人掌图(每条边至多在一个简单环上),求直径(最长的点对最短路径).n<=50000,m<=10^7. [算法]DFS树处理仙人掌 [题解]参考:仙人掌相关问题的处理方法(未完 ...

  2. 去除UITableView多余的seperator

    UIView *v = [[UIView alloc] initWithFrame:CGRectZero]; [tableView setTableFooterView:v]; [v release] ...

  3. NYOJ 231 Apple Tree (树状数组)

    题目链接 描述 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in t ...

  4. Distance Gym - 102028I (思维)

    题目链接:https://cn.vjudge.net/problem/Gym-102028I 具体思路:首先我们选定左边界和右边界.然后每一次按照左边一个,第二次右边一个的规律往上就可以了 具体原因: ...

  5. hdu 5319 Painter(杭电多校赛第三场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5319 Painter Time Limit: 2000/1000 MS (Java/Others)   ...

  6. hdfs的datanode工作原理

    datanode的作用: (1)提供真实文件数据的存储服务. (2)文件块(block):最基本的存储单位.对于文件内容而言,一个文件的长度大小是size,那么从文件的0偏移开始,按照固定的大小,顺序 ...

  7. Machine Learning系列--EM算法理解与推导

    EM算法,全称Expectation Maximization Algorithm,译作最大期望化算法或期望最大算法,是机器学习十大算法之一,吴军博士在<数学之美>书中称其为“上帝视角”算 ...

  8. 使用常见的网络命令查看当前网络状态——Mac OS X篇

    转载自:http://blog.csdn.net/zkh90644/article/details/50539948 操作系统拥有一套通用的实用程序来查明本地主机的有线或者无线链路状态和IP的连接情况 ...

  9. 双系统卸载linux和装双系统的方法

    卸载linux系统: 因为本人装的是windows和Ubuntu,所以引导程序在linux系统里,linux系统可以引导windows系统,而Windows不能引导linux,所以需要修改引导程序,使 ...

  10. WordPress用户角色与用户能力/权限

    WordPress用户角色(user roles)是WP或者其它插件增加的,可以让网站管理员(网站管理员也是一种角色)来方便的管理用户的权限/能力(Capabilities,一般情况下,一种角色不止有 ...