Nux Walpurgis

Time Limit: 8000ms
Memory Limit: 131072KB

This problem will be judged on HDU. Original ID: 5483
64-bit integer IO format: %I64d      Java class name: Main

Given a weighted undirected graph, how many edges must be on the minimum spanning tree of this graph?
 

Input

The first line of the input is a integer T, meaning that there are T test cases.

Every test cases begin with a integer n ,which is the number of vertexes of this graph.

Then n−1 lines follow, the ith line contain n−i integers, the jth number w in this line represents the weight between vertex i and vertex i+j.

1≤T≤20.

1≤n,w≤3,000.

Output

For every test case output the number of edges must be on the minimum spanning tree of this graph.
 

Sample Input

2
3
1 1
1
4
2 2 3
2 2
3

Sample Output

0
1

Source

 
解题:题目还是挺难想的,求图的最小生成树的必要边的数量,学习的是某位大神的解法
 #include <bits/stdc++.h>
using namespace std;
using PII = pair<int,int>;
const int maxn = ;
int head[maxn],uf[maxn],dfn[maxn],low[maxn],clk,tot,ret;
struct arc {
int to,next;
arc(int x = ,int z = -) {
to = x;
next = z;
}
} e[];
vector<PII>g[maxn];
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
int Find(int x) {
if(x != uf[x]) uf[x] = Find(uf[x]);
return uf[x];
}
void tarjan(int u,int fa) {
dfn[u] = low[u] = ++clk;
bool flag = true;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to == fa && flag) {
flag = false;
continue;
}
if(!dfn[e[i].to]) {
tarjan(e[i].to,u);
low[u] = min(low[u],low[e[i].to]);
if(low[e[i].to] > dfn[u]) ++ret;
} else low[u] = min(low[u],dfn[e[i].to]);
}
}
int main() {
int kase,n;
scanf("%d",&kase);
while(kase--) {
scanf("%d",&n);
for(int i = ret = ; i < maxn; ++i) {
uf[i] = i;
g[i].clear();
}
for(int i = ,tmp; i < n; ++i) {
for(int j = i + ; j <= n; ++j) {
scanf("%d",&tmp);
g[tmp].push_back(PII(i,j));
}
}
for(int i = ; i < maxn; ++i) {
memset(dfn,,sizeof dfn);
memset(head,-,sizeof head);
tot = ;
for(int j = g[i].size()-; j >= ; --j) {
int u = Find(g[i][j].first);
int v = Find(g[i][j].second);
if(u != v) {
add(u,v);
add(v,u);
}
}
for(int j = ; j <= n; ++j)
if(!dfn[j]) tarjan(j,-);
for(int j = g[i].size()-; j >= ; --j) {
int u = Find(g[i][j].first);
int v = Find(g[i][j].second);
if(u != v) uf[u] = v;
}
}
printf("%d\n",ret);
}
return ;
}

HDU 5483 Nux Walpurgis的更多相关文章

  1. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  2. 在CentOS或RHEL上安装Nux Dextop仓库

    介绍 Nux Dextop是类似CentOS.RHEL.ScientificLinux的第三方RPM仓库(比如:Ardour,Shutter等等).目前,Nux Dextop对CentOS/RHEL ...

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. JavaScript实现的9大排序算法

    笔试面试经常涉及各种算法,本文简要介绍常用的一些算法,并用JavaScript实现. 1.插入排序 1)算法简介 插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法.它的工作原 ...

  2. Mongo学习

    几种可能存在的实体类型 public class AAA{ public List<Common> CommonList{get;set;} } public class BBB{ pub ...

  3. Android 仿微信朋友圈添加图片

    github地址(欢迎下载Demo) https://github.com/zhouxu88/WXCircleAddPic 老习惯,先上图,着急用的朋友,直接带走Demo,先拿来用吧,毕竟老板催的紧, ...

  4. Actionbar Demo

    源码下载:http://download.csdn.net/detail/bx276626237/8874119

  5. 明白这十个故事-->你也就参悟了人生 .

    1.断箭 不相信自己的意志,永远也做不成将军.  春秋战国时代,一位父亲和他的儿子出征打仗.父亲已做了将军,儿子还只是马前卒.又一阵号角吹响,战鼓雷鸣了,父亲庄严地托起一个箭囊,其中插着一只箭.父亲郑 ...

  6. vba 两个表 信息合体一个表格

    1 把Excel某一区域的内容读入到数组中: Dim strArray() As Variant Dim wk_data As Variant Set wk_data = Sheets("S ...

  7. access处理重复创建表的方法。

    第一种,使用MSysObjects表查找表名为当前创建表的名字的内容,相当于普通查询,但是access数据库有一个安全问题,就是有时候一开始是没有权限去调这些系统表的,这时可以再2007的access ...

  8. python在d盘,robotframework引入seleniumlibrary报错

    在*** setting*** 中引入库   Library  SeleniumLibrary  报错 unknown seleniumlibrary library ,try to use quic ...

  9. Windows MinGW 64-bit boost 踩坑

    >g++ -Wall -shared -g -DBUILD_DLL main.cpp -ID:\gcc\boost\include\boost-1_69 -LD:\gcc\boost\lib - ...

  10. easyui树节点拖拽排序的存储过程

    easyui树的拖拽排序功能 easyui树中有拖拽功能 树结构如下: 一个行政区域对应一个单位,一个单位对应多个部门,每个部门下有相关人员,功能要求: (1)行政区域没有子节点,点击text加载部门 ...