hdu5483Nux Walpurgis

来源:互联网 发布:windows浏览器下载 编辑:程序博客网 时间:2024/06/06 18:49

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

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

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

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

/*****************************************Author      :Crazy_AC(JamesQi)Time        :2015File 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_backtypedef 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