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 ...
随机推荐
- 在django模板中添加jquery
路径 /project_name /app_name /templates /index.html /project_name setting.py /static /js jquery.js 导入方 ...
- 【技巧】Java工程中的Debug信息分级输出接口
也许本文的标题你们没咋看懂.但是,本文将带大家领略输出调试的威力. 灵感来源 说到灵感,其实是源于笔者在修复服务器的ssh故障时的一个发现. 这个学期初,同袍(容我来一波广告产品页面,同袍官网)原服务 ...
- [Android FrameWork 6.0源码学习] View的重绘过程之WindowManager的addView方法
博客首页:http://www.cnblogs.com/kezhuang/p/关于Activity的contentView的构建过程,我在我的博客中已经分析过了,不了解的可以去看一下<[Andr ...
- webapi 使用Autofac 开发经历
2018/4/6 号 早上五点..被手机震动吵醒. 之后直接打开电脑,打算再加强下我自己的webapi这套东西. 虽然三年的工作经验接触了N多框架和各种风格的开发方式,但是让我自己来搞一套实在不会搞, ...
- Win7下安装composer, 并使用其安装smarty
安装composer需要开启PHP openssl扩展. 1) 先查看PHP是否开启了openssl扩展 键盘win+r 输出cmd, 可以看到Dos窗口, 然后执行php -m (需要添加PHP环境 ...
- Web Api 接收图片
public async Task<HttpResponseMessage> Upload() { if (!Request.Content.IsMimeMultipartContent( ...
- Python内置函数(54)——callable
英文文档: callable(object) Return True if the object argument appears callable, False if not. If this re ...
- 你考虑清楚了吗就决定用 Bootstrap ?
近年来,在前端项目中, Bootstrap 已经成为了一个非常受欢迎的工具. Bootstrap 的确有很多优点,然而,如果你的团队中恰好有一个专职的前端工程师.那我推荐你们不要使用 Bootstra ...
- 新概念英语(1-49)At the butcher's
新概念英语(1-49)At the butcher's What does Mr. Bird like? A:Do you want any meat today, Mrs. Bird? B:Yes, ...
- GIT入门笔记(19)GIT 小结
1.add和commit为什么Git添加文件需要add,commit一共两步呢?因为commit可以一次提交很多文件,所以你可以多次add不同的文件,比如:$ git add file1.txt$ g ...