题目链接: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. kle 日志收集系统维护之清理索引及索引优化脚本

    logstash每天往es建好索引,按天生成,就目前的需求,需要清理不需要的数据,以保证最新日志的速度展示,哈哈,瞎搞了这个脚本,路过的大神批评. #!/usr/bin/env python # co ...

  2. 【acmm】一道简单的数学题

    emm卡常 我本来写成了这个样子: #include<bits/stdc++.h> using namespace std; typedef long long LL; ; struct ...

  3. Xcode变量概览-summary

    问题描述 在Xcode中断点调试时,鼠标停留在变量上,就能看到变量的信息.但对于自定义对象,通常Xcode提供的直接信息非常有限,像这样 想要了解这个对象具体的内容,需要展开左边的箭头 当开发者想要知 ...

  4. laravel后台返回ajax数据

    后台模式: $array = array('msg'=>'添加失败!','status'=>'false'); return json_encode($array); 前台显示: $.aj ...

  5. css3背景色过渡

    <!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...

  6. bzoj 2303 并查集

    首先如果没有限制的话,我们可以直接求出答案,假设对于n*m的矩阵,我们最上方一行和左方的一列随意确定,那么首先这写确定的状态肯定是不会不合法的,因为我们可以调整剩下的01状态来使得这一行一列的状态合法 ...

  7. E - Travel Cards CodeForces - 847K (思维)

    题目链接:https://cn.vjudge.net/contest/272855#problem/E 题目大意:给你n,a,b,k,f.n代表有n次旅行计划,然后a代表一次单程旅行的车费,b代表从下 ...

  8. WebAPI使用Swagger生成接口文档

    开发工具:VS2017 版本15.7.1 新建项目,选择空模板,下面只勾选WebAPI 配置Web.config <system.webServer> 节点改为 <system.we ...

  9. 网络管理 SNMP基础知识

    SNMP Agent快速开发   网友:SmileWolf 发布于: 2007.08.02 16:06 (共有条评论) 查看评论 | 我要评论                   摘自  http:/ ...

  10. poj 2337(单向欧拉路的判断以及输出)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11648   Accepted: 3036 Descrip ...