蒟蒻关于全局变量的问题
  • 板块学术版
  • 楼主CommonDigger
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/8/19 21:43
  • 上次更新2024/8/20 09:29:59
查看原帖
蒟蒻关于全局变量的问题
551417
CommonDigger楼主2024/8/19 21:43

对不起,我是真的蒟蒻

有些地方,循环变量采用全局变量会WA,但是独立之后就AC了?

比如这个程序(P2341)中的j变量:

#include "iostream"
#include "stack"
#include "cstring"
using namespace std;
int n, m, temp1, temp2, t=0, ans, belong[10005], siz[10005];
int head[10005], idx;
int dfn[10005],  low[10005];
int out[10005], c1, c2;
bool ins[10005];
stack<int>s;
struct edge{
	int to, nxt;
}e[100005];
void add_edge(int u, int v){
	e[++idx].to=v;
	e[idx].nxt=head[u];
	head[u]=idx;
}
void dfs(int x){
	low[x]=dfn[x]=++t;
	s.push(x);
	ins[x]=true;
	for(int i=head[x];i;i=e[i].nxt){
		int j=e[i].to;
		if(!dfn[j]){
			dfs(j);
			low[x]=min(low[x], low[j]);
		}else if(ins[j]) low[x]=min(low[x], dfn[j]);
	}
	if(dfn[x]==low[x]){
		ins[x]=false;
		belong[x]=++ans;
		siz[ans]=1;
		while(s.top()!=x)
		{
			belong[s.top()]=ans;
			ins[s.top()]=false;
			siz[ans]++;
			s.pop();
		}
		s.pop();
	}
}
int main(){
	scanf("%d%d", &n, &m);
	for(int i=1;i<=m;i++){
		scanf("%d%d", &temp1, &temp2);
		add_edge(temp1, temp2);
	}
	for(int i=1;i<=n;i++)
		if(!dfn[i]) dfs(i);
	for(int i=1;i<=n;i++){
		for(int j=head[i];j;j=e[j].nxt){
			if(belong[i]!=belong[e[j].to])
				out[belong[i]]++;
		}
	}
	for(int i=1;i<=ans;i++) if(out[i]==0) c1++, c2+=siz[i];
	if(c1==1) printf("%d", c2);
	else printf("0");
}

当他定义为全局变量的时候会WA,独立的时候AC

求为什么?

2024/8/19 21:43
加载中...