Rikka with Graph(联通图取边,暴力)
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
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?
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.
3
1 2
2 3
3 1
1 3
题解:题目问的是去掉边使图仍然联通。很简单的一道图论题,我竟然用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(联通图取边,暴力)的更多相关文章
- hdu 5631 Rikka with Graph(图)
n个点最少要n-1条边才能连通,可以删除一条边,最多删除2条边,然后枚举删除的1条边或2条边,用并查集判断是否连通,时间复杂度为O(n^3) 这边犯了个错误, for(int i=0;i<N;i ...
- HDU 5631 Rikka with Graph 暴力 并查集
Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...
- Rikka with Graph(hdu5631)
Rikka with Graph Accepts: 123 Submissions: 525 Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- HDU 5422 Rikka with Graph
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- poj 3177 Redundant Paths 求最少添加几条边成为双联通图: tarjan O(E)
/** problem: http://poj.org/problem?id=3177 tarjan blog: https://blog.csdn.net/reverie_mjp/article/d ...
- Tarjan 联通图 Kuangbin 带你飞 联通图题目及部分联通图题目
Tarjan算法就不说了 想学看这 https://www.byvoid.com/blog/scc-tarjan/ https://www.byvoid.com/blog/biconnect/ 下面是 ...
- [CF1051F]The Shortest Statement (LCA+最短路)(给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路)
题目:给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路 n≤100000,m≤100000,m-n≤20. 首先看到m-n≤20这条限制,我们可以想到是围绕这个20来做这道题. 即如果我们 ...
- HDU 5424——Rikka with Graph II——————【哈密顿路径】
Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 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) ...
随机推荐
- liunx下NetworkManager导致网卡不能启动
前几天在客户现场,配置一台系统为redhat 6.0的服务器,这台服务器是IBM x3755,系统是预装的.在把服务器的IP地址配置完成后,使用命令不能启动网卡.提示:弹出界面 eht0:错误:激活链 ...
- Linux突然断电后文件丢失的问题
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://yuyongid.blog.51cto.com/10626891/168504 ...
- HDU4738【杭州网赛、判桥】
刚拿到这道题时挺有思路,无奈平日里只敲过找割顶的代码,判桥的代码当时自己也没仔细敲. 当时一把泪啊,忽然感觉自己的图论才只是刚搞了个起步啊.. 题目有神坑. 就是先判是否连通,不连通直接输出0; ...
- cnn softmax regression bp求导
内容来自ufldl,代码参考自tornadomeet的cnnCost.m 1.Forward Propagation convolvedFeatures = cnnConvolve(filterDim ...
- Qt widgets
1,Qt部件Widgets--CheckWidgets,安置其他部件的Widgets,让用户选择数值的部件 选择部件---使用户能够从预定义的条目菜单中做出选择,combination QListBo ...
- 求解printf函数?
求大神解释一下下面的代码为什么答案不是1 2,而是1 0. #include <stdio.h> int ans = 0; int a() { ans = 1; return ans++; ...
- Copy from chromium-dev!
https://app.yinxiang.com/pub/gguangle0/chromium-dev 做了一些搬运工的活..............
- sql中int字段实现百分比
首先: 用col*1.00 把int字段隐式转换成decimal类型. 然后: 用 round(col,2)来截取前两个小数前的数据 最后: 用 CONVERT(FLOAT,decimalNum)来转 ...
- OCP prepare 20140628
1. null if nvl nvl2 NULLIF函数 Oracle NULLIF函数语法为NULLIF(表达式1,表达式2),如果表达式1和表达式2相等则返回空值,如果表达式1 ...
- Oracle中针对中文进行排序[Z]
在oracle 9i之前,对中文的排序,是默认按2进制编码来进行排序的. 9i时增加了几种新的选择: 按中文拼音进行排序:SCHINESE_PINYIN_M 按中文部首进行排序:SCHINESE_RA ...