题目链接: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. CSS与HTML结合

    CSS与HTML结合的4中方式: 1.每个HTML标签都有style属性 2.当页面中有多个标签具有相同样式时,可定义style标签封装样式以复用 <style type=”text/css”& ...

  2. 【BZOJ3745】Norma [分治]

    Norma Time Limit: 20 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description Input 第1行,一个整数N: ...

  3. 【CodeForces】671 C. Ultimate Weirdness of an Array

    [题目]C. Ultimate Weirdness of an Array [题意]给定长度为n的正整数序列,定义一个序列的价值为max(gcd(ai,aj)),1<=i<j<=n, ...

  4. JS设计模式——4.继承(示例)

    目的 我们的目的就是编写一个用于创建和管理就地编辑域的可重用的模块化API.它是指网页上的一段普通文本被点击后就变成一个配有一些按钮的表单域,以便用户就地对这段文本进行编辑. 思路 当用户点击时 1. ...

  5. NASA: Seeing Jupiter(注视木星)

    This image of Jupiter’s southern hemisphere was captured by NASA’s Juno spacecraft on the outbound l ...

  6. docker之设置开机自启动(二)

    docker的自启动 通过sysv-rc-conf等管理 启动脚本 # docker.service #!/bin/sh sudo systemctl enable docker sudo syste ...

  7. appium===常用方法介绍,元素定位

    https://testerhome.com/topics/3711 元素定位方法: find_element_by_android_uiautomator ,使用uiautomator定位,后面参数 ...

  8. Power Profiles for Android

    http://source.android.com/devices/tech/power.html Battery usage information is derived from battery ...

  9. selenium grid应用2-多节点执行用例

    启动远程 node我们目前启动的 Hub 与 node 都是在一台主机.那么要在其它主机启动 node 必须满足以下几个要求: 1)本地 hub 主机与远程 node 主机之间可以相互 ping 通 ...

  10. u-boot启动第二阶段以及界面命令分析

    u-boot第一阶段完成了一些平台相关的硬件的配置,第一阶段所做的事情也是为第二阶段的准备,我们知道在第一阶段最后时搭建好C运行环境,之后调用了start_armboot(),那么很显然第二阶段从st ...