hdu5631 BestCoder Round #73 (div.2)
Rikka with Graph
众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 给出一张 nn 个点 n+1n+1 条边的无向图,你可以选择一些边(至少一条)删除。 现在勇太想知道有多少种方案使得删除之后图依然联通。 当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?
第一行一个整数表示数据组数 T(T \leq 30)T(T≤30)。 每组数据的第一行是一个整数 n(n \leq 100)n(n≤100)。 接下来 n+1n+1 行每行两个整数 u,vu,v 表示图中的一条边。
对每组数据输出一行一个整数表示答案。
1
3
1 2
2 3
3 1
1 3
9
/*
BestCoder Round #73 (div.2)
hdu5631 Rikka with Graph 连通图 bfs or 并查集
思路:
总共有n+1条边,所以我们最多只需枚举两条边然后判断图是否是连通的即可
hhh-2016-02-25 11:27:16
*/
#include <functional>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn = 105;
struct node
{
int from,to,next;
} edge[maxn*2]; int vis[maxn*2];
int used[maxn];
int head[maxn*2];
int tot,n;
int ans;
void addedge(int u,int v)
{
edge[tot].from = u;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} bool bfs()
{
memset(used,0,sizeof(used));
queue<int> q;
q.push(1);
used[1] = 1;
while(!q.empty())
{
int t = q.front();
q.pop();
for(int i = head[t];i != -1;i = edge[i].next)
{
int v = edge[i].to;
if(vis[i]) continue;
if(used[v]) continue;
used[v] = 1;
q.push(v);
}
}
for(int i =1;i <= n;i++)
{
if(!used[i])
return false;
}
return true;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int x,y;
scanf("%d",&n);
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
tot = 0;
for(int i =1; i <= n+1; i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
ans = 0; for(int i = 0; i <= n; i++)
{
for(int j = i+1; j <= n; j++)
{
vis[i*2] = vis[i*2+1] = 1;
vis[j*2] = vis[j*2+1] = 1;
if(bfs())
ans++;
vis[i*2] = vis[i*2+1] = 0;
vis[j*2] = vis[j*2+1] = 0;
}
}
//cout << ans <<endl;
for(int i = 0; i <= n; i++)
{
vis[i*2] = vis[i*2+1] = 1;
if(bfs())
ans ++;
vis[i*2] = vis[i*2+1] = 0;
}
printf("%d\n",ans);
}
return 0;
}
hdu5631 BestCoder Round #73 (div.2)的更多相关文章
- hdu5634 BestCoder Round #73 (div.1)
Rikka with Phi Accepts: 5 Submissions: 66 Time Limit: 16000/8000 MS (Java/Others) Memory Limit: ...
- hdu5630 BestCoder Round #73 (div.2)
Rikka with Chess Accepts: 393 Submissions: 548 Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- BestCoder Round #73 (div.2)
1001 Rikka with Chess ans = n / 2 + m / 2 1002 Rikka with Graph 题意:n + 1条边,问减去至少一条使剩下的图连通的方案数. 分析:原来 ...
- BestCoder Round #73 (div.2)(hdu 5630)
Rikka with Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BestCoder Round #73 (div.2)1002/hdoj5631
题意: 给出一张 nnn 个点 n+1n+1n+1 条边的无向图,你可以选择一些边(至少一条)删除. 分析: 一张n个点图,至少n-1条边才能保证联通 所以可以知道每次可以删去1条边或者两条边 一开始 ...
- BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)
Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- BestCoder Round #68 (div.2) tree(hdu 5606)
tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- BestCoder Round #11 (Div. 2) 题解
HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu5635 BestCoder Round #74 (div.2)
LCP Array Accepts: 131 Submissions: 1352 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 13 ...
随机推荐
- python的PEP8 代码风格指南
PEP8 代码风格指南 这篇文章原文实际上来自于这里:https://www.python.org/dev/peps/pep-0008/ 知识点 代码排版 字符串引号 表达式和语句中的空格 注释 版本 ...
- 个人技术博客(alpha)
APP的权限校验不同于web网页端,web一般使用session记录用户的状态信息,而app则使用token令牌来记录用户信息.有这样一个场景,系统的数据量达到千万级,需要几台服务器部署,当一个用户在 ...
- Codechef March Challenge 2014——The Street
The Street Problem Code: STREETTA https://www.codechef.com/problems/STREETTA Submit Tweet All submis ...
- 动手写IL到Lua的翻译器——准备
文章里的代码粘过来的时候格式有点问题,原因是一开始文章是在订阅号上写的(gamedev101,文末有二维码),不知道为啥贴过来就没了格式,还要手动删行号,就没搞了. 介绍下问题背景: 小说君正在参与的 ...
- 08-TypeScript中的类
类的概念通常是在后端开发中实现的思想,比如C#.C++或Java,传统的JavaScript开发通过使用原型模式来模拟类的功能.在TypeScript中,天生就是支持类 的,可以让前端的开发更加具有面 ...
- javascript递归函数
递归函数:是指函数直接或间接调用函数本身,则称该函数为递归函数. 这句话理解起来并不难,从概念上出发,给出以下的例子: function foo(){ console.log("函数 foo ...
- Python内置函数(36)——reversed
英文文档: reversed(seq) Return a reverse iterator. seq must be an object which has a __reversed__() meth ...
- Python/ selectors模块及队列
Python/selectors模块及队列 selectors模块是可以实现IO多路复用机制: 它具有根据平台选出最佳的IO多路机制,比如在win的系统上他默认的是select模式而在linux上它默 ...
- ASC学习笔记
TCL:(Tool Command Language), a computer programming languagecharm++:基于C++的面向对象的并行编程语言.Charm++ is a p ...
- Java基础语法<六> 数组 Arrays
笔记整理 来源于<Java核心技术卷 I > <Java编程思想> 允许数组长度为0 new element[0] 数组长度为0与null不同 1. 数组拷贝 允许将一 ...