• 环路关系hdu 3342 拓扑排序


    发一下牢骚和主题无关:

        http://acm.hdu.edu.cn/showproblem.php?pid=3342

        目题意大:在n个人间中存在m条师徒关系,且师徒关系拥有递传效应,判断这些关系否是理合;

        思绪1:若关系不理合,必定存在环路。用最短路的floyd算法找出全部可以联系的人,如果一个人与自己有联系,则明说存环路。

        思绪2:对n个点按给出的关系停止拓扑序排,若序排实现则明说不存在环路,否则存在环路。

        方法一:floyd

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    using namespace std;
    int map[101][101];
    void floyd(int n)
    {
    	int i,j,k;
    	for(k=0;k<n;k++){
    		for(i=0;i<n;i++){
    			for(j=0;j<n;j++){
    				if(map[i][k]&&map[k][j])
    					map[i][j]=1;
    			}
    		}
    	}
    }
    int main()
    {
    	int i,n,m,a,b,flag;
    	while(scanf("%d%d",&n,&m),n){
    		flag=1;
    		memset(map,0,sizeof(map));
    		for(i=0;i<m;i++){
    			scanf("%d%d",&a,&b);
    			if(a!=b)
    				map[a][b]=1;
    		}
    		floyd(n);
    		for(i=0;i<n;i++){
    			if(map[i][i]){
    				flag=0;break;
    			}
    		}
    		if(flag) printf("YES\n");
    		else printf("NO\n");
    	}
    	return 0;
    }
        每日一道理
    虽然你现在还只是一株稚嫩的幼苗。然而只要坚韧不拔,终会成为参天大树;虽然你现在只是涓涓细流,然而只要锲而不舍,终会拥抱大海;虽然你现在只是一只雏鹰,然而只要心存高远,跌几个跟头之后,终会占有蓝天。

        方法二:toposort

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    using namespace std;
    int map[101][101],in[101];
    int toposort(int n)
    {
    	int i,j,k,cnt=0;
    	for(i=0;i<n;i++){
    		for(j=0;j<n;j++){
    			if(in[j]==0){
    				in[j]--;
    				cnt++;
    				if(cnt==n) return cnt;
    				for(k=0;k<n;k++){
    					if(map[j][k])
    						in[k]--;
    				}
    			}
    		}
    	}
    	return cnt;
    }
    int main()
    {
    	int n,m,i,a,b;
    	while(scanf("%d%d",&n,&m),n){
    		memset(map,0,sizeof(map));
    		memset(in,0,sizeof(in));
    		for(i=0;i<m;i++){
    			scanf("%d%d",&a,&b);
    			if(a!=b && map[a][b]==0){
    				map[a][b]=1;
    				in[b]++;
    			}
    		}
    		if(toposort(n)==n) puts("YES");
    		else puts("NO");
    	}
    	return 0;
    }

    文章结束给大家分享下程序员的一些笑话语录: 《诺基亚投资手机浏览器UCWEB,资金不详或控股》杯具了,好不容易养大的闺女嫁外国。(心疼是你养的吗?中国创业型公司创业初期哪个从国有银行贷到过钱?)

  • 相关阅读:
    [LeetCode] 314. Binary Tree Vertical Order Traversal
    [LeetCode] 139. Word Break
    [LeetCode] 540. Single Element in a Sorted Array
    [LeetCode] 1443. Minimum Time to Collect All Apples in a Tree
    [LeetCode] 1442. Count Triplets That Can Form Two Arrays of Equal XOR
    [LeetCode] 1441. Build an Array With Stack Operations
    [LeetCode] 277. Find the Celebrity
    [LeetCode] 1232. Check If It Is a Straight Line
    [LeetCode] 10. Regular Expression Matching
    [LeetCode] 1192. Critical Connections in a Network
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3047889.html
Copyright © 2020-2023  润新知