Rikka with Graph

 Accepts: 123
 Submissions: 525
 Time Limit: 2000/1000 MS (Java/Others)
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:

给出一张 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)的更多相关文章

  1. hdu5634 BestCoder Round #73 (div.1)

    Rikka with Phi  Accepts: 5  Submissions: 66  Time Limit: 16000/8000 MS (Java/Others)  Memory Limit: ...

  2. hdu5630 BestCoder Round #73 (div.2)

    Rikka with Chess  Accepts: 393  Submissions: 548  Time Limit: 2000/1000 MS (Java/Others)  Memory Lim ...

  3. BestCoder Round #73 (div.2)

    1001 Rikka with Chess ans = n / 2 + m / 2 1002 Rikka with Graph 题意:n + 1条边,问减去至少一条使剩下的图连通的方案数. 分析:原来 ...

  4. BestCoder Round #73 (div.2)(hdu 5630)

    Rikka with Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. BestCoder Round #73 (div.2)1002/hdoj5631

    题意: 给出一张 nnn 个点 n+1n+1n+1 条边的无向图,你可以选择一些边(至少一条)删除. 分析: 一张n个点图,至少n-1条边才能保证联通 所以可以知道每次可以删去1条边或者两条边 一开始 ...

  6. 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 ( ...

  7. 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 ...

  8. BestCoder Round #11 (Div. 2) 题解

    HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. hdu5635 BestCoder Round #74 (div.2)

    LCP Array  Accepts: 131  Submissions: 1352  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: 13 ...

随机推荐

  1. Network in Network

     论文要点: 用更有效的非线性函数逼近器(MLP,multilayer perceptron)代替 GLM 以增强局部模型的抽象能力.抽象能力指的模型中特征是对于同一概念的变体的不变形. 使用 gl ...

  2. c++ 中lambda

    C++ 11中的Lambda表达式用于定义并创建匿名的函数对象,以简化编程工作. 1.Lambda表达式完整的声明格式如下: [capture list] (params list) mutable  ...

  3. Three.js three.js Uncaught TypeError: Cannot read property 'getExtension' of null

    在调试Three.js执行加载幕布的时候,突然爆出这个错误three.js Uncaught TypeError: Cannot read property 'getExtension' of nul ...

  4. 学习phalcon框架按照官网手册搭建第一个项目注册功能

    中文手册官网:http://phalcon.ipanta.com/1.3/tutorial.html#bootstrap 官网提供http://www.tutorial.com项目源码github地址 ...

  5. Spring知识点回顾(08)spring aware

    Spring知识点回顾(08)spring aware BeanNameAware 获得容器中的bean名称 BeanFactoryAware 获得当前的bean factory Applicatio ...

  6. Spring Security入门(3-3)Spring Security 手工配置并注入 authenticationProvider 和 异常信息传递

    特别注意的是 这样就能保证抛出UsernameNotFoundException时,前台显示出错信息: 另外,ps:

  7. ios8新的api

    self.navigationController.hidesBarsOnSwipe=YES; 滚动时隐藏导航栏

  8. Qt编译oci教程

    Qt编译OCI教程 上图oci.dll 不是oci.lib 我很奇怪网上大部分教程都是写的oci.lib,其实大家可以去oracle目录找一下这个文件,看看是不是在这个目录,我找了一下没发现.而lib ...

  9. Java:import com.sun.awt.AWTUtilities;报错

    参考网址:http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-req ...

  10. [js]关于call()和apply()的理解

    call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向. 因为 JavaScript 的函数存在「定义时上下文」和 ...