跳到正文

个人记录

总结(代码)

wuweize150815约 11 分钟
原文
文章信息与版本

文章信息

收录
2026年6月26日
更新
2026年7月3日
篇幅
约 11 分钟

版本

  1. v1当前

第一题:

C++
#include <bits/stdc++.h>
using namespace std;
vector <int> g[25];
string s[25];
string t;
long long dp[25];
int v[25] , in[25];
long long maxn=0;
int n;
bool p;
void bfs ( )
{
	queue <int> q;
	for (int i=1;i<=n;i++)
	{
		if (in[i]==0)
		{
			q.push (i);
			dp[i]=v[i];
			s[i]=to_string (i);
		}
	}
	while (!(q.empty ( )))
	{
		int p=q.front ( );
		q.pop ( );
		for (auto i:g[p])
		{
			if (dp[p]+v[i]>dp[i])
			{
				dp[i]=dp[p]+v[i];
				s[i]=s[p]+" "+to_string (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;
	for (int i=1;i<=n;i++)
	{
		cin >> v[i];
	}
	for (int i=1;i<=n;i++)
	{
		for (int j=i+1;j<=n;j++)
		{
			cin >> p;
			if (p==1)
			{
				g[i].push_back (j);
				in[j]++;
			} 
		}
	}
	bfs ( );
	for (int i=1;i<=n;i++)
	{
		if (dp[i]>maxn)
		{
			maxn=dp[i];
			t=s[i];
		}
	}
	cout << t << "\n" << maxn;
	return 0;
 } 

第二题:

C++
#include <bits/stdc++.h>
using namespace std;
vector <int> g[100005] , G[100005];
int dfn[100005] , low[100005];
bool vis[100005];
int dp[100005] , sz[100005];
int in[100005] , scc[100005];
int n , m , v;
int tot=0 , id=0 , maxn=0;
stack <int> st;
void tarjan (int now)
{
	tot++;
	dfn[now]=tot;
	low[now]=tot;
	st.push (now);
	vis[now]=1;
	for (auto i:g[now])
	{
		if (dfn[i]==0)
		{
			tarjan (i);
			low[now]=min (low[now] , low[i]);
		}
		else if (vis[i]==1)
		{
			low[now]=min (low[now] , dfn[i]);
		}
	}
	if (dfn[now]==low[now])
	{
		id++;
		while (1)
		{
			int t=st.top ( );
			st.pop ( );
			vis[t]=0;
			scc[t]=id;
			sz[id]++;
			if (t==now) break;
		}
	}
}
void bfs ( )
{
	queue <int> q;
	for (int i=1;i<=id;i++)
	{
		if (in[i]==0)
		{
			q.push (i);
			dp[i]=sz[i];
		}
	}
	while (!(q.empty ( )))
	{
		int p=q.front ( );
		q.pop ( );
		for (auto i:G[p])
		{
			dp[i]=max (dp[i] , dp[p]+sz[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;
	for (int i=1;i<=n;i++)
	{
		cin >> m;
		while (m--)
		{
			cin >> v;
			g[i].push_back (v);
		}
	}
	for (int i=1;i<=n;i++)
	{
		if (dfn[i]==0) tarjan (i);
	}
	for (int i=1;i<=n;i++)
	{
		for (auto j:g[i])
		{
			if (scc[i]==scc[j]) continue;
			G[scc[i]].push_back (scc[j]);
			in[scc[j]]++;
		}
	}
	bfs ( );
	for (int i=1;i<=id;i++)
	{
		maxn=max (maxn , dp[i]);
	}
	if (maxn==n)
	{
		cout << 1;
	}
	else
	{
		cout << 0;
	}
	return 0;
 } 

第三题:

C++
#include <bits/stdc++.h>
using namespace std;
const int M=8e4+5;
struct node
{
	int id , s , w;
};
vector <node> g[M];
vector <node> G[M];
int dfn[M] , low[M];
bool vis[M];
int in[M] , scc[M];
int he[M] , MAXN[M];
int dp[M];
int n , m , x , y , k , s , maxn=0;
double v;
int tot=0 , id=0;
stack <int> st;
void tarjan (int now)
{
	tot++;
	dfn[now]=tot;
	low[now]=tot;
	st.push (now);
	vis[now]=1;
	for (auto i:g[now])
	{
		if (dfn[i.id]==0)
		{
			tarjan (i.id);
			low[now]=min (low[now] , low[i.id]);
		}
		else if (vis[i.id]==1)
		{
			low[now]=min (low[now] , dfn[i.id]);
		}
	}
	if (dfn[now]==low[now])
	{
		id++;
		while (1)
		{
			int t=st.top ( );
			st.pop ( );
			vis[t]=0;
			scc[t]=id;
			if (t==now) break;
		}
	}
}
void bfs ( )
{
	memset (dp , -0x3f , sizeof (dp));
	dp[scc[s]]=he[scc[s]];
	queue <int> q;
	for (int i=1;i<=id;i++)
	{
		if (in[i]==0)
		{
			q.push (i);
		}
	}
	while (!(q.empty ( )))
	{
		int p=q.front ( );
		q.pop ( );
		for (auto i:G[p])
		{
			dp[i.id]=max (dp[i.id] , dp[p]+i.s);
			in[i.id]--;
			if (in[i.id]==0)
			{
				q.push (i.id);
			}
		}
	}
}
int main ( )
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	while (m--)
	{
		cin >> x >> y >> k >> v;
		g[x].push_back ({y , k , int (v*10) });
	}
	for (int i=1;i<=n;i++)
	{
		if (dfn[i]==0) tarjan (i);
	}
	for (int i=1;i<=n;i++)
	{
		for (auto j:g[i])
		{
			if (scc[i]==scc[j.id])
			{
				int sum=0;
				while (j.s!=0)
				{
					sum+=j.s;
					j.s=j.s*j.w/10;
				}
				he[scc[i]]+=sum;
			}
			else
			{
				G[scc[i]].push_back ({scc[j.id] , j.s , j.w});
				in[scc[j.id]]++;
			}
		}
	}
	cin >> s;
	bfs ( );
	for (int i=1;i<=id;i++)
	{
		maxn=max (maxn , dp[i]);
	}
	cout << maxn;
	return 0;
 } 

第四题:

C++
#include <bits/stdc++.h>
using namespace std;
map <pair <int , int> , bool> mp;
vector <int> g[50005];
vector <int> G[50005];
int dfn[50005] , low[50005];
bool vis[50005];
int sz[50005];
int dp[50005] , in[50005] , scc[50005];
int n , m , a , b;
int tot=0 , id=0;
stack <int> st;
void tarjan (int now)
{
	tot++;
	dfn[now]=tot;
	low[now]=tot;
	st.push (now);
	vis[now]=1;
	for (auto i:g[now])
	{
		if (dfn[i]==0)
		{
			tarjan (i);
			low[now]=min (low[now] , low[i]);
		}
		else if (vis[i]==1)
		{
			low[now]=min (low[now] , dfn[i]);
		}
	}
	if (dfn[now]==low[now])
	{
		id++;
		while (1)
		{
			int t=st.top ( );
			st.pop ( );
			vis[t]=0;
			scc[t]=id;
			sz[id]++;
			if (t==now) break;
		}
	}
}
void bfs ( )
{
	queue <int> q;
	for (int i=1;i<=id;i++)
	{
		if (in[i]==0)
		{
			q.push (i);
		}
		dp[i]=sz[i];
	}
	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 >> a >> b;
		g[b].push_back (a);
	}
	for (int i=1;i<=n;i++)
	{
		if (dfn[i]==0) tarjan (i);
	} 
	for (int i=1;i<=n;i++)
	{
		for (auto j:g[i])
		{
			if (scc[i]==scc[j]) continue;
			if (mp.count ({scc[i] , scc[j]})) continue;
			mp[{scc[i] , scc[j]}]=1;
			G[scc[i]].push_back (scc[j]);
			in[scc[j]]++;
		}
	}
	bfs ( );
	for (int i=1;i<=n;i++)
	{
		cout << dp[scc[i]]-1 << "\n";
	}
	return 0;
 } 
原文