Week6-程序设计思维与实践

作业题


A

题意

实验室里原先有一台电脑(编号为1),最近实验室又购置了N-1台电脑,编号为2到N。每台电脑都用网线连接到一台先前安装的电脑上,求第i台电脑到其他电脑的最大网线长度。
在这里插入图片描述
样例说明:样例输入对应这个图,从这个图中你可以看出,距离1号电脑最远的电脑是4号电脑,他们之间的距离是3。 4号电脑与5号电脑都是距离2号电脑最远的点,故其答案是2。5号电脑距离3号电脑最远,故对于3号电脑来说它的答案是3。同样的我们可以计算出4号电脑和5号电脑的答案是4。

Input

输入文件包含多组测试数据。对于每组测试数据,第一行一个整数N (N<=10000),接下来有N-1行,每一行两个数,对于第i行的两个数,它们表示与i号电脑连接的电脑编号以及它们之间网线的长度。网线的总长度不会超过 1 0 9 10^9 109,每个数之间用一个空格隔开。

Output

对于每组测试数据输出N行,第i行表示i号电脑的答案 (1<=i<=N).

Sample Input

5
1 1
2 1
3 1
1 1

Sample Output

3
2
3
4
4

思路

根据输入数据建树,假设树的最长路的两个叶子结点为 v 1 , v 2 v1,v2 v1,v2,这道题要求找到某个结点 x x x 所能到达的最长路径,那么这个结点 x x x 的最长路径要么是到 v 1 v1 v1的路径,要么就是到 v 2 v2 v2的路径,所以首先需要从任意结点开始执行DFS找到最远结点 v 1 v1 v1,然后再以 v 1 v1 v1为源点执行DFS找到另一个最远结点 v 2 v2 v2同时记录 v 1 v1 v1到各点的最长路径 d 1 [ N ] d1[N] d1[N],最后再以 v 2 v2 v2为源点执行DFS,同时记录 v 2 v2 v2到各点的最长路径 d 2 [ N ] d2[N] d2[N],则某个结点 x x x所能到达的最长路径即为 m a x ( d 1 [ x ] , d 2 [ x ] ) max(d1[x],d2[x]) max(d1[x],d2[x])

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=100010;

struct Edge{
	int u,v,w,next;
}Edges[maxn];
int head[maxn],d1[maxn],d2[maxn],tot,vv;
bool vis[maxn];

void init(int n){
	tot=0;
	for(int i=0;i<n;i++)
		head[i]=-1;	
}

void addEdge(int u,int v,int w){
	Edges[tot].u=u;
	Edges[tot].v=v;
	Edges[tot].w=w;
	Edges[tot].next=head[u];
	head[u]=tot;
	tot++;
}

void dfs(int u,int path,int *d){
	d[u]=path;
	if(d[vv]<d[u])
		vv=u;
	for(int i=head[u];i!=-1;i=Edges[i].next){
		if(!vis[Edges[i].v]){
			vis[Edges[i].v]=true;
			dfs(Edges[i].v, path+Edges[i].w, d);
		}
	}
}

int main()
{
	int n,u,v,w;
	while(~scanf("%d",&n)){
		init(n+1);
		for(int i=2;i<=n;i++){
			scanf("%d%d",&v,&w);
			addEdge(i,v,w);
			addEdge(v,i,w);
		}
		//找到最远点v1
		memset(vis,0,sizeof(vis)); 
		d1[vv]=0;
		vis[1]=1;
		dfs(1,0,d1);
		//找到最远点v2 
		memset(vis,0,sizeof(vis));
		d1[vv]=0;
		vis[vv]=1;
		dfs(vv,0,d1);
		//v2执行dfs 
		memset(vis,0,sizeof(vis));
		d2[vv]=0;
		vis[vv]=1;
		dfs(vv,0,d2);
		for(int i=1;i<=n;i++){
			int maxd=max(d1[i],d2[i]);
			printf("%d\n",maxd);
		}
	}
}

B

题意

编号为0的同学感染了新冠肺炎,输入几个聚集团体的信息,要求找出与0号同学直接或间接接触过的人数。

Input

