SZU 2015 Winter Training Day#3

7 篇文章 0 订阅
3 篇文章 0 订阅

Problem(A70):Locked Boxes

Judge Info

MemoryLimit: 32768KB

Case TimeLimit: 10000MS

Time Limit:10000MS

Judger:Number Only Judger

Description

Poseidonhas N boxes with lock. What's inside the box? (hum..., think by yourself) Eachbox can either be opened with its corresponding key or smashed. Poseidon hasput the keys in some of the boxes. He remembers which key has been placed inwhich box. Poseidon wants to access to all of the boxes. However, he wants todestroy as few of them as possible. Please write a Program to help Poseidon todetermine how many boxes have to be smashed.

Input

The inputwill contains multiple test cases. The first line of the input is a singleinteger T (1<= T<=30) which is the number of test cases. T test casesfollow.

Each testcase contains a single integer N (1 <= N <= 1,000,000) - this is thenumber of boxes owned by Poseidon. The boxes (as well as their correspondingkeys) are numbered from 1 to N. Next, there are N lines: the i+1st linecontains a single integer - the number of the box which the ith key has beenplaced in.

Output

For eachinput test case, you are to output a single integer - the minimal number ofboxes to be smashed in order to access to all of the boxes.

Sample Input

2

 

4

2

1

2

4

 

4

2

1

2

4

Sample Output

2

2


程序:
#include <iostream>
using namespace std;

const int MAXN=1000000;

int key[MAXN+1];

int find(int);

int main()
{
	//memset(key,0,sizeof(key));

	int t,i,n,sum,temp;

	scanf("%d",&t);

	while (t--)
	{
		sum=0;

		scanf("%d",&n);
		for (i=1;i<=n;i++)
			key[i]=i;
		
		for (i=1;i<=n;i++)
		{
			scanf("%d",&temp);
			key[i]=find(temp);
		}

		for (i=1;i<=n;i++)
			if (key[i]==i) sum++;
		
		printf("%d\n",sum);
	}

	return 0;
}

int find(int x)
{
	if (key[x]==x) return x;
	else
		return key[x]=find(key[x]);
}

 

Problem(B36):Reading books

Judge Info

Memory Limit: 32768KB

Case Time Limit: 10000MS

Time Limit: 10000MS

Judger: Number Only Judger

Description

In the summer vacation, LRJ wants to improvehimself in computer science. So he finds out N books of computer science in theschool library. The books are numbered from 0 to N-1.

To finish reading the i-th book, it takesLRJ time[i] minutes. But some books are similar in the content. If the i-thbook and the j-th book are similar, then if LRJ has finished reading the i-thbook, it will take him only \left \lfloor \frac{time[j]}{2} \right \rfloorminutes to finish reading the j-th book. Of course if LRJ has finished readingthe j-th book, it will take him only \left \lfloor \frac{time[i]}{2} \right\rfloor minutes to finish reading the i-th book. Now you are asked to tell LRJthe minimal total time to finish reading all the N books.

Input

The first line contains two integers N(0\leqN \leq 100) and M(0\leq M \leq N\times (N-1)/2). N is the total number ofbooks. M is the number of pairs which are similar.

Then the following N lines describetime[0],time[1],\cdots,time[n-1](1\leq time[i] \leq 10^{5}).

Next comes M lines, each contains twointeger (i,j), indicating that the i-th book and the j-th book are similar.

Input is ended with EOF.

Output

For each test case, just output the minimaltotal time on a single line.

Sample Input

2 1

6

10

0 1

3 2

1

2

3

0 1

1 2

3 1

2

4

6

0 1

Sample Output

11

3

10

 

程序:
#include <iostream>
using namespace std;

int f[105],t[105],temp1,temp2;
long long sum;

int find(int);

int main()
{
	int i,j,n,m,a,b;

	while (scanf("%d%d",&n,&m)!=EOF)
	{
		memset(f,0,sizeof(f));

		for (i=0;i<n;i++)
		{
			f[i]=i;
			scanf("%d",&t[i]);
		}

		for (i=0;i<m;i++)
		{
			scanf("%d%d",&temp1,&temp2);

			a=find(temp1);
			b=find(temp2);

			if (a!=b)
			{
				if (t[a]<t[b])
					swap(a,b);
				f[a]=b;
				for (j=0;j<n;j++)
					if (f[j]==a) f[j]=b;
			}
		}

		sum=0;
		for (i=0;i<n;i++)
		{
			if (f[i]==i) 
				sum+=t[i];
			else 
				sum+=t[i]/2;
		}

		printf("%lld\n",sum);
	}
	
	return 0;
}

int find(int x)
{
	if (f[x]==x) return x;
	else return f[x]=find(f[x]);
}
 

Problem(B64):Network Connections

Judge Info

Memory Limit: 32768KB

Case Time Limit: 10000MS

Time Limit: 10000MS

Judger: Normal

Description

