Legal or Not(模板题)

本来以为这题能用并查集做的,但一想不对

例如A-> B,A->C如果用并查集的话B与C就不能连了,但实际B可以是C的徒弟,所以这题是考拓扑排序。

#include<stdio.h>
#include<string.h>
int d[105],map[105][105],vis[105];
int main()
{
    int i,j,k,f,n,m,u,v;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0) break;
        memset(d,0,sizeof(d));
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        for(i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
            if(!map[u][v])//这里很重要,因为可能有输入重复的点
            {
                map[u][v]=1;
                d[v]++;
            }
        }
        for(i=0; i<n; i++)
        {
            f=0;
            for(j=0; j<n; j++)
            {
                if(d[j]==0&&vis[j]==0)//用于处理存在好几个入度为0的节点
                {
                    vis[j]=1;
                    for(k=0; k<n; k++)
                    if(map[j][k])
                    d[k]--;
                    f=1;
                    break;//找到一个入度为0的就结束
                }
            }
            if(f==0)//如果节点未输出完,而图中就没有了入度为0的节点,则说明有环,不合法。
            break;
        }
        if(f==0)
        printf("NO\n");
        else
        printf("YES\n");
    }
}

 

Legal or Not(模板题)

时间: 2024-08-05 23:40:47

Legal or Not(模板题)的相关文章

hdu 2966 In case of failure kdtree模板题

问求每个点距离平方的最小的点 kd-tree模板题…… 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(x) cerr<<#x<<"=="<<(x)<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int>

几道树剖模板题

寒假后半段一直都在外出旅游..颓了好久..qaq 旅游期间写了几道树剖模板题,贴上来.. BZOJ 1036 没啥好说的,裸题 1 #include <cstdio> 2 #include <algorithm> 3 4 #define LEFT (segt[cur].l) 5 #define RIGHT (segt[cur].r) 6 #define MID (segt[cur].mid) 7 #define SUM (segt[cur].Sum) 8 #define MAX (

poj3630 Phone List (trie树模板题)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

洛谷P3381——费用流模板题

嗯..随便刷了一道费用流的模板题....来练练手. #include<iostream> #include<cstdio> #include<cstring> using namespace std; int h[5210],d[5210],used[5210],que[100010],last[5210]; int k=1,INF=0x7fffffff,ans1=0,ans2=0; inline int read(){ int t=1,num=0; char c=ge

HDU 1251 Trie树模板题

1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b #define F(i,a,b

LA 4670 出现次数最多的子串 (AC自动机模板题)

Dominating Patterns Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; ea

【POJ 2104】 K-th Number 主席树模板题

达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没有评测,但我立下flag这个代码一定能A.我的同学在自习课上考语文,然而机房党都跑到机房来避难了\(^o^)/~ #include<cstdio> #include<cstring> #include<algorithm> #define for1(i,a,n) for(i

hdu5384 AC自动机模板题,统计模式串在给定串中出现的个数

http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation&q