多组数据,对于每组测试数据:
第一行为两个整数n和m(n = m = 0表示输入结束,不需要处理),n是学生的数量,m是学生群体的数量。0 < n <= 3e4 , 0 <= m <= 5e2
学生编号为0~n-1
小A编号为0
随后,m行,每行有一个整数num即小团体人员数量。随后有num个整数代表这个小团体的学生。

Output

输出要隔离的人数,每组数据的答案输出占一行

Sample Input

100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0

Sample Output

4
1
1

思路

并查集的简单应用,将每一组的同学进行合并,最后输出0号同学所属集合的人数即可。

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn=100010;
int n,m,num,p,t,f[maxn],rnk[maxn];

int father(int x){
	while(f[x]!=x){
		f[x]=f[f[x]];
		x=f[x];
	}
	return x;
}

void unionfather(int x,int y){
	x=father(x),y=father(y);
	if(x==y)
		return;
	if(rnk[x]>rnk[y])
		swap(x,y);
	f[x]=y;
	rnk[y]=rnk[x]+rnk[y];
	rnk[x]=rnk[y];
}

int main()
{
	while(~scanf("%d%d",&n,&m)){
		if(n==0&&m==0)
			break;
		for(int i=0;i<n;i++){
			f[i]=i;
			rnk[i]=1;
		}
		for(int i=0;i<m;i++){
			scanf("%d",&num);
			scanf("%d",&p);
			for(int j=1;j<num;j++){
				scanf("%d",&t);
				unionfather(p,t);
			}
		}
		printf("%d\n",rnk[father(0)]);
	}
	return 0;
}

C

题意

东东在老家农村无聊,想种田。农田有 n 块,编号从 1~n。
众所周知东东是一个魔法师,他可以消耗一定的 MP 在一块田上施展魔法,使得黄河之水天上来。他也可以消耗一定的 MP 在两块田的渠上建立传送门,使得这块田引用那块有水的田的水。 (1<=n<=3e2)
黄河之水天上来的消耗是 Wi,i 是农田编号 (1<=Wi<=1e5)
建立传送门的消耗是 Pij,i、j 是农田编号 (1<= Pij <=1e5, Pij = Pji, Pii =0)
求灌溉田地的最小消耗。

Input

第1行:一个数n
第2行到第n+1行:数wi
第n+2行到第2n+1行:矩阵即pij矩阵

Output

东东最小消耗的MP值

Sample Input

4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0

Sample Output

9

思路

由于对每一农田有一个隐含的灌溉路径 W i W_i Wi,那么我们再加一个源点,并向 n n n个边连边,边权为 W i W_i Wi,然后对这 n + 1 n+1 n+1点执行一遍Kruskal算法即可得到最终答案。

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;

struct edge{
	int from,to,dis;
	edge(){}
	edge(int ff,int tt,int dd):from(ff),to(tt),dis(dd){}
	bool operator<(const edge &a){
		return dis<a.dis;
	}
};

int f[50100],n,w,p;
edge a[501000];

int father(int x){
	while(f[x]!=x){
		f[x]=f[f[x]];
		x=f[x];
	}
	return x;
}

void unionfather(int x,int y){
	f[father(y)]=father(x);
}


int main()
{
	scanf("%d",&n);
	int cnt=0;
	for(int i=0;i<n;i++){
		scanf("%d",&w);
		a[cnt++]=edge(n,i,w);
		a[cnt++]=edge(i,n,w);
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			scanf("%d",&p);
			a[cnt++]=edge(i,j,p);
		}
	}
	sort(a,a+cnt);
	int ans=0,count=0;
	for(int i=0;i<=n;i++){
		f[i]=i;
	}
	for(int i=0;i<cnt;i++){
		if(count==n)
			break;
		if(father(a[i].from)!=father(a[i].to)){
			unionfather(a[i].from,a[i].to);
			ans+=a[i].dis;
			count++;
		}
	}
	printf("%d\n",ans);
	return 0;
}

D

题意

在这里插入图片描述
在这里插入图片描述

思路

总结一下题意就是给定一个无向图,求解一棵生成树使得最大边权最小,直接执行一遍Kruskal算法,输出最小生成树中的最大边即可。

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;

