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. 二维树状数组(HD2642)

    #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<stdio.h> #include<string. ...

  2. Struts2中在Action里面向前端页面传值的方法总结

    由于在Action中并不能直接诶访问Servlet API,但它提供了相关类ActionContext来访问HttpServletRequest.HttpSession和ServletContext, ...

  3. J2SE知识点摘记(六)

    1.        static关键字的使用 static 关键字:可以用于修饰属性,也可以用于修饰方法,还可以用于修饰类. static 修饰属性:无论一个类生成了多少个对象,所有这些对象共同使用唯 ...

  4. JVM内存分配和回收

    本文内容来自<Java编程思想(第四版)>第二章<一切都是对象>和第五章<初始化与清理>.作为一个使用了好几年的Javaer,再次看编程思想的前面章节(不要问我为什 ...

  5. JavaMail回复

    JavaMail邮件回复 http://blog.csdn.net/o_darling/article/details/17558049 http://blog.csdn.net/xiyang_199 ...

  6. Android系统Root原理初探(转)

    http://www.imooc.com/learn/126 chkconfig setup 解压update.zip这个文件,可发现它一般打包了如下这几个文件: 或者没有updates而是syste ...

  7. Nginx 介绍和安装

    Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...

  8. #include <assert.h>

    assert宏 适用于软件测试.调试.排错 被除数不能为0,assert可以用于检测被除数是否为0 #define _CRT_SECURE_NO_WARNINGS //#define NDEBUG// ...

  9. Android 它们的定义View (一)

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 非常Android入门程序员AndroidView.可能都是比較恐 ...

  10. 从头开始-01.C语言环境测试

    在Mac下编写C程序需要以下几步: 编写代码 a>编译:把C语言编译成0和1 b>工具:clang编译器 c>指令:cc -c 文件名.c      编译成功会生成一个. o目标文件 ...