Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

怎么会WA呢,哪位好心人帮俺看看

Posted by tafengren at 2008-08-21 10:19:30 on Problem 2485
#include<stdio.h>
#include<set>
#include<algorithm>
using namespace std;


struct Edge{
	int u,v,w;
} ;

class UFset{
	int *rank;
	int *parent;
	int n;
public:
	UFset(int n){
		int i;
		this->n=n;
		rank=new int[n + 1];
		parent=new int [n + 1];
		for(i=0;i<=n;++i){//从1开始,表示从节点1开始
			parent[i]=-1;
			rank[i]=1;
		}
	}
	~UFset(){
		delete []parent;
		delete []rank;
	}
	int Find(int e){
		int i=e;
		while(parent[i]!=-1)
			i=parent[i];
		int j=e;
		while(j!=i){
			int pj=parent[j];
			parent[j]=i;
			j=pj;
		}
		return i;
	}
	void Union(int a,int b){
		if(a==b) return;
		if(rank[a]>rank[b])
			parent[b]=a;
		else
			parent[a]=b;
		if(rank[a]==rank[b])
			rank[b]++;
	}
		
		
};


class Graph{
public:
	int n,m;
	Edge edge[130000];
	void kruskal();
};
bool cmp(const Edge& e1,const Edge& e2){
	return e1.w<e2.w;
}
void Graph::kruskal(){
	sort(edge+1,edge+m+1,cmp);
	UFset ufset(n);
	int min=0;
	int k=1;
	int count=1;
	for(int i=1;i<=m;++i){
		int p1=ufset.Find(edge[i].u);
		int p2=ufset.Find(edge[i].v);
		if(p1!=p2){
			ufset.Union(p1,p2);
			++k;
			++count;
		}
		if(k==n)
			break;
	}
	printf("%d\n",edge[count-1].w);
}
Graph g;




int G[501][501];
int main(){
	int t,i,j,N;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&N);
		for(i=1;i<=N;++i)
			for(j=1;j<=N;++j){
				scanf("%d",&G[i][j]);
			}
		g.n=N;
		int m=1;
		for(int j=0;j<13000;++j)
			g.edge[j].w=(1<<15);
		for(i=2;i<=N;++i)
			for(j=1;j<i;++j){
				g.edge[m].w=G[i][j];
				g.edge[m].u=i;
				g.edge[m].v=j;
				m++;
			}
		g.m=m-1;
		g.kruskal();
	
	}
	return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator