WA求调
查看原帖
WA求调
1206518
Nervegas楼主2024/7/8 13:26
#include<bits/stdc++.h>
using namespace std;
const int maxn = 4000500;
const int maxm = 2050;
int h[maxm],cnt,hh[maxm],cnt1;
int dfn[maxn],sccno[maxn],low[maxn],dfs_clock,scc_cnt;
int size[maxm];//记录每个强连通分量的大小; 
int in[maxm];
int n;
int ans;
bitset<maxm> miao[maxm];//反向建图记录能到哪个点; 
string s;
struct node{
	int to,nxt;
}e[maxn],ee[maxn];
void add(int u,int v){
	e[++cnt].to = v;
	e[cnt].nxt = h[u];
	h[u] = cnt;
}
void addplus(int u,int v){
	ee[++cnt1].to = v;
	ee[cnt1].nxt = hh[u];
	hh[u] = cnt1;
}
void tarjan(int u){
	dfn[u] = low[u] = ++dfs_clock;
	stack<int> S;
	S.push(u);
	for(int i = h[u] ; i ; i = e[i].nxt){
		int v = e[i].to;
		if(!dfn[v]){
			tarjan(v);
			low[u] = min(low[u],low[v]);
		}
		else if(!sccno[v]){
			low[u] = min(low[u],dfn[v]);
		}
	}
	if(low[u] == dfn[u]){
		scc_cnt++;
		while(true){
			int v = S.top();
			S.pop();
			sccno[v] = scc_cnt;
			size[scc_cnt]++;
			if(v == u){
				break;
			}
		}
		miao[scc_cnt][scc_cnt] = 1;
	}
}
void tuopu(){
	queue<int> q;
	for(int i = 1 ; i <= scc_cnt ; i++){
		if(in[i] == 0){
			q.push(i);
		}
	}
	while(!q.empty()){
		int x = q.front();
		q.pop();
		for(int i = hh[x] ; i ; i = ee[i].nxt){
			int y = ee[i].to;
			miao[y] |= miao[x];//倒着把状态更新; 
			in[y]--;
			if(in[y] == 0){
				q.push(y); 
			}
		}
	}
} 
int main(){
	ios::sync_with_stdio(0),cin.tie(0);
	cin >> n;
	for(int i = 1 ; i <= n ; i++){
		cin >> s;
		for(int j = 0 ; j < s.size() - 1 ; j++){
			if(s[j] == '1'){
				add(i,j + 1);
			}
		}
	}
	for(int i = 1 ; i <= n ; i++){
		if(!dfn[i]){
			tarjan(i);
		}
	}
	for(int i = 1 ; i <= n ; i++){
		for(int j = h[i] ; j ; j = e[j].nxt){
			int y = e[j].to;
			if(sccno[i] != sccno[y]){
				addplus(sccno[y],sccno[i]);
				in[sccno[i]]++;//反向建图 
			}
		}
	}
	tuopu(); 
	for(int i = 1 ; i <= scc_cnt ; i++){
		for(int j = 1 ; j <= scc_cnt ; j++){
			if(miao[i][j]){
				ans += size[i] * size[j]; 
			}
		}
	}
	cout << ans << endl;  
	return 0;
} 
2024/7/8 13:26
加载中...