跳到正文

个人记录

3.11 下午训练题 -- 407

Meovow约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟

版本

  1. v1当前

3.11 下午训练题 — 407

P3916 图的遍历

  • 先用邻接表把图存起来
  • 枚举每一个点,再搜从当前这个点能够到达的所有点,并记录最大的编号
  • 用 visvis 数组记录每个点能够到达的最大编号

ACcode:

C++
#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] 椰奶国

这道题可以先分别计算出起始时刻和结束时刻是一天当中的第几秒,然后再作差,就能直接得到答案。

设一点东西:

  • tottot 表示一天有多少秒
  • res1res1 表示 A:B:CA:B:C 是第几秒
  • res2res2 表示 D:E:FD:E:F 是第几秒

则答案为 res2res2 −- res1res1

最后还要判断 res2res2 −- res1res1 是否为负数,如果为负数,说明 A:B:CA:B:C 和 D:E:FD:E:F 不在同一天,还要加上 tottot

ACcode:

C++
#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;
}
原文