跳到正文

个人记录

B3644玄学数组大小(?)

xcc_114约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟

版本

  1. v1当前

原题:1≤N≤100

第一遍提交:

C++
#include<bits/stdc++.h>

using namespace std;

const int maxn = 105;
int n, x, in[maxn];
int head[maxn], to[maxn], nx[maxn], cnt;
queue<int> q;

void add(int u, int v){
	to[++cnt] = v;
	nx[cnt] = head[u];
	head[u] = cnt;
	in[v]++;
}

void toposort(){
	while(!q.empty()){
		int u = q.front();
		cout << u << " ";
		q.pop();
		for(int i = head[u]; i; i = nx[i]){
			in[to[i]]--;
			if(in[to[i]] == 0){
				q.push(to[i]);
			}
		}
	}
}

int main(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		while(1){
			cin >> x;
			if(x == 0) break;
			else add(i, x);
		}
	}
	for(int i = 1; i <= n; i++){
		if(in[i] == 0){
			q.push(i);
		}
	}
	toposort();
	return 0;
}

提交结果:86pts

第二遍提交:

C++
#include<bits/stdc++.h>

using namespace std;

const int maxn = 10005;
int n, x, in[maxn];
int head[maxn], to[maxn], nx[maxn], cnt;
queue<int> q;

void add(int u, int v){
	to[++cnt] = v;
	nx[cnt] = head[u];
	head[u] = cnt;
	in[v]++;
}

void toposort(){
	while(!q.empty()){
		int u = q.front();
		cout << u << " ";
		q.pop();
		for(int i = head[u]; i; i = nx[i]){
			in[to[i]]--;
			if(in[to[i]] == 0){
				q.push(to[i]);
			}
		}
	}
}

int main(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		while(1){
			cin >> x;
			if(x == 0) break;
			else add(i, x);
		}
	}
	for(int i = 1; i <= n; i++){
		if(in[i] == 0){
			q.push(i);
		}
	}
	toposort();
	return 0;
}

提交结果:100pts

只改了一个maxn,即数组大小,不开到1e3貌似都过不了

题目链接:toposort模板 求dalao解答。

原文