HDU 5154 Harry and Magical Computer (强连通分量)

#include<stack>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int maxn=1005;
vector<int>G[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
stack<int>S;
int n,m;

void dfs(int u)//大白的板子
{
    pre[u]=lowlink[u]=++dfs_clock;
    S.push(u);
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(!pre[v])
        {
            dfs(v);
            lowlink[u]=min(lowlink[u],lowlink[v]);
        }
        else if(!sccno[v])
        {
            lowlink[u]=min(lowlink[u],pre[v]);
        }
    }
    if(lowlink[u]==pre[u])
    {
        scc_cnt++;
        for(;;)
        {
            int x=S.top();  S.pop();
            sccno[x]=scc_cnt;
            if(x==u)    break;
        }
    }
}

void find_scc(int n)
{
    dfs_clock=scc_cnt=0;
    memset(sccno,0,sizeof(sccno));
    memset(pre,0,sizeof(pre));
    for(int i=1;i<=n;i++)
    {
        if(!pre[i])
        {
            dfs(i);
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        while(!S.empty())   S.pop();
        for(int i=0;i<=n;i++)   G[i].clear();
        memset(lowlink,0,sizeof(lowlink));
        dfs_clock=0;
        scc_cnt=0;
        int flag=0;
        for(int i=1;i<=m;i++)
        {
            int x,y;    scanf("%d%d",&x,&y);
            if(x==y)    flag=1;
            G[x].push_back(y);
        }
        find_scc(n);
        if(scc_cnt==n&&flag!=1)  printf("YES\n");<span style="white-space:pre">	</span>// 考虑一种特殊情况 5 1 1 1
        else printf("NO\n");<span style="white-space:pre">				</span>//这种情况下强连通分量也是5个 但存在环(自环)   直接特判
        //printf("%d\n",scc_cnt);
    }
}
<h1 style="color: rgb(26, 92, 200); text-align: center; font-family: "Times New Roman";">Harry and Magical Computer</h1><span style="font-family: "Times New Roman";font-size:14px; text-align: -webkit-center;"><strong><span style="font-family: Arial; font-size: 12px; color: green;">Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2447    Accepted Submission(s): 945
</span></strong></span><br style="font-family: "Times New Roman";font-size:14px; text-align: -webkit-center;" /><br style="font-family: "Times New Roman";font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background: url("panel-title.png") left bottom no-repeat transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold;">Problem Description</div><div class="panel_content" style="height: auto; background: url("panel-content.png") repeat-y; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: "Times New Roman";">In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal with a process, it will work until the ending of the processes. One day the computer got n processes to deal with. We number the processes from 1 to n. However there are some dependencies between some processes. When there exists a dependencies (a, b), it means process b must be finished before process a. By knowing all the m dependencies, Harry wants to know if the computer can finish all the n processes.</div><div class="panel_bottom" style="height: auto; margin: 0px; background: url("panel-bottom.png") left top no-repeat; font-family: "Times New Roman";font-size:14px; text-align: -webkit-center;"> </div><br style="font-family: "Times New Roman";font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background: url("panel-title.png") left bottom no-repeat transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold;">Input</div><div class="panel_content" style="height: auto; background: url("panel-content.png") repeat-y; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: "Times New Roman";">There are several test cases, you should process to the end of file.
For each test case, there are two numbers n m on the first line, indicates the number processes and the number of dependencies. <span class="MathJax_Preview" style="color: rgb(136, 136, 136);"></span><span class="MathJax" id="MathJax-Element-1-Frame" role="textbox" aria-readonly="true" style="display: inline; line-height: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px;"><nobr style="transition: none; border: 0px; padding: 0px; margin: 0px; max-width: none; max-height: none; min-width: 0px; min-height: 0px; vertical-align: 0px; line-height: normal;"><span class="math" id="MathJax-Span-1" style="transition: none; display: inline-block; position: static; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; width: 12.625em;"><span style="transition: none; display: inline-block; position: relative; border-top: 0px; border-right: 0px; border-bottom: 0px; border-left-style: initial; border-left-color: initial; border-image: initial; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; width: 12.209em; height: 0px; font-size: 14.42px; border-left-width: 0px !important;"><span style="transition: none; position: absolute; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; clip: rect(1.807em 1000em 3.124em -0.551em); top: -2.701em; left: 0.003em;"><span class="mrow" id="MathJax-Span-2" style="transition: none; display: inline; position: static; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal;"><span class="mn" id="MathJax-Span-3" style="transition: none; display: inline; position: static; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">1</span><span class="mo" id="MathJax-Span-4" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">≤</span><span class="mi" id="MathJax-Span-5" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Math-italic;">n</span><span class="mo" id="MathJax-Span-6" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">≤</span><span class="mn" id="MathJax-Span-7" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">100</span><span class="mo" id="MathJax-Span-8" style="transition: none; display: inline; position: static; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">,</span><span class="mn" id="MathJax-Span-9" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.142em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">1</span><span class="mo" id="MathJax-Span-10" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">≤</span><span class="mi" id="MathJax-Span-11" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Math-italic;">m</span><span class="mo" id="MathJax-Span-12" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">≤</span><span class="mn" id="MathJax-Span-13" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">10000</span></span><span style="transition: none; display: inline-block; position: static; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; width: 0px; height: 2.708em;"></span></span></span><span style="transition: none; display: inline-block; position: static; border-width: 0px 0px 0px 0.004em; border-top-style: initial; border-right-style: initial; border-bottom-style: initial; border-left-style: solid; border-color: initial; border-image: initial; padding: 0px; margin: 0px; vertical-align: -0.282em; line-height: normal; overflow: hidden; width: 0px; height: 1.004em;"></span></span></nobr></span>
The next following m lines, each line contains two numbers a b, indicates a dependencies (a, b). <span class="MathJax_Preview" style="color: rgb(136, 136, 136);"></span><span class="MathJax" id="MathJax-Element-2-Frame" role="textbox" aria-readonly="true" style="display: inline; line-height: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px;"><nobr style="transition: none; border: 0px; padding: 0px; margin: 0px; max-width: none; max-height: none; min-width: 0px; min-height: 0px; vertical-align: 0px; line-height: normal;"><span class="math" id="MathJax-Span-14" style="transition: none; display: inline-block; position: static; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; width: 5.274em;"><span style="transition: none; display: inline-block; position: relative; border-top: 0px; border-right: 0px; border-bottom: 0px; border-left-style: initial; border-left-color: initial; border-image: initial; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; width: 5.135em; height: 0px; font-size: 14.42px; border-left-width: 0px !important;"><span style="transition: none; position: absolute; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; clip: rect(1.807em 1000em 3.124em -0.551em); top: -2.701em; left: 0.003em;"><span class="mrow" id="MathJax-Span-15" style="transition: none; display: inline; position: static; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal;"><span class="mn" id="MathJax-Span-16" style="transition: none; display: inline; position: static; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">1</span><span class="mo" id="MathJax-Span-17" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">≤</span><span class="mi" id="MathJax-Span-18" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Math-italic;">a</span><span class="mo" id="MathJax-Span-19" style="transition: none; display: inline; position: static; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-20" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.142em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Math-italic;">b</span><span class="mo" id="MathJax-Span-21" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Main;">≤</span><span class="mi" id="MathJax-Span-22" style="transition: none; display: inline; position: static; border: 0px; padding: 0px 0px 0px 0.281em; margin: 0px; vertical-align: 0px; line-height: normal; font-family: MathJax_Math-italic;">n</span></span><span style="transition: none; display: inline-block; position: static; border: 0px; padding: 0px; margin: 0px; vertical-align: 0px; line-height: normal; width: 0px; height: 2.708em;"></span></span></span><span style="transition: none; display: inline-block; position: static; border-width: 0px 0px 0px 0.004em; border-top-style: initial; border-right-style: initial; border-bottom-style: initial; border-left-style: solid; border-color: initial; border-image: initial; padding: 0px; margin: 0px; vertical-align: -0.282em; line-height: normal; overflow: hidden; width: 0px; height: 1.075em;"></span></span></nobr></span></div><div class="panel_bottom" style="height: auto; margin: 0px; background: url("panel-bottom.png") left top no-repeat; font-family: "Times New Roman";font-size:14px; text-align: -webkit-center;"> </div><br style="font-family: "Times New Roman";font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background: url("panel-title.png") left bottom no-repeat transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold;">Output</div><div class="panel_content" style="height: auto; background: url("panel-content.png") repeat-y; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: "Times New Roman";">Output one line for each test case. 
If the computer can finish all the process print "YES" (Without quotes).
Else print "NO" (Without quotes).</div><div class="panel_bottom" style="height: auto; margin: 0px; background: url("panel-bottom.png") left top no-repeat; font-family: "Times New Roman";font-size:14px; text-align: -webkit-center;"> </div><br style="font-family: "Times New Roman";font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background: url("panel-title.png") left bottom no-repeat transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold;">Sample Input</div><div class="panel_content" style="height: auto; background: url("panel-content.png") repeat-y; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: "Times New Roman";"><pre style="word-wrap: break-word; white-space: pre-wrap; margin-top: 0px; margin-bottom: 0px;"><div style="font-family: "Courier New", Courier, monospace;">3 2
3 1
2 1
3 3
3 2
2 1
1 3</div>
 

Sample Output
  
  
YES NO
 

Source
 

Recommend
heyang   |   We have carefully selected several similar problems for you:   5981  5980  5979  5978  5977 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值