社区讨论

站外提求助(拓扑排序)

学术版参与者 2已保存回复 7

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
7 条
当前快照
1 份
快照标识符
@mhj0to2t
此快照首次捕获于
2025/11/03 18:52
4 个月前
此快照最后确认于
2025/11/03 18:52
4 个月前
查看原帖
代码TLE了,空间也换不了,不然就MLE 代码如下:
CPP
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int n, a[N], l[N], op[N], lcx = 0, mx = 0;
vector<int>p[N], ciu;
struct node{
	int x, y;
}cch[N];
map<int, int>mp;
//void add(int xandy, int x, int y){
//	for(auto &i : p[x]){
//		p[xandy].push_back(i);
//		l[xandy]++;
//	}
//	for(auto &i : p[y]){
//		p[xandy].push_back(i);
//		l[xandy]++;
//	}
//}
void dy(int i){
	if(op[i] == 1){
		for(auto &j : p[i]){
			mx = max(mx, ++mp[j]);
			lcx++;
		}
	}else{
		dy(cch[i].x);
		dy(cch[i].y);
	}
}
signed main(){
	int t;cin >> t;while(t--){
		mp.clear();
		ciu.clear();lcx = 0;
		cin >> n;
		for(int i = 1; i <= n; i++){
			p[i].clear();
			cin >> op[i];
			if(op[i] == 1){
				cin >> l[i];
				for(int j = 1; j <= l[i]; j++){
					int pcl;cin >> pcl;
					p[i].push_back(pcl);
				}
			}else{
				int x, y;cin >> x >> y;
				cch[i].x = x, cch[i].y = y;
			}
		}
//		for(auto &i : p[n]){
//			cout << i << " ";
//		}cout << "\n";
		mx = 0;
		dy(n);
		if(mx >= lcx / 2)cout << 2 * (lcx - mx) << "\n";
		else cout << lcx << "\n";
	}
	return 0;
}
主要的修改应该是拓扑排序。
老师说要用拓扑排序,但有点久没写过了,所以本人不太看得出来,求助!!!

回复

7 条回复,欢迎继续交流。

正在加载回复...