int n,m,root;
struct edge{
	int from,to,dis;
	edge(){}
	edge(int ff,int tt,int dd):from(ff),to(tt),dis(dd){}
	bool operator<(const edge &a){
		return dis<a.dis;
	}
};

int f[500100];
edge a[500100];

int father(int x){
	while(f[x]!=x){
		f[x]=f[f[x]];
		x=f[x];
	}
	return x;
}

void unionfather(int x,int y){
	f[father(y)]=father(x);
}

int main()
{
	scanf("%d%d%d",&n,&m,&root);
	for(int i=0;i<m;i++){
		scanf("%d%d%d",&a[i].from,&a[i].to,&a[i].dis);
	}
	sort(a,a+m);
	for(int i=1;i<=n;i++)
		f[i]=i;
	int maxt=0,cnt=0;
	for(int i=0;i<m;i++){
		if(cnt==n-1)
			break;
		if(father(a[i].from)!=father(a[i].to)){
			maxt=max(maxt,a[i].dis);
			unionfather(a[i].from,a[i].to);
			cnt++;
		}
	}
	printf("%d\n",maxt);
	return 0;
}


模拟题

题意

东东有 A × B 张扑克牌。每张扑克牌有一个大小(整数,记为a,范围区间是 0 到 A - 1)和一个花色(整数,记为b,范围区间是 0 到 B - 1) ,扑克牌是互异的,也就是独一无二的,也就是说没有两张牌大小和花色都相同。 “一手牌”的意思是你手里有5张不同的牌,这 5 张牌没有谁在前谁在后的顺序之分,它们可以形成一个牌型。 我们定义了 9 种牌型,如下是 9 种牌型的规则,我们用“低序号优先”来匹配牌型,即这“一手牌”从上到下满足的第一个牌型规则就是它的“牌型编号”(一个整数,属于1到9):

  • 同花顺: 同时满足规则 2 和规则 3.
  • 顺子 : 5张牌的大小形如 x, x + 1, x + 2, x + 3, x + 4
  • 同花 : 5张牌都是相同花色的.
  • 炸弹 : 5张牌其中有4张牌的大小相等.
  • 三带二 : 5张牌其中有3张牌的大小相等,且另外2张牌的大小也相等.
  • 两对: 5张牌其中有2张牌的大小相等,且另外3张牌中2张牌的大小相等.
  • 三条: 5张牌其中有3张牌的大小相等.
  • 一对: 5张牌其中有2张牌的大小相等.
  • 要不起: 这手牌不满足上述的牌型中任意一个.

现在, 东东从A × B 张扑克牌中拿走了 2 张牌!分别是 (a1, b1) 和 (a2, b2). (其中a表示大小,b表示花色) 现在要从剩下的扑克牌中再随机拿出 3 张!组成一手牌!!
其实东东除了会打代码,他业余还是一个魔法师,现在他要预言他的未来的可能性,即他将拿到的“一手牌”的可能性,我们用一个“牌型编号(一个整数,属于1到9)”来表示这手牌的牌型,那么他的未来有 9 种可能,但每种可能的方案数不一样。
现在,东东的阿戈摩托之眼没了,你需要帮他算一算 9 种牌型中,每种牌型的方案数。

Input

第 1 行包含了整数 A 和 B (5 ≤ A ≤ 25, 1 ≤ B ≤ 4).
第 2 行包含了整数 a1, b1, a2, b2 (0 ≤ a1, a2 ≤ A - 1, 0 ≤ b1, b2 ≤ B - 1, (a1, b1) ≠ (a2, b2)).

Output

输出一行,这行有 9 个整数,每个整数代表了 9 种牌型的方案数(按牌型编号从小到大的顺序)

Examples

Input
5 2
1 0 3 1
Output
0 8 0 0 0 12 0 36 0
Input
25 4
0 0 24 3
Output
0 0 0 2 18 1656 644 36432 113344

思路

解决方法较为简单,判断顺子时,先将5张牌进行排序,然后再判断是否满足相应条件即可;判断其他牌型时,先创建一个int到int的map,将5张牌插入到map中,然后根据map的大小以及各关键字所包含元素的多少进行判断即可。

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;

