• HDU——T 3342 Legal or Not


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

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 9122    Accepted Submission(s): 4230


    Problem Description
    ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?

    We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not. 

    Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.
     
    Input
    The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. The input is terminated by N = 0.
    TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
     
    Output
    For each test case, print in one line the judgement of the messy relationship.
    If it is legal, output "YES", otherwise "NO".
     
    Sample Input
    3 2 0 1 1 2 2 2 0 1 1 0 0 0
     
    Sample Output
    YES NO
     
    Author
    QiuQiu@NJFU
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1285 2647 3333 3339 3341 
     
     
    存在环就说明关系乱了、
     
     1 #include <algorithm>
     2 #include <cstring>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 const int N(10000+5);
     8 const int M(30000+5);
     9 int head[N],sumedge;
    10 struct Edge
    11 {
    12     int v,next;
    13     Edge(int v=0,int next=0):v(v),next(next){}
    14 }edge[M];
    15 inline void ins(int u,int v)
    16 {
    17     edge[++sumedge]=Edge(v,head[u]);
    18     head[u]=sumedge;
    19 }
    20 
    21 int tim,dfn[N],low[N];
    22 int top,Stack[N],instack[N];
    23 int sumcol,col[N],point[N];
    24 void DFS(int now)
    25 {
    26     low[now]=dfn[now]=++tim;
    27     Stack[++top]=now;instack[now]=1;
    28     for(int v,i=head[now];i;i=edge[i].next)
    29     {
    30         v=edge[i].v;
    31         if(!dfn[v]) DFS(v),low[now]=min(low[now],low[v]);
    32         else if(instack[v]) low[now]=min(low[now],dfn[v]);
    33     }
    34     if(low[now]==dfn[now])
    35     {
    36         col[now]=++sumcol;
    37         point[sumcol]++;
    38         for(;Stack[top]!=now;top--)
    39         {
    40             col[Stack[top]]=sumcol;
    41             point[sumcol]++;
    42             instack[Stack[top]]=0;
    43         }
    44         top--; instack[now]=0;
    45     }
    46 }
    47 
    48 bool jud()
    49 {
    50     for(int i=1;i<=sumcol;i++)
    51         if(point[i]>1) return false;
    52     return true;
    53 }
    54 
    55 int AC()
    56 {
    57     for(int n,m;scanf("%d%d",&n,&m)&&n;)
    58     {
    59         for(int u,v;m--;)
    60         scanf("%d%d",&u,&v),ins(u,v);
    61         DFS(1);
    62         if(jud()) puts("YES");
    63         else puts("NO");
    64         memset(instack,0,sizeof(instack));
    65         memset(point,0,sizeof(point));
    66         memset(Stack,0,sizeof(Stack));
    67         memset(edge,0,sizeof(edge));
    68         memset(head,0,sizeof(head));
    69         memset(low,0,sizeof(low));
    70         memset(dfn,0,sizeof(dfn));
    71         memset(col,0,sizeof(col));
    72         sumedge=sumcol=tim=top=0;
    73     }
    74     return 0;
    75 }
    76 
    77 int I_want_AC=AC();
    78 int main() {;}
    在不知廉耻的附上只A掉样例的Tarjan

    Top_sort

     1 #include <algorithm>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <queue>
     5 
     6 using namespace std;
     7 
     8 const int N(110);
     9 int head[N],sumedge;
    10 struct Edge
    11 {
    12     int v,next;
    13     Edge(int v=0,int next=0):v(v),next(next){}
    14 }edge[N];
    15 inline void ins(int u,int v)
    16 {
    17     edge[++sumedge]=Edge(v,head[u]);
    18     head[u]=sumedge;
    19 }
    20 
    21 int AC()
    22 {
    23     for(int ans,n,m,rd[N];scanf("%d%d",&n,&m)&&n;)
    24     {
    25         ans=n;
    26         queue<int>que;sumedge=0;
    27         memset(rd,0,sizeof(rd));
    28         memset(head,0,sizeof(head));
    29         memset(edge,0,sizeof(edge));
    30         for(int u,v;m--;)
    31         {
    32             scanf("%d%d",&u,&v);
    33             ins(u+1,v+1); rd[v+1]++;
    34         }
    35         for(int i=1;i<=n;i++)
    36             if(!rd[i]) que.push(i);
    37         for(int u,v;!que.empty();)
    38         {
    39             ans--;
    40             u=que.front();que.pop();
    41             for(int i=head[u];i;i=edge[i].next)
    42             {
    43                 v=edge[i].v;
    44                 if(--rd[v]==0) que.push(v);
    45             }
    46         }
    47         if(!ans) puts("YES");
    48         else puts("NO");
    49     }
    50     return 0;
    51 }
    52 int I_want_AC=AC();
    53 
    54 int main() {;}
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    mac java 环境设置
    当你打开网页的时候,世界都发生了什么(1)
    不同场景下 MySQL 的迁移方案
    10分钟学会前端调试利器——FireBug
    中华人民共和国专利法
    【过时】博客园中使用syntaxhighlighter插件(图文详细版本)
    微软系列的网站小集合
    VS代码提示不出现或者提示变成英文或者各种奇葩问题的解决
    Linux基础命令
    【QQ技术】群文件报毒怎样下载?~ 变相绕过QQ复杂检验过程
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7419373.html
Copyright © 2020-2023  润新知