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的编译配置过程,希 ...
随机推荐
- 利用PhantomJS生成网站截图
var page = require('webpage').create(); page.open('http://qq.com', function () { page.render('exampl ...
- 【CodeForces】671 C. Ultimate Weirdness of an Array
[题目]C. Ultimate Weirdness of an Array [题意]给定长度为n的正整数序列,定义一个序列的价值为max(gcd(ai,aj)),1<=i<j<=n, ...
- Attention is all you need 论文详解(转)
一.背景 自从Attention机制在提出之后,加入Attention的Seq2Seq模型在各个任务上都有了提升,所以现在的seq2seq模型指的都是结合rnn和attention的模型.传统的基于R ...
- PyCharm 自定义文件和代码模板
PyCharm提供了文件和代码模板功能,可以利用此模板来快捷新建代码或文件.比如在PyCharm中新建一个html文件,新的文件并不是空的,而是会自动填充了一些基础的必备的内容,就像这样: <! ...
- ip_rcv && ip_rcv_finish
(1) 在inet_init中注册了类型为ETH_P_IP协议的数据包的回调ip_rcv (2) 当二层数据包接收完毕,会调用netif_receive_skb根据协议进行向上层分发 (3) 类型为E ...
- ActiveMQ-如何使用JMS API?
JMS编程模型 JMS定义了Java中访问消息中间件的一组接口,主要包括ConnectionFactory.Connection.Session.Destination.MessageProducer ...
- JSP基础与提高(一).md
JSP基础 JSP的由来 1.1. 为什么有JSP规范 Servlet技术产生以后,在使用过程中存在一个很大的问题,即为了表现页面的效果而需要输出大量的HTML标签,这些标签在Servlet中表现为一 ...
- 【转+整理+答案】python315+道面试题
提示 自己整理的答案,很局限,如有需要改进的地方,或者有更好的答案,欢迎提出! [合理利用 Ctrl+F 提高查找效率] 第一部分 Python基础篇(80题) 1.为什么学习Python? # 因为 ...
- explicit浅谈
在C++中,explicit关键字主要用于防止隐式转换,用于修饰构造函数.复制构造函数. 例如有一个类: class A { public: A( int count ) : m_data( coun ...
- iOS开发:用DES对字符串加解密
参考http://www.cnblogs.com/janken/archive/2012/04/05/2432930.html,做了个小修改,实现PHP,JAVA,Objective-c加解密结果相同 ...