为什么没有通过 hack 数据
查看原帖
为什么没有通过 hack 数据
768144
_Trangle_楼主2024/3/25 15:48
#include <bits/stdc++.h>
#define rep(i, b, e) for (auto i(b); i != e; i++)
#define req(i, b, e) for (auto i(b); i >= e; i--)
using namespace std;

int n, sc;
long long ans;
bool graph[2005][2005], tgraph[2005][2005];
int siz[2005], scc[2005];
bitset<2005> cango[2005];

int dfncnt, top;
int dfn[2005], low[2005];
int stk[2005];
bool vis[2005];

void tarjan(int cur) {
	dfn[cur] = low[cur] = ++dfncnt;
	stk[++top] = cur, vis[cur] = 1;

	rep(i, 1, n+1)
	if (graph[cur][i]) {
		if (!dfn[i]) {
			tarjan(i);
			low[cur] = min(low[cur], low[i]);
		}
		else if (vis[i]) low[cur] = min(low[cur], dfn[i]);
	}

	if (dfn[cur] == low[cur]) {
		sc++;

		do scc[stk[top]] = sc, vis[stk[top]] = 0;
		while (stk[top--] != cur);

		cango[sc][sc] = 1;
	}
}

int r;
int que[2005], degree[2005];

void topsort() {
	rep(i, 1, sc+1)
	if (!degree[i]) que[++r] = i;

	rep(f, 1, r+1) rep(i, 1, sc+1)
	if (tgraph[que[f]][i]) {
		cango[i] |= cango[que[f]];
		if (!(--degree[i])) que[++r] = i;
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cin >> n;

	rep(i, 1, n+1) {
		string s;
		cin >> s;
		rep(j, 0, s.size()) graph[i][j+1] = s[j] == 49;
	}

	rep(i, 1, n+1) {
		if (!dfn[i]) tarjan(i);
		siz[scc[i]]++;
	}

	rep(i, 1, n+1) rep(j, 1, n+1)
	if (graph[i][j] && scc[i] != scc[j])
		tgraph[scc[j]][scc[i]] = 1, degree[scc[i]]++;

	topsort();

	rep(i, 1, sc+1) rep(j, 1, sc+1)
	if (cango[i][j]) ans += siz[i] * siz[j];

	cout << ans;
	return 0;
}
2024/3/25 15:48
加载中...