UVALive 6044(双连通分量的应用)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34902
思路:首先是双连通缩点,然后就是搜索一下,搜索时要跳过连通分量的点的个数>=2的点,最后的答案是n*(n-1)/2.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
#define MAXN 11111
#define MAXM 444444 struct Edge{
int v,next;
}edge[MAXM]; int n,m,NE,cnt,_count;
int head[MAXN]; void Insert(int u,int v)
{
edge[NE].v=v;
edge[NE].next=head[u];
head[u]=NE++;
} int low[MAXN],dfn[MAXN];
int color[MAXN];
bool mark[MAXN];
stack<int>S;
void Tarjan(int u,int father)
{
int flag=;
low[u]=dfn[u]=++cnt;
mark[u]=true;
S.push(u);
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(v==father&&!flag){ flag=;continue; }
if(dfn[v]==){
Tarjan(v,u);
low[u]=min(low[u],low[v]);
}else if(mark[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
_count++;
int x=S.top();
if(x==u)S.pop();
else {
do{
x=S.top();
S.pop();
mark[x]=false;
color[x]=_count;
}while(x!=u);
}
}
} int ans;
void dfs(int u,int father)
{
color[u]=;
_count++;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(v==father)continue;
if(color[v])continue;
dfs(v,u);
}
} int main()
{
int _case,u,v,t=;
scanf("%d",&_case);
while(_case--){
scanf("%d%d",&n,&m);
NE=;
memset(head,-,sizeof(head));
while(m--){
scanf("%d%d",&u,&v);
Insert(u,v);
Insert(v,u);
}
cnt=_count=;
memset(dfn,,sizeof(dfn));
memset(color,,sizeof(color));
for(int i=;i<=n;i++){
if(dfn[i]==)Tarjan(i,-);
}
ans=;
for(int i=;i<=n;i++){
if(color[i]==){
_count=;
dfs(i,-);
ans+=_count*(_count-)/;
}
}
printf("Case #%d: %d\n",t++,ans);
}
return ;
}
UVALive 6044(双连通分量的应用)的更多相关文章
- UVALive - 5135 - Mining Your Own Business(双连通分量+思维)
Problem UVALive - 5135 - Mining Your Own Business Time Limit: 5000 mSec Problem Description John D ...
- 训练指南 UVALive - 5135 (双连通分量)
layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...
- UVALive 5135 Mining Your Own Business 双连通分量 2011final
题意:n条隧道由一些点连接而成,其中每条隧道链接两个连接点.任意两个连接点之间最多只有一条隧道.任务就是在这些连接点中,安装尽量少的太平井和逃生装置,使得不管哪个连接点倒塌,工人都能从其他太平井逃脱, ...
- UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)
由于互相憎恨的骑士不能相邻,把可以相邻的骑士连上无向边,会议要求是奇数,问题就是求不在任意一个简单奇圈上的结点个数. 如果不是二分图,一定存在一个奇圈,同一个双连通分量中其它点一定可以加入奇圈.很明显 ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- 【Codefoces487E/UOJ#30】Tourists Tarjan 点双连通分量 + 树链剖分
E. Tourists time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard inpu ...
- 【BZOJ-2730】矿场搭建 Tarjan 双连通分量
2730: [HNOI2012]矿场搭建 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1602 Solved: 751[Submit][Statu ...
- hihoCoder 1184 连通性二·边的双连通分量
#1184 : 连通性二·边的双连通分量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在基本的网络搭建完成后,学校为了方便管理还需要对所有的服务器进行编组,网络所的老 ...
- HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...
随机推荐
- TWaver版3D化学元素周期表
非常早就有人做3D网页版的化学元素周期表了.酷炫效果和新奇技巧一度被众多粉丝奉为神明,争相研究和效仿.甚至有人放弃一切扑向这颗蜡烛.不由总是想到那个OPPO广告女主角拽拽的歧视道:"辞职去旅 ...
- android中RecycleView分页原生代码封装,无任何第三方代
概述 RecycleView分页加载封装,简单方便,功能齐全 详细 代码下载:http://www.demodashi.com/demo/13283.html 一.场景: 在项目开发中经常使用到列表集 ...
- 推荐算法——非负矩阵分解(NMF)
一.矩阵分解回想 在博文推荐算法--基于矩阵分解的推荐算法中,提到了将用户-商品矩阵进行分解.从而实现对未打分项进行打分. 矩阵分解是指将一个矩阵分解成两个或者多个矩阵的乘积.对于上述的用户-商品矩阵 ...
- Python爬虫实战案例:爬取爱奇艺VIP视频
一.实战背景 爱奇艺的VIP视频只有会员能看,普通用户只能看前6分钟.比如加勒比海盗5的URL:http://www.iqiyi.com/v_19rr7qhfg0.html#vfrm=19-9-0-1 ...
- python --中文相关问题
往文件中写入中文,再从文件中读出,范例1: #coding:utf- with open('data.log','w') as f: a=u'中文' b = a.encode('gbk') f.wri ...
- 使用xftp连接VirtualBox中的centos6.5
首先要在windows上安装xftp软件,这个是傻瓜式操作就不说了 安装完毕之后,在centos上查看是否装了xftpd服务. [root@centos Desktop]# rpm -qa | gre ...
- ORC 资料Mark
1 OCR开源代码网址汇总 1.1 OCRE(OCR Easy), http://lem.eui.upm.es/ocre.html 1.2 Clara OCR,http://directory.fs ...
- mysql在linux7下systemd的相关配置
---- # Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. # # This program ...
- XCODE中配置使用boost
1. 开发平台:os x 2. boost安装目录: 3. xcode中配置: 4. 测试
- 【Android】15.0 第15章 广播和通知—本章示例主界面
分类:C#.Android.VS2015: 创建日期:2016-02-28 一.简介 广播(Broadcast):其功能类似于收音机的广播,你只要调到那个台(只要在接收的类中注册了要接收的广播),就能 ...