Bob, who is a network administrator,supervises a network of computers. He is keeping a log connections between thecomputers in the network. Each connection is bi-directional. Two computers areinterconnected if they are directly connected or if they are interconnectedwith the same computer. Occasionally, Bob has to decide, quickly, whether twogiven computers are connected, directly or indirectly, according to the loginformation.

Write a program which based on informationinput from a text file counts the number of successful and the number ofunsuccessful answers to the questions of the kind : is computer i \,interconnected with computer j\, ?

Input

The first line of the input contains thenumber of dataset, and it's followed by a blank line. Each dataset is definedas follows:

The number of computers in the network N\,(N\leq 10,000);

A list of pairs of the form:

c\, computer i\, computer j\,, where computeri\, and computer j\, are integers from 1\, to N\,. A pair of this form showsthat computer i\, and computer j\, get interconnected.

q\, computer i\, computer j\,, wherecomputer i\, and computer j\, are integers from 1\, to N \,. A pair of this formstands for the question: is computer i\, interconnected with computer j\,?

There's a blank line between datasets.

Each pair is on a separate line. Pairs canappear in any order, regardless of their type. The log is updated after eachpair of type (a) and each pair of type (b) is processed according to thecurrent network configuration.

Output

For example, the input file illustrated inthe sample below corresponds to a network of 10 computers and 7 pairs. Thereare N1\, successfully answered questions and N2\, unsuccessfully answeredquestions. The program prints these two numbers to the standard output on thesame line, in the order: successful answers, unsuccessful answers, as shown inthe sample output. Print a blank line between datasets.

Sample Input

1

 

10

c 1 5

c 2 7

q 7 1

c 3 9

q 9 6

c 2 5

q 7 5

Sample Output

1,2

程序:
#include <iostream>
using namespace std;

int f[10005];

int find(int);

int main()
{
	int kase,n,i,j,k,no,yes,a,b;
	char c;

	scanf("%d",&kase);
	while (kase--)
	{
		scanf("%d",&n);
		getchar();

		for (k=1;k<=n;k++)
			f[k]=k;

		no=yes=0;

		while (1)
		{
			c=getchar();
			if (c=='\n') break;
			if (scanf("%d%d",&i,&j)==EOF) break;
			getchar();

			a=find(i);
			b=find(j);
			if (c=='c' && a!=b)
				f[a]=b;

			if (c=='q')
				if (a!=b)
					no++;
				else 
					yes++;
		}

		printf("%d,%d\n",yes,no);
		if (kase) printf("\n");
	}

	return 0;
}

int find(int x)
{
	if (f[x]==x) return x;
	else
		return f[x]=find(f[x]);
}
 

Problem(H63):Cup

Judge Info

Memory Limit: 65536KB

Case Time Limit: 1000MS

Time Limit: 1000MS

Judger: Float Numbers (1e-4) Judger

Description

中科黑超推出了一种新型水杯,我们可以将其称为椭球。

 

如图所示,水杯的外壳可以用方程

\frac {x^2}{r^2} + \frac {y^2}{r^2} + \frac{z^2}{h^2} = 1

来表示,水杯的 z 轴总是垂直于地面。 假设杯口在外壳顶部,且大小可忽略不计。 你恰好买了一个新型水杯,并且灌入了 v 单位体积的水。 请问此时水面距离杯底有多远?虽然直接用尺子就可以量出,但很不巧我们手头没有尺子。 不过我们有台电脑,所以请写个程序告诉我答案。

Input

输入包括多组数据。

每组数据占一行,包含三个整数r,h,v(r>0,h>0,0<=v<=4/3 \pi\, hr^2)

输入数据以 ”0 0 0” 结束。

Output

对每组数据输出水面和杯底的距离,四舍五入到小数点后四位。

Sample Input

1 1 1

1 1 0

1 1 4

0 0 0

Sample Output

0.6355

0.0000

1.7437


 

程序:
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

int f[10001];

int find(int);

int main()
{
	int i,n,m,a[10001],b[10001],t1,t2,x,y,kase;
	string st1,st2,temp;

	map<string,int> point;

	kase=1;

	while (scanf("%d",&n)!=EOF)
	{
		for (i=1;i<=n;i++)
		{
			f[i]=i;
			cin>>st1>>st2;

			if (!point[st1]) point[st1]=i;
			if (!point[st2]) point[st2]=i;
			x=find(point[st1]);
			y=find(point[st2]);
			if (x!=y)
				f[x]=y;
		}

		scanf("%d",&m);
		while (m--)
		{
			t1=t2=0;

			while (cin>>temp)
			{
				a[t1++]=f[point[temp]];
				if (getchar()=='\n')
					break;
			}

			while (cin>>temp)
			{
				b[t2++]=f[point[temp]];
				if (getchar()=='\n')
					break;
			}

			if (t1!=t2) printf("Case %d: no\n",kase++);
			else
			{
				sort(a,a+t1);
				sort(b,b+t2);
				for (i=0;i<t1;i++)
					if (a[i]!=b[i]) break;

				if (i==t1) 
					printf("Case %d: yes\n",kase++);
				else
					printf("Case %d: no\n",kase++);
			}
		}
	}

	return 0;
}

