第一题:
#include <bits/stdc++.h>
using namespace std;
vector <int> g[10005];
long long dp[10005];
int in[10005] , v[10005];
long long maxn=0;
int n , m , x , y;
void bfs ( )
{
queue <int> q;
for (int i=1;i<=n;i++)
{
if (in[i]==0)
{
q.push (i);
dp[i]=v[i];
}
}
while (!(q.empty ( )))
{
int p=q.front ( );
q.pop ( );
for (auto i:g[p])
{
dp[i]=max (dp[i] , dp[p]+v[i]);
in[i]--;
if (in[i]==0)
{
q.push (i);
}
}
}
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i=1;i<=n;i++)
{
cin >> v[i];
}
while (m--)
{
cin >> x >> y;
g[x].push_back (y);
in[y]++;
}
bfs ( );
for (int i=1;i<=n;i++)
{
maxn=max (maxn , dp[i]);
}
cout << maxn;
return 0;
} 第二题:
#include <bits/stdc++.h>
using namespace std;
vector <int> g[100005];
int dp[100005];
int in[100005] , IN[100005] , OUT[100005];
int n , m , x , y , sum=0;
void bfs ( )
{
queue <int> q;
for (int i=1;i<=n;i++)
{
if (in[i]==0)
{
q.push (i);
dp[i]=1;
}
}
while (!(q.empty ( )))
{
int p=q.front ( );
q.pop ( );
for (auto i:g[p])
{
dp[i]+=dp[p];
in[i]--;
if (in[i]==0)
{
q.push (i);
}
}
}
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
while (m--)
{
cin >> x >> y;
in[y]++;
IN[y]++;
OUT[x]++;
g[x].push_back (y);
}
bfs ( );
for (int i=1;i<=n;i++)
{
if (OUT[i]==0&&IN[i]>0)
{
sum+=dp[i];
}
}
cout << sum;
return 0;
} 第三题:
#include <bits/stdc++.h>
using namespace std;
vector <int> g[30];
string ans;
string s[105];
map <char , bool> mp;
int in[30];
bool p=0;
int n;
void cz (int a , int b)
{
int A=s[a].size ( ) , B=s[b].size ( );
for (int i=0;i<=min (A , B)-1;i++)
{
if (s[a][i]==s[b][i]) continue;
int x=s[a][i]-'a'+1 , y=s[b][i]-'a'+1;
g[x].push_back (y);
in[y]++;
return;
}
if (A>B)
{
cout << "!";
exit (0);
}
}
void bfs ( )
{
queue <char> q;
for (char i='a';i<='z';i++)
{
if (in[i-'a'+1]==0&&mp.count (i))
{
q.push (i);
}
}
while (!(q.empty ( )))
{
if (q.size ( )>=2) p=1;
char p=q.front ( );
q.pop ( );
ans+=p;
for (auto i:g[p-'a'+1])
{
in[i]--;
if (in[i]==0)
{
q.push (i+'a'-1);
}
}
}
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> s[i];
for (int j=0;j<=s[i].size ( )-1;j++)
{
mp[s[i][j]]=1;
}
if (i>1)
{
cz (i-1 , i);
}
}
bfs ( );
if (ans.size ( )!=mp.size ( ))
{
cout << "!";
}
else if (p==1)
{
cout << "?";
}
else
{
cout << ans;
}
return 0;
}第四题:
#include <bits/stdc++.h>
using namespace std;
vector <int> g[200005];
long long dp[200005];
int cnt[200005];
int in[200005];
long long sum=0;
int n , x , y;
void bfs ( )
{
queue <int> q;
for (int i=1;i<=n;i++)
{
if (in[i]==0)
{
q.push (i);
cnt[i];
}
}
while (!(q.empty ( )))
{
int p=q.front ( );
q.pop ( );
for (auto i:g[p])
{
dp[i]+=dp[p]+cnt[p]+1;
cnt[i]+=cnt[p]+1;
in[i]--;
if (in[i]==0)
{
q.push (i);
}
}
}
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i=1;i<=n-1;i++)
{
cin >> x >> y;
g[x].push_back (y);
in[y]++;
}
bfs ( );
for (int i=1;i<=n;i++)
{
sum+=dp[i];
}
cout << sum;
return 0;
}