int A,B,a_1,b_1,a_2,b_2,idex,res[10];

struct s{
	int a,b;
	s(){}
	s(int aa,int bb):a(aa),b(bb){}
	friend bool operator<(const s &s1,const s &s2){
		return s1.a<s2.a;
	}
};

s c[10],tmp[1000];


//2张相等 
bool eight(){
	map<int,int> mp; 
	for(int i=0;i<5;i++){
		mp[c[i].a]++;
	}
	map<int,int>::iterator it=mp.begin();
	for(it;it!=mp.end();it++){
		if(it->second>=2)
			return true;
	}		
	return false;	
}
//3张相等 
bool seven(){
	map<int,int> mp; 
	for(int i=0;i<5;i++){
		mp[c[i].a]++;
	}
	map<int,int>::iterator it=mp.begin();
	for(it;it!=mp.end();it++){
		if(it->second>=3)
			return true;
	}		
	return false;	
}
//2张相等,2张相等 
bool six(){
	map<int,int> mp; 
	for(int i=0;i<5;i++){
		mp[c[i].a]++;
	}
	int cnt=0;
	map<int,int>::iterator it=mp.begin();
	for(it;it!=mp.end();it++){
		if(it->second>=2)
			cnt++;
	}		
	if(cnt>=2)
		return true;
	else
		return false;
}
//三张相等,另外两张也相等 
bool five(){
	map<int,int> mp; 
	bool flag_1=false;
	bool flag_2=false;
	for(int i=0;i<5;i++){
		mp[c[i].a]++;
	}
	if(mp.size()==2){
		map<int,int>::iterator it=mp.begin();
		for(it;it!=mp.end();it++){
			if(it->second==3){
				flag_1=true;
			}
			if(it->second==2){
				flag_2=true;
			}
		}
	}
	if(flag_1&&flag_2)
		return true;
	else
		return false;	
}
//四张牌大小相等 
bool four(){
	map<int,int> mp; 
	for(int i=0;i<5;i++){
		mp[c[i].a]++;
	}
	map<int,int>::iterator it=mp.begin();
	for(it;it!=mp.end();it++){
		if(it->second>=4)
			return true;
	}
	return false;
}
//5张牌花色相同 
bool three(){
	int x=c[0].b;
	for(int i=1;i<5;i++){
		if(c[i].b!=x)
			return false;
	}
	return true;	
}
//顺子 
bool two(){
	sort(c,c+5);
	int x=c[0].a;
	for(int i=1;i<5;i++){
		x++;
		if(c[i].a!=x){
			return false;
		}
	}
	return true;	
}
//同花顺 
bool one(){
	bool flag_1=two();
	bool flag_2=three();
	return flag_1&&flag_2; 
}

bool isok(int i,int j){
	if((i==a_1&&j==b_1)||(i==a_2&&j==b_2))
		return false;
	return true;
}

void Solve(){
	if(one()){
		res[1]++;
	}
	else if(two()){
		res[2]++;
	}
	else if(three()){
		res[3]++;
	}
	else if(four()){
		res[4]++;
	}
	else if(five()){
		res[5]++;
	}
	else if(six()){
		res[6]++;
	}
	else if(seven()){
		res[7]++;
	}
	else if(eight()){
		res[8]++;
	}
	else 
		res[9]++;
}


int main()
{
	cin>>A>>B;
	cin>>a_1>>b_1>>a_2>>b_2;	
	memset(res,0,sizeof(res));
	int id=0;
	for(int i=0;i<A;i++){
		for(int j=0;j<B;j++){
			if(isok(i,j)){
				tmp[id++]=s(i,j);
			}	
		}
	}
	for(int i=0;i<id;i++){		
		for(int j=i+1;j<id;j++){			
			for(int k=j+1;k<id;k++){
				c[0].a=a_1;c[0].b=b_1;
				c[1].a=a_2;c[1].b=b_2;				
				c[2]=tmp[i];
				c[3]=tmp[j];
				c[4]=tmp[k];
				Solve();
			}
		}
	}
	for(int i=1;i<=9;i++)
		cout<<res[i]<<" ";
	cout<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值