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. 代码中看见一共8个变量参数{SEO,0,0,0,0,0,0,0} 解读!{Top,0,0,0,0,0,0,Top}{Nav,0,0,0,0,0,0,Nav}

    代码中看见{SEO,0,0,0,0,0,0,0}{Top,0,0,0,0,0,0,Top}{Nav,0,0,0,0,0,0,Nav}解读! 举个例子: {GetNew,977,0,23,500,0,0 ...

  2. rm和mv和dirname和查看文件的命令

    rm -f -i -r ############################ mv -f -i -u 比较新旧 ########################### basename   获取文 ...

  3. vba 时间

    Sub tt1() Dim d1, d2 As Date d1 = #//# d2 = #//# Debug.Print "相隔" & (d2 - d1) & &q ...

  4. 人人必知的10个 jQuery 小技巧

    原文地址:http://info.9iphp.com/10-jquery-tips-everyone-should-know/ 人人必知的10个 jQuery 小技巧   收集的10个 jQuery ...

  5. mvc工作总结

    MVC的页面跳转方式(放在一般类): filterContext.Result = new RedirectResult("controller/action"); filterC ...

  6. Codeforces Round #290 (Div. 2) _B找矩形环的三种写法

    http://codeforces.com/contest/510/status/B 题目大意 给一个n*m  找有没有相同字母连起来的矩形串 第一种并查集 瞎搞一下 第一次的时候把val开成字符串了 ...

  7. Data truncation: Data too long for column 'id' at row 1

    Caused by: java.sql.BatchUpdateException: Data truncation: Data too long for column 'titleimg' at ro ...

  8. 树形dp——Tree2cycle

    一.问题描述(题目链接) 给你一棵树,删除或添加一条边的费用都是1,问使它变成一个环的最小费用. 二.解题思路 回溯法,然后回溯的时候的当前节点度数>2(如果是成环的话肯定就是2或者小于2)就把 ...

  9. 用例重试机制rerunfailures

    安装 rerunfailures插件 pip install pytest-rerunfailures 使用: pytest --reruns  重试次数 如:pytest --reruns  2 重 ...

  10. 自动发表QQ空间说说

    require("gb2312toutf8") local http = require "socket.http" local surl = "ht ...