萌新三体人没学过 OI,样例没过求条
查看原帖
萌新三体人没学过 OI,样例没过求条
661984
Stone_Xz楼主2024/8/13 21:46

用的 tarjan

#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N = 3;

int a, b, n, m, in, dfn[N], low[N], sum, scc[N], d[N], haha[N];

bool onstk[N], vis[N];

vector<int> nbr[N];

int ans;

stack<int> s;

void tarjan(int cur)
{
	dfn[cur] = low[cur] = ++in;
	s.push(cur);
	onstk[cur] = true;
	for(auto tmp : nbr[cur])
	{
		int nxt = tmp;
		if(dfn[nxt] == 0) {
			tarjan(nxt);
			low[cur] = min(low[nxt], low[cur]);
		}
		else if(onstk[nxt] == true) {
			low[cur] = min(low[cur], dfn[nxt]);
		}
	}
	if(dfn[cur] == low[cur])
	{
		while(s.top() != cur)
		{
			int tmp = s.top();
			s.pop();
			onstk[tmp] = false;
			scc[tmp] = sum;
			haha[scc[tmp]] += d[tmp];
		}
		s.pop();
		onstk[cur] = false;
		scc[cur] = sum;
		haha[scc[cur]] += d[cur];
	}
}

signed main()
{
	cin >> a >> b;
	n = 2;
	d[1] = a;
	d[2] = b;
	nbr[1].push_back(2);
	nbr[2].push_back(1);
	for(int i = 1; i <= n; i++)
		if(dfn[i] == 0) tarjan(i);
	for(int i = 1; i <= sum; i++)
	{
		ans += (!vis[scc[i]]) * haha[scc[i]];
  		vis[scc[i]] = true;
	}
	cout << ans;
	return 0;
}
2024/8/13 21:46
加载中...