int find(int x)
{
	if (f[x]==x) return x;
	else
		return f[x]=find(f[x]);
}
 

Problem(J97):折半查找

Judge Info

 

Memory Limit: 32768KB

Case Time Limit: 1000MS

Time Limit: 1000MS

Judger: Number Only Judger

Description

给定一个静态有序查找表(不妨设为升序序列)S{s1,s2,…sn},n为表元素的个数, 以及一个需要查找的值Key,使用折半查找从静态查找表S中查找Key。

如果查找成功,输出成功标志1,否则输出0。查找成功时需输出Key为S中的第几个元素,不成功输出0。此外,不管查找成功还是失败,需输出查找过程中进行了几次比较操作。

例1: S={5,13,19,21,37,56,64,75,80,88,92},n = 11, Key=64

表中有64, 查找成功,为第7个元素,共折半比较了3次

因此,输出为 1 7 3

例2: S={5,13,19,21,37,56,64,75,80,88,92},n = 11, Key=59

表中没有59, 查找不成功,共折半比较了3次

因此,输出为 0 0 3

Input

第一行输入一个整数t,表示有t组测试数据

第二行起每三行表示一次折半搜索

第1行为静态有序查找表的元素个数

第2行为静态有序查找表S

第3行为待查找的Key

Output

输出成功标志、第几个元素、折半比较次数

Sample Input

2

11

3 11 17 23 29 37 56 75 83 87 93

56

9

11 19 23 31 39 43 53 67 71

41

 

Sample Output

1 7 3

0 0 3

 

程序:
#include <iostream>
using namespace std;

int main()
{
	int t,n,i,s,a[100000];
	int pos,time,l,r,x;

	memset(a,0,sizeof(a));

	scanf("%d",&t);
	while (t--)
	{
		scanf("%d",&n);
		for (i=1;i<=n;i++)
			scanf("%d",&a[i]);
		scanf("%d",&s);
		
		l=1;
		r=n;
		pos=0;
		time=0;

		while (l<=r)
		{
			x=(l+r)/2;
			time++;
			if (a[x]==s) 
			{
				pos=x;
				break;
			}
			else
			if (a[x]<s)
				l=x+1;
			else
				r=x-1;
		}

		if (pos)
			printf("%d %d %d\n",1,pos,time);
		else
			printf("%d %d %d\n",0,0,time);
	}

	return 0;
}


 

Problem(K28):Travel Problem

Judge Info

 

Memory Limit: 65536KB

Case Time Limit: 4000MS

Time Limit: 5000MS

Judger: Normal

Description

 

After SzuHope take part in the 36th ACMICPCAsia Chendu Reginal Contest. Then go to QingChengShan for fun. QCS has manyspot as the picture following, each spot has its unique value for SzuHope, theywill get the value just once if and only if they had been there. From one spotwill exists some roads to other spots, it means that one road link two spotswithout direction. SzuHope can choose any spot to travel at beginning, but thenthey can go to next spot only by roads. Can you help them make the travel’s totalvalue biggest? File:qcs.jpg

Input

There are less than 100 test cases. For eachcase, the first line has two numbers M,N.describe the number of spots androads(1<=N<=1000, 0<=M<=N*N) , the spots are numbered with 1,2,3…N;the second line has N numbers describe the value xi of eachspot(0<xi<1000); the next M line, each line has two numbers U,V(1<=U,V<=N) means U,V is connected. 0 0 for end.

Output

For each test case, you should output twolines. The first line is "Case #:", # means the number of the testcase. The second line is the result.

Sample Input

0 1

5

1 3

1 3 2

1 3

0 0

Sample Output

Case 1:

5

Case 2:

3



程序:
#include <iostream>
using namespace std;

int f[1005];

int find(int);

int main()
{
	int m,n,i,j,k,a,b,sum,kase;
	int x[1005];

	memset(x,0,sizeof(x));

	kase=1;

	while (scanf("%d%d",&m,&n) && m+n)
	{
		memset(f,0,sizeof(f));

		for (i=1;i<=n;i++)
		{
			scanf("%d",&x[i]);
			f[i]=i;
		}

		for (i=0;i<m;i++)
		{
			scanf("%d%d",&j,&k);
			a=find(j);
			b=find(k);
			if (a!=b)
			{
				f[a]=b;
				x[b]+=x[a];
			}
		}

		sum=0;
		for (i=1;i<=n;i++)
			if (f[i]==i)
				sum=max(sum,x[i]);

		printf("Case %d:\n",kase++);
		printf("%d\n",sum);
	}

	return 0;
}

int find(int x)
{
	if (f[x]==x) return x;
	else 
		return f[x]=find(f[x]);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值