3.11 下午训练题 — 407
P3916 图的遍历
- 先用邻接表把图存起来
- 枚举每一个点,再搜从当前这个点能够到达的所有点,并记录最大的编号
- 用 数组记录每个点能够到达的最大编号
ACcode:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
vector<int>g[N];
int vis[N];
int n,m;
void dfs(int x,int w)
{
for(int i=0;i<g[x].size();i++)
{
if(!vis[g[x][i]])
{
vis[g[x][i]]=w;
dfs(g[x][i],w);
}
}
return ;
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
g[v].push_back(u);
}
for(int i=n;i>=1;i--)
{
if(vis[i])
continue;
vis[i]=i;
dfs(i,i);
}
for(int i=1;i<=n;i++)
cout<<vis[i]<<" ";
return 0;
}B3856 [语言月赛 202309] 椰奶国
这道题可以先分别计算出起始时刻和结束时刻是一天当中的第几秒,然后再作差,就能直接得到答案。
设一点东西:
- 表示一天有多少秒
- 表示 是第几秒
- 表示 是第几秒
则答案为
最后还要判断 是否为负数,如果为负数,说明 和 不在同一天,还要加上
ACcode:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, a, b, c, e, f, g;
void solve()
{
cin >> n >> a >> b >> c >> e >> f >> g;
int tot = 0;
for (int i = 0; i <= n - 1; i++)
tot += (1 + 10 * i + 1) * (i + 1) / 2;
int res1 = 0;
for (int i = 0; i <= a - 1; i++)
res1 += (1 + 10 * i + 1) * (i + 1) / 2;
res1 += (1 + (b - 1) * 10 + 1) * b / 2;
res1 += c + 1;
int res2 = 0;
for (int i = 0; i <= e - 1; i++)
res2 += (1 + 10 * i + 1) * (i + 1) / 2;
res2 += (1 + (f - 1) * 10 + 1) * f / 2;
res2 += g + 1;
if (res2 >= res1)
cout << res2 - res1 << "\n";
else
cout << res2 + tot - res1 << "\n";
return ;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
solve();
return 0;
}