Rikka with Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 190    Accepted Submission(s): 78

Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
Yuta has a non-direct graph with n vertices and n+1 edges. Rikka can choose some of the edges (at least one) and delete them from the graph.
Yuta wants to know the number of the ways to choose the edges in order to make the remaining graph connected.
It is too difficult for Rikka. Can you help her?
 
Input
The first line contains a number T(T≤30)——The number of the testcases.
For each testcase, the first line contains a number n(n≤100).
Then n+1 lines follow. Each line contains two numbers u,v , which means there is an edge between u and v.
 
Output
For each testcase, print a single number.
 
Sample Input
1
3
1 2
2 3
3 1
1 3
 
Sample Output
9
 

题解:题目问的是去掉边使图仍然联通。很简单的一道图论题,我竟然用prime错了半天。。。最后还是改了krustra,让找有多少中取法,直接暴力取边

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x,y) scanf("%lf%lf",&x,&y)
#define P_ printf(" ")
typedef long long LL;
const int MAXN=;
int pre[MAXN];
int s[MAXN],e[MAXN];
int N,ans;
int find(int r){
return pre[r]= pre[r]==r?r:find(pre[r]);
}
int check(int a,int b){
for(int i=;i<=N;i++)pre[i]=i;
for(int i=;i<=N;i++){
if(i==a||i==b)continue;
int f1=find(s[i]),f2=find(e[i]);
//printf("%d %d\n",f1,f2);
if(f1!=f2)pre[f1]=f2;
}
int cnt=;
for(int i=;i<=N;i++){
if(pre[i]==i)cnt++;
// if(cnt>1)printf("%d\n",cnt);
if(cnt>)return ;
}
return ;
}
int main(){
int T;
SI(T);
while(T--){
SI(N);
for(int i=;i<=N;i++)
SI(s[i]),SI(e[i]);
int ans=;
for(int i=;i<=N;i++)
for(int j=i;j<=N;j++){//相等代表的是取一条边。
ans+=check(i,j);
}
printf("%d\n",ans);
}
return ;
}

Rikka with Graph(联通图取边,暴力)的更多相关文章

  1. hdu 5631 Rikka with Graph(图)

    n个点最少要n-1条边才能连通,可以删除一条边,最多删除2条边,然后枚举删除的1条边或2条边,用并查集判断是否连通,时间复杂度为O(n^3) 这边犯了个错误, for(int i=0;i<N;i ...

  2. HDU 5631 Rikka with Graph 暴力 并查集

    Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...

  3. Rikka with Graph(hdu5631)

    Rikka with Graph  Accepts: 123  Submissions: 525  Time Limit: 2000/1000 MS (Java/Others)  Memory Lim ...

  4. HDU 5422 Rikka with Graph

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

  5. poj 3177 Redundant Paths 求最少添加几条边成为双联通图: tarjan O(E)

    /** problem: http://poj.org/problem?id=3177 tarjan blog: https://blog.csdn.net/reverie_mjp/article/d ...

  6. Tarjan 联通图 Kuangbin 带你飞 联通图题目及部分联通图题目

    Tarjan算法就不说了 想学看这 https://www.byvoid.com/blog/scc-tarjan/ https://www.byvoid.com/blog/biconnect/ 下面是 ...

  7. [CF1051F]The Shortest Statement (LCA+最短路)(给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路)

    题目:给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路 n≤100000,m≤100000,m-n≤20. 首先看到m-n≤20这条限制,我们可以想到是围绕这个20来做这道题. 即如果我们 ...

  8. HDU 5424——Rikka with Graph II——————【哈密顿路径】

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

  9. HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5

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

随机推荐

  1. Swift笔记2

    1.元组类型 let cat =(age:4,weight:2,cocle:"black",beauty :true) if(cat.beauty){ printf("我 ...

  2. 转发:使用sql命令查询视图中所有引用的基础表

    转自:使用sql命令查询视图中所有引用的基础表 使用sql命令查询视图中所有引用的基础表 之前有写过如何利用sql查询视图中所有引用的表发现这个方法并不能查出视图中所有的基础表,如果视图中有嵌套视图就 ...

  3. RenderPartial: No overload for method 'Write' takes 0 arguments

    如下方法调用RenderPartial: 报“No overload for method 'Write' takes 0 arguments”的错误: @if (@Model != null &am ...

  4. knockout 与checkbox联动

    knockout 通过teplate实现简单的代码实现复杂的操作绑定checkbox,代码如下自我感觉很赞!!! 前台HTml <ul data-bind="template: { n ...

  5. SQL Server 统计信息的创建与更新

    前期准备: 普通表.临时表:它两会有统计信息. 表变量:           不会有统计信息. ---------------------------------------------------- ...

  6. 把给定的字符串解析为Date对象

    把给定的字符串解析为Date对象: /** * <pre> * 把给定的字符串解析为Date对象 * </pre> * * @param str 要进行解析的字符串 * @pa ...

  7. Advanced Fruits(好题,LCS的模拟)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. SQL整理2

    数据库的概念 结构化查询语言:structured query language 简称:SQL 数据库管理系统:database management system 简称:DBMS 数据库管理员:da ...

  9. nide.js(二)文件I/O

    文件I/O fs模块的基本用法 node.js中提供一个名为fs的模块来支持I/O操作,fs模块的文件I/O是对标准POSIX函数的简单封装. 1.writeFile函数的基本用法 文件I/O,写入是 ...

  10. android入门——BroadCast(1)

    使用广播要定义一个广播接收类,如 package com.example.wkp.broadcast; import android.content.BroadcastReceiver; import ...