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

我是如此的SB……对自己无语了

Posted by eche at 2012-03-01 11:16:18 on Problem 1797
觉得题目不难,无向图,直接二分+并查集做了,用例也过了。。。然后WA,然后考虑有向图,用prim过了一次用例,WA。。。然后觉得自己对题目里的n点理解错了,不是一个点,而是每个点都要能到达的最大重,WA。。。然后看各种解题报告,确定还是无向图,并且感觉本来的理解和算法没错,再交了一遍,WA。。。直到最后怀疑是不是多用例情况下出错。。。果然,拿用例交两遍的结果是:
Scenario #2:
4

Scenario #2:
4

……原来把用例个数当用例序号输出了,败了……这个题的教训就是以后遇到多用例的题一定要拿多用例来测一下,以检测每个用例的初始化等问题。

最后附二分+并查的代码,200多ms

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int Scenario;
int VN,ArcN;
int Start[1000005],End[1000005],Weight[1000005];

int ufs[1005];

int find(int x)
{
	if(ufs[x]<0)
		return x;
	else
		return ufs[x] = find(ufs[x]);
}

void setUnion(int r1,int r2)
{
	if(ufs[r1]<ufs[r2])
	{
		ufs[r1] += ufs[r2];
		ufs[r2] = r1;
	}
	else
	{		
		ufs[r2] += ufs[r1];
		ufs[r1] = r2;
	}
}

void addConnection(int elem1,int elem2)
{
	int r1 = find(elem1),r2 = find(elem2);
	if( r1 != r2 )
		setUnion(r1,r2);
}

bool connected(int elem1,int elem2)
{
	int r1 = find(elem1),r2 = find(elem2);
	return r1 == r2;
}

bool Good(int k)
{
	int j;
	memset(ufs,-1,sizeof(ufs));
	for(j=1;j<=ArcN;++j)
	{
		if(Weight[j]>=k)
			addConnection(Start[j],End[j]);
	}
	if( !connected(1,VN) )
		return false;
	else
		return true;
}

int main()
{
	int i,j;
	int b,e,m;
	scanf("%d",&Scenario);
	for(i = 1;i<=Scenario;++i)
	{
		scanf("%d%d",&VN,&ArcN);
		for(j=1;j<=ArcN;++j)
			scanf("%d%d%d",&Start[j],&End[j],&Weight[j]);

		b = 1;
		e = 1000000;
		while( b != e )
		{
			m = (b+e)/2;
			if( Good(m) )
				b = m + 1;
			else
				e = m;
		}
		if( Good(b) )
			printf("Scenario #%d:\n%d\n\n",i,b);
		else
			printf("Scenario #%d:\n%d\n\n",i,b-1);

	}
	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