hdu5483Nux Walpurgis

题目意思就是求一张图上的最小生成树必须经过的边的最少条数。

因为不含有重边,而且一颗确定的生成树要换边不换值的话就是同值的边进行变换。值不同必然边的数目也不同了。。。

同值的边不能能替换的等价条件是这条边在这群同值边都成的图中是桥,结合kruskal算法。

主要看能网上的题解才搞出来的。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
/**********************Point*****************************/
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B){
	return Vector(A.x + B.x,A.y + B.y);
}
Vector operator - (Vector A,Vector B){//向量减法
	return Vector(A.x - B.x,A.y - B.y);
}
Vector operator * (Vector A,double p){//向量数乘
	return Vector(A.x * p,A.y * p);
}
Vector operator / (Vector A,double p){//向量除实数
	return Vector(A.x / p,A.y / p);
}
int dcmp(double x){//精度正负、0的判断
	if (fabs(x) < eps) return 0;
	return x < 0?-1:1;
}
bool operator < (const Point& A,const Point& B){//小于符号的重载
	return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A,const Point& B){//点重的判断
	return dcmp(A.x - B.x) == 0&& dcmp(A.y - B.y) == 0;
}
double Dot(Vector A,Vector B){//向量的点乘
	return A.x * B.x + A.y * B.y;
}
double Length(Vector A){//向量的模
	return sqrt(Dot(A,A));
}
double Angle(Vector A,Vector B){//向量的夹角
	return acos(Dot(A,B) / Length(A) / Length(B));
}
double Cross(Vector A,Vector B){//向量的叉积
	return A.x * B.y - A.y * B.x;
}
double Area2(Point A,Point B,Point C){//三角形面积
	return Cross(B - A,C - A);
}
Vector Rotate(Vector A,double rad){//向量的旋转
	return Vector(A.x * cos(rad) - A.y * sin(rad),A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A){//法向量
	int L = Length(A);
	return Vector(-A.y / L,A.x / L);
}
double DistanceToLine(Point p,Point A,Point B){//p到直线AB的距离
	Vector v1 = B - A,v2 = p - A;
	return fabs(Cross(v1,v2)) / Length(v1);
}
double DistanceToSegment(Point p,Point A,Point B){//p到线段AB的距离
	if (A == B) return Length(p - A);
	Vector v1 = B - A, v2 = p - A,v3 = p - B;
	if (dcmp(Dot(v1,v2) < 0)) return Length(v2);
	else if (dcmp(Dot(v1,v3)) > 0) return Length(v3);
	else return DistanceToLine(p,A,B);
}
bool SegmentProperIntersection(Point A1,Point A2,Point B1,Point B2){//线段相交
	double c1 = Cross(A2 - A1,B1 - A1),c2 = Cross(A2 - A1,B2 - A1);
	double c3 = Cross(B2 - B1,A1 - B1),c4 = Cross(B2 - B1,A2 - B1);
	return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}
int t;
int n;
const int N = 3010;
vector<ii> G[N];//
int head[N], pnt[N * N / 2], nxt[N * N / 2], ecnt;
int F[N];
int Find(int x) {
	return F[x] == x ?x:F[x] = Find(F[x]);
}
void addedge(int u,int v) {
	pnt[ecnt] = v, nxt[ecnt] = head[u], head[u] = ecnt++;
}
int dfn[N], low[N];
int Times;
int cnt;
void Tarjan(int u,int fa) {
	dfn[u] = low[u] = ++Times;
	bool first = true;
	for (int i = head[u];~i;i = nxt[i]) {
		int v = pnt[i];
		if (v == fa && first) {
			first = false;
			continue;
		}
		if (dfn[v] == -1) {
			Tarjan(v, u);
			low[u] = min(low[u], low[v]);
			if (low[v] > dfn[u]) cnt++;
		}else if (dfn[v] < low[u]) low[u] = dfn[v];
	}
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	scanf("%d",&t);
	while (t--) {
		cnt = 0;
		for (int i = 1;i <= 3000;++i) 
			G[i].clear();
		scanf("%d",&n);
		for (int i = 1;i <= n;++i)
			F[i] = i;
		int top = 0;
		for (int i = 1, x;i < n;++i) {
			for (int j = 1;j <= n - i;++j) {
				scanf("%d",&x);
				if (x > top) top = x;
				G[x].push_back(ii(i, j + i));
			}
		}
		for (int i = 1;i <= top;++i) {
			memset(head, -1,sizeof head);
			ecnt = 0;
			for (int j = (int)G[i].size() - 1;j >= 0;--j) {
				int u = Find(G[i][j].first);
				int v = Find(G[i][j].second);
				if (u != v) {
					addedge(u, v);
					addedge(v, u);
				}
			}
			memset(dfn, -1,sizeof dfn);
			Times = 0;
			for (int j = 1;j <= n;++j)
				if (dfn[j] == -1) Tarjan(j, -1);
			for (int j = (int)G[i].size() - 1;j >= 0;--j) {
				int u = Find(G[i][j].first);
				int v = Find(G[i][j].second);
				if (u != v) F[u] = v;
			}
		}
		printf("%d\n", cnt);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值