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) ...
随机推荐
- RenderPartial: No overload for method 'Write' takes 0 arguments
如下方法调用RenderPartial: 报“No overload for method 'Write' takes 0 arguments”的错误: @if (@Model != null &am ...
- elasticsearch 索引 类型 id
zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat logstash_indexer01.conf input { redi ...
- H.264视频的RTP荷载格式
Status of This Memo This document specifies an Internet standards track protocol for the Internet ...
- 杭电oj1062 Text Reverse
Tips:使用一个临时数组c[1000] ,将输入的数据一边复制一边处理,碰到空格时就将前面的字符反向输出即可 #include<stdio.h> #include<string.h ...
- Igor In the Museum(搜搜搜151515151515******************************************************1515151515151515151515)
D. Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 用C#实现生成PDF文档
using System; using System.IO; using System.Text; using System.Collections; namespace PDFGenerator { ...
- android实现点击短链接进入应用 并获得整个连接的内容
例如:我收到一个短信是一个链接:http://www.qq.com/a/b/?, 当我点击以后我要实现的场景是:点击----->有两种情况可供选择:手机中有此应用,跳转到应用:反之,则进入网页. ...
- U3D学习使用笔记(四)
1.Unity3D项目实战笔记(5):延时功能的几种实现 http://www.xifarm.com/invoke/ 2.动画运行出屏幕范围时消失 Animation CullingType 3.No ...
- Oracle学习之start with...connect by子句的用法
转自:http://www.blogjava.net/xzclog/archive/2010/03/05/314642.html,多谢博主分享 Oracle中start with…connect by ...
- PHP基础示例:商品信息管理系统v1.1
实现目标:使用php和mysql写一个商品信息管理系统,并带有购物车功能 一.创建数据库和表 1.创建数据库和表:demodb 2.创建表格:goods 字段:商品编号,商品名称,商品类型,商品图片, ...