user-0 提问: 拓扑排序问题

回复:
user: user-0

user: user-0
content: 引用 7 楼 mxway 的回复:输入 第一行两个数n,m,代表节点数和边数 m行，每行两个数代表一条有向边  测试数据范围：(1<=n<=50,0<=m<2500) const int MAXN = 25; 你的节点数才定义25，这肯定会有数组下标越界的行为。  第二你没有考虑重边的问题，题目已经有提示了。看下面的测试用例，两个1,2，1,2这个应该只能算是一条边吧。 5 5 1 2 1 2 2 5 3 4 4 5 我竟然看做这个上限了。 还有如果我这么排序的话也会正确输出拓扑排序，我当时想重边并不会影响输出结果。但是现在有个问题就是，是不是有重边就不是DAG了，应该输出no。 DAG好像只是简单图才行？？不胜感激

user: user-1
content: 输入 第一行两个数n,m,代表节点数和边数 m行，每行两个数代表一条有向边  测试数据范围：(1<=n<=50,0<=m<2500) const int MAXN = 25; 你的节点数才定义25，这肯定会有数组下标越界的行为。  第二你没有考虑重边的问题，题目已经有提示了。看下面的测试用例，两个1,2，1,2这个应该只能算是一条边吧。 5 5 1 2 1 2 2 5 3 4 4 5

user: user-0
content: 这下看着舒服多了

user: user-0
content: #define LOCAL #include<iostream> #include<vector> #include<queue> #include<cstring> //#include<stack> using namespace std;  const int MAXN = 25; const int MAXM = 2501; int n,m,inDegree[MAXN];//入度 vector<int> G[MAXN];//邻接表存储图 int vis[MAXN];//深度遍历访问标志  vector<int> circle;//存放DFS所找到的第一个环 int count = 0;//记录环的个数 vector<int> trace;//记录遍历路径  bool topologicalSort();//是否DAG，是则输出拓扑排序之一 //void topologicalPrint(); void circlePrint();//不是DAG，则输出DFS所找到的第一个环 bool isBiCircle();//是否存在自环，存在则输出 int main(){ #ifdef LOCAL 	freopen("in.txt","r",stdin); 	freopen("out.txt","w",stdout); #endif 	memset(inDegree,0,sizeof(inDegree)); 	memset(vis,0,sizeof(vis)); 	cin>>n>>m; 	int from,to; 	for(int i = 0; i < m; i++){ 		cin>>from>>to; 		G[from].push_back(to); 		inDegree[to]++; 	} 	/*for(int i = 0 ; i <= n ; i++){ 		cout<<i<<":"; 		for(int j = 0 ; j < G[i].size() ; j++){ 			cout<<G[i][j]<<"-->"; 		} 		cout<<endl; 	}*/ 	if(isBiCircle()) 		return 0; 	if(topologicalSort()){ 		return 0; 	}else{ 		circlePrint(); 		//cout<<"wtf"; 	} 	return 0; } bool topologicalSort(){ 	int num = 0; 	queue<int> a; 	vector<int> print1; 	for(int i = 1; i <= n; i++){ 		if(inDegree[i] == 0){ 			a.push(i); 		} 	} 	while(!a.empty()){ 		int u = a.front(); 		print1.push_back(u); 		num++; 		a.pop(); 		for(int i = 0 ; i < G[u].size() ; i++){ 			int v = G[u][i]; 			inDegree[v]--; 			if(inDegree[v] == 0){ 				a.push(v); 			} 		} 	} 	if(num == n){ 		int first = 1; 		cout<<"YES"<<endl; 		for(int i = 0 ; i < print1.size() ; i++){ 			if(first){ 				first = 0; 				cout<<print1[i]; 			} 			else{ 				cout<<","<<print1[i]; 			} 		} 		cout<<endl; 		return true; 	} 	else return false; } void DFS(int u){ 	vis[u] = 1; 	trace.push_back(u); 	for(int i = 0; i < G[u].size() ; i++){ 		int v = G[u][i]; 		if(v == trace[0]){//找到环了 			circle.assign(trace.begin(),trace.end());//把遍历路径以及最后定点加入环中 			circle.push_back(v); 			return; 		} 		if(vis[v] == 0){ 			DFS(v); 		} 	} 	trace.pop_back(); } void circlePrint(){ 	for(int i = 1 ; i <= n; i++){ 		if(vis[i] == 0) 			DFS(i); 	} 	cout<<"NO"<<endl<<circle[0]; 	for(int i = 1 ; i < circle.size() ; i++) 		cout<<","<<circle[i]; 	cout<<endl; 	 } bool isBiCircle(){ 	for(int i = 1 ; i <= n ; i++){ 		for(int j = 0 ; j < G[i].size() ; j++){ 			if(G[i][j] == i){ 				cout<<"NO"<<endl<<i<<","<<i<<endl; 				return true; 			} 		} 	} 	return false; }

user: user-0
content: #define LOCAL #include<iostream> #include<vector> #include<queue> #include<cstring> //#include<stack> using namespace std;  const int MAXN = 25; const int MAXM = 2501; int n,m,inDegree[MAXN];//入度 vector<int> G[MAXN];//邻接表存储图 int vis[MAXN];//深度遍历访问标志  vector<int> circle;//存放DFS所找到的第一个环 int count = 0;//记录环的个数 vector<int> trace;//记录遍历路径  bool topologicalSort();//是否DAG，是则输出拓扑排序之一 //void topologicalPrint(); void circlePrint();//不是DAG，则输出DFS所找到的第一个环 bool isBiCircle();//是否存在自环，存在则输出 int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif memset(inDegree,0,sizeof(inDegree)); memset(vis,0,sizeof(vis)); cin>>n>>m; int from,to; for(int i = 0; i < m; i++){ cin>>from>>to; G[from].push_back(to); inDegree[to]++; } /*for(int i = 0 ; i <= n ; i++){ cout<<i<<":"; for(int j = 0 ; j < G[i].size() ; j++){ cout<<G[i][j]<<"-->"; } cout<<endl; }*/ if(isBiCircle()) return 0; if(topologicalSort()){ return 0; }else{ circlePrint(); //cout<<"wtf"; } return 0; } bool topologicalSort(){ int num = 0; queue<int> a; vector<int> print1; for(int i = 1; i <= n; i++){ if(inDegree[i] == 0){ a.push(i); } } while(!a.empty()){ int u = a.front(); print1.push_back(u); num++; a.pop(); for(int i = 0 ; i < G[u].size() ; i++){ int v = G[u][i]; inDegree[v]--; if(inDegree[v] == 0){ a.push(v); } } } if(num == n){ int first = 1; cout<<"YES"<<endl; for(int i = 0 ; i < print1.size() ; i++){ if(first){ first = 0; cout<<print1[i]; } else{ cout<<","<<print1[i]; } } cout<<endl; return true; } else return false; } void DFS(int u){ vis[u] = 1; trace.push_back(u); for(int i = 0; i < G[u].size() ; i++){ int v = G[u][i]; if(v == trace[0]){//找到环了 circle.assign(trace.begin(),trace.end());//把遍历路径以及最后定点加入环中 circle.push_back(v); return; } if(vis[v] == 0){ DFS(v); } } trace.pop_back(); } void circlePrint(){ for(int i = 1 ; i <= n; i++){ if(vis[i] == 0) DFS(i); } cout<<"NO"<<endl<<circle[0]; for(int i = 1 ; i < circle.size() ; i++) cout<<","<<circle[i]; cout<<endl;  } bool isBiCircle(){ for(int i = 1 ; i <= n ; i++){ for(int j = 0 ; j < G[i].size() ; j++){ if(G[i][j] == i){ cout<<"NO"<<endl<<i<<","<<i<<endl; return true; } } } return false; }

user: user-2
content: 各种排序算法动画演示http://www.webhek.com/misc/comparison-sort/

user: user-2
content: 百度搜相关关键字。

user: user-0

user: user-0
content: #define LOCAL #include<iostream> #include<vector> #include<queue> #include<cstring> //#include<stack> using namespace std;  const int MAXN = 25; const int MAXM = 2501; int n,m,inDegree[MAXN];//入度 vector<int> G[MAXN];//邻接表存储图 int vis[MAXN];//深度遍历访问标志  vector<int> circle;//存放DFS所找到的第一个环 int count = 0;//记录环的个数 vector<int> trace;//记录遍历路径  bool topologicalSort();//是否DAG，是则输出拓扑排序之一 //void topologicalPrint(); void circlePrint();//不是DAG，则输出DFS所找到的第一个环 bool isBiCircle();//是否存在自环，存在则输出 int main(){ #ifdef LOCAL 	freopen("in.txt","r",stdin); 	freopen("out.txt","w",stdout); #endif 	memset(inDegree,0,sizeof(inDegree)); 	memset(vis,0,sizeof(vis)); 	cin>>n>>m; 	int from,to; 	for(int i = 0; i < m; i++){ 		cin>>from>>to; 		G[from].push_back(to); 		inDegree[to]++; 	} 	/*for(int i = 0 ; i <= n ; i++){ 		cout<<i<<":"; 		for(int j = 0 ; j < G[i].size() ; j++){ 			cout<<G[i][j]<<"-->"; 		} 		cout<<endl; 	}*/ 	if(isBiCircle()) 		return 0; 	if(topologicalSort()){ 		return 0; 	}else{ 		circlePrint(); 		//cout<<"wtf"; 	} 	return 0; } bool topologicalSort(){ 	int num = 0; 	queue<int> a; 	vector<int> print1; 	for(int i = 1; i <= n; i++){ 		if(inDegree[i] == 0){ 			a.push(i); 		} 	} 	while(!a.empty()){ 		int u = a.front(); 		print1.push_back(u); 		num++; 		a.pop(); 		for(int i = 0 ; i < G[u].size() ; i++){ 			int v = G[u][i]; 			inDegree[v]--; 			if(inDegree[v] == 0){ 				a.push(v); 			} 		} 	} 	if(num == n){ 		int first = 1; 		cout<<"YES"<<endl; 		for(int i = 0 ; i < print1.size() ; i++){ 			if(first){ 				first = 0; 				cout<<print1[i]; 			} 			else{ 				cout<<","<<print1[i]; 			} 		} 		cout<<endl; 		return true; 	} 	else return false; } void DFS(int u){ 	vis[u] = 1; 	trace.push_back(u); 	for(int i = 0; i < G[u].size() ; i++){ 		int v = G[u][i]; 		if(v == trace[0]){//找到环了 			circle.assign(trace.begin(),trace.end());//把遍历路径以及最后定点加入环中 			circle.push_back(v); 			return; 		} 		if(vis[v] == 0){ 			DFS(v); 		} 	} 	trace.pop_back(); } void circlePrint(){ 	for(int i = 1 ; i <= n; i++){ 		if(vis[i] == 0) 			DFS(i); 	} 	cout<<"NO"<<endl<<circle[0]; 	for(int i = 1 ; i < circle.size() ; i++) 		cout<<","<<circle[i]; 	cout<<endl; 	 } bool isBiCircle(){ 	for(int i = 1 ; i <= n ; i++){ 		for(int j = 0 ; j < G[i].size() ; j++){ 			if(G[i][j] == i){ 				cout<<"NO"<<endl<<i<<","<<i<<endl; 				return true; 			} 		} 	} 	return false; }