蓝桥算法训练营—提高组—2023年2.8日打卡

_谦言万语 2023-02-08 22:19:13

青蛙的约会

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL a,b,m,n,L,t,k;

LL exgcd(LL a, LL b, LL &x, LL &y)
{
    if(!b)
    {
        x=1,y=0;
        return a;
    }
    LL d=exgcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}

int main()
{
    cin>>a>>b>>m>>n>>L;
    if(m>n)
    {
        swap(m,n);
        swap(a,b);
    }
    LL d=exgcd(n-m,L,t,k);
    if((a-b)%d) puts("Impossible");
    else
    {
        t*=(a-b)/d;
        LL h=L/d;
        cout<<(t%h+h)%h;
    }
    return 0;
}

没有上司的舞会

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int w[6005],s[6005],dp[6005][2];
int n,root;
vector<int> son[6005];

void dfs(int x) {
	dp[x][0]=0;
	dp[x][1]=w[x];
	for(int i=0; i<son[x].size(); i++) {
		int y=son[x][i];
		dfs(y);
		dp[x][0]+=max(dp[y][0],dp[y][1]);
		dp[x][1]+=dp[y][0];
	}
}

int main() {
	cin>>n;
	for(int i=1; i<=n; i++)
	cin>>w[i];
	for(int i=1; i<=n-1; i++) {
		int x,y;
		cin>>x>>y;
		son[y].push_back(x);
		s[x]=1;
	}
	for(int i=1; i<=n; i++) {
		if(!s[i]) {
		root=i;
		break;}
	}
	dfs(root);
	cout<<max(dp[root][0],dp[root][1]);
	return 0;
}

 

...全文
20 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,798

社区成员

发帖
与我相关
我的任务
社区描述
和众多高校算法内卷分子,一起学习和交流算法那。浓郁的算法交流氛围,拒绝躺平,有效内卷。加入我们,私信我拉你入核心内卷群。
算法数据结构leetcode 个人社区
社区管理员
  • 执 梗
  • Dream-Y.ocean
  • ღCauchyོꦿ࿐
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

 刷题!

试试用AI创作助手写篇文章吧