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 ;
}

最新文章

  1. 我心中的核心组件(可插拔的AOP)~大话开篇及目录
  2. Spark on Yarn:java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost\\db_instance_name:1433;databaseName=db_name
  3. php 网页 301 跳转
  4. poj 3259 Wormholes(最短路 Bellman)
  5. C++ notes for beginners
  6. ENC28J60 + M430G2553,用uip搭建http服务器,解决“在XP系统下可以访问,在Win7下不能访问”的问题
  7. 让 asp.net mvc 支持 带有+ _ 等特殊字符的路由
  8. Android 手机应用开发经验 之 通过Socket(TCP/IP)与PC通讯
  9. 读书笔记 effective c++ Item 15 在资源管理类中提供对原生(raw)资源的访问
  10. openstack配置
  11. BZOJ_1408_[Noi2002]Robot_数学
  12. python3 练手实例7 斐波那契数列
  13. Color Schema 配色随笔
  14. Netty实现简单UDP服务器
  15. 一款易搭建,运行快的Git服务器:Gitea安装教程
  16. 反射简化switch语句
  17. 实现A星算法
  18. Flume 在有赞大数据的实践
  19. upsource初探
  20. [已解决]Can&#39;t update: no tracked branch

热门文章

  1. if __FILE__ == $0 end
  2. 最小化安装centos后ifconfig看不到eth0
  3. strongSwan大坑一直重启(ubuntu)
  4. mysql利用binlog恢复数据详细例子
  5. 自动发表QQ空间说说
  6. for..in...时,注意hasOwnProperty验证
  7. (62)zabbix客户端自动注册
  8. (22)zabbix触发器依赖关系详解
  9. PWA介绍
  10. cache控制器取值从TCM/CACHE/FLASH