49,425
社区成员




青蛙的约会
#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;
}