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 ...
随机推荐
- vim配置强悍来袭
vim 这个关键字,我不想再过多的解释,相信看到这里的同仁,对vim都有十七八分的理解,如果你还不知道vim是什么,自己找个黑屋子... 废话不多说,今天在这里主要说vim的,不带插件的配置,也就 ...
- scrapy 博客爬取
item.py import scrapy class FulongpjtItem(scrapy.Item): # define the fields for your item here like: ...
- C实现单链表
typedef int DataType; typedef struct ListNode { DataType data; struct ListNode* next; }ListNode; //初 ...
- Spring MVC Restful Put方法无法获取参数值
Spring MVC Restful 无法通过@ReqeustParam获取参数值 原因是Tomcat只支持POST/GET获取参数值,对于PUT这些方法需要通过HttpPutFormContentF ...
- org.hibernate.hibernate.connection.release_mode
org.hibernate.connection包的主要封装了通过JDBC来连接数据库的操作,用户可以以数据源的方式,或者通过特定数据库驱动的方式,甚至是自己定义连接类的方式来完成数据库的连接操作,包 ...
- 2017 国庆湖南 Day5
期望得分:76+80+30=186 实际得分:72+10+0=82 先看第一问: 本题不是求方案数,所以我们不关心 选的数是什么以及的选的顺序 只关心选了某个数后,对当前gcd的影响 预处理 cnt[ ...
- SpringMVC之HandlerMapping的使用
上篇博客在了解SpringMVC的工作流程时留了一些疑问,今天先学习下HandlerMapping,在HandlerMapping中可以通过HandlerExecutionChain getHandl ...
- 织梦cms网上复制图片不可用的解决方法
背景描述: 织梦cms采集图片集时, 需要使用织梦cms提供的"网上复制图片"的功能, 好像我这里这个功能一直不可用, 今天下定决心研究了下源代码并进行了适当修改, 将我的修改提供 ...
- .Net Core MongoDB 简单操作。
一:MongoDB 简单操作类.这里引用了MongoDB.Driver. using MongoDB.Bson; using MongoDB.Driver; using System; using S ...
- 20170222==(MODBUS读取多个寄存器)
MODBUS读取多个寄存器(功能码04) 为了简单我这里只用4个寄存器,当让你也可以用125个寄存器,但是最多也只能用125个寄存器的.每个寄存器有上面的表知道为一个字的大小即2个字节或者叫16比特位 ...