コード例 #1
0
int main(){

	scanf("%d%d", &noV, &noE);

	G = VVI(noV+1, VI(0, 0));
	RG = VVI(noV+1, VI(0, 0));
	ans = VVI(noV+1, VI(0,0));
	vis = VB(noV+1, false);

	for(int i = 0 ; i < noE ; i++){
		int u,v;
		scanf("%d%d", &u, &v);
		G[u].push_back(v);
		RG[v].push_back(u);
	}

	for(int i = 1 ; i <= noV ; i++){
		if(!vis[i]){
			vis[i] = true;
			dfs(i);
			st.push(i);
		}
	}

	vis = VB(noV+1, false);
	vInfoAns = VI(noV+1, -1);
	while(!st.empty()){
		int here = st.top(); st.pop();

		if(!vis[here]){
			vis[here] = true;
			ans[noAns].push_back(here);
			vInfoAns[here] = noAns;
			dfsReverse(here);
			noAns++;
			sort(ans[noAns-1].begin(), ans[noAns-1].end());
		}
	}

	vis = VB(noAns+1, false);
	printf("%d\n", noAns);
	for(int i = 1 ; i <= noV ; i++){
		int here = vInfoAns[i]; // == noAns
		if(!vis[here]){
			vis[here] = true;
			for(auto iter = ans[here].begin() ; iter != ans[here].end() ; iter++){
				printf("%d ",*iter);
			}
			puts("-1");
		}
	}

	return 0;
}
void Graph::topologicalSort(int src, bool *visit, SI &s){
	visit[src] = true;
	LN::iterator iter = adj[src].begin();
	while(iter != adj[src].end()){
		if(!visit[(*iter).v]){
			topologicalSort((*iter).v, visit, s);
		}
		iter++;
	}
	s.push(src);
}
コード例 #3
0
void dfs(int vertex){
	for(auto iter = G[vertex].begin() ; iter != G[vertex].end() ; iter++){
		int here = *iter;

		if(!vis[here]){
			vis[here] = true;
			dfs(here);
			st.push(here);
		}

	}
	return ;
}
コード例 #4
0
int main(int argc, char const *argv[])
{
	int n, c, s;
	
	while(SC2(n, k) && n)
	{	
		int lt = 0;	
		ZERO(in); ZERO(out);
		
		for(int i = 1; i <= n; i++)
		{
			SC2(c, s);	
			in[c] = i;
			out[s] = i;
			lt = max(lt, s+1);
		}
		
		while(!p.empty()) 
			p.pop();
		
		int i;
		
		for(i = 1; i < lt; i++)
		{
			if(out[i]){
				if(p.empty() || p.top() != out[i]) break;
				else p.pop();
			}
			
			if(in[i]){
				if((int)p.size() < k) p.push(in[i]);
				else break;
			}
		}

		printf("%s\n", i == lt ? "Sim" : "Nao");
	}
}