zoukankan      html  css  js  c++  java
  • uva 11504 强连通分量 && 缩点

    Problem D: Dominos

    Dominos are lots of fun. Children like to stand the tiles on their side in long lines. When one domino falls, it knocks down the next one, which knocks down the one after that, all the way down the line. However, sometimes a domino fails to knock the next one down. In that case, we have to knock it down by hand to get the dominos falling again.

    Your task is to determine, given the layout of some domino tiles, the minimum number of dominos that must be knocked down by hand in order for all of the dominos to fall.

    Input Specification

    The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing two integers, each no larger than 100 000. The first integer n is the number of domino tiles and the second integer m is the number of lines to follow in the test case. The domino tiles are numbered from 1 to n. Each of the following lines contains two integers x and y indicating that if domino number x falls, it will cause domino number y to fall as well.

    Sample Input

    1
    3 2
    1 2
    2 3
    

    Output Specification

    For each test case, output a line containing one integer, the minimum number of dominos that must be knocked over by hand in order for all the dominos to fall.

    Output for Sample Input

    1
    

    Ondřej Lhoták

    求出强连通分量,然后缩圈为点,再数入度为零的点的个数、

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    
    using namespace std;
    
    #define LL long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define cint const int
    
    #define MAXN 111111
    vector<int> g[MAXN];
    int ind[MAXN], v[MAXN], n, m;
    int dfn[MAXN], low[MAXN], cnt, tsp;
    bool ins[MAXN];
    stack<int> s;
    
    void tarjan(int u){
        ins[u] = true;  s.push(u);
        low[u] = dfn[u] = ++tsp;
        for(int i=0; i<g[u].size(); i++){
            int v = g[u][i];
            if(!dfn[v]){
                tarjan(v);
                low[u] = min(low[u], low[v]);
            }
            else if(ins[v]) low[u] = min(low[u], dfn[v]);
        }
        if(dfn[u]==low[u]){
            int x;      cnt++;
            do{
                x = s.top();    s.pop();
                ins[x] = false;
                v[x] = cnt;
            }while(x!=u);
        }
    }
    
    void solve(){
        int i, j;
        cnt = tsp = 0;
        fill_n(dfn+1, n, 0);
        fill_n(ins+1, n, false);
    
        for(i=1; i<=n; i++) if(!dfn[i])
            tarjan(i);
        fill_n(ind+1, n, 0);
        for(i=1; i<=n; i++)
            for(j=0; j<g[i].size(); j++){
                int x = g[i][j];
                if(v[x] != v[i]) ind[v[x]]++;
            }
        int ans = 0;
        for(i=1; i<=cnt; i++) if(!ind[i])
            ans++;
        printf("%d
    ", ans);
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int T;
        scanf(" %d", &T);
        while(T--){
            scanf(" %d %d", &n, &m);
            int i, x, y;
            for(i=1; i<=n; i++) g[i].clear();
            for(i=0; i<m; i++){
                scanf(" %d %d", &x, &y);
                g[x].push_back(y);
            }
            solve();
        }
        return 0;
    }
    
  • 相关阅读:
    常用的dos命令
    定时器
    自动化工具下载地址
    Eclipse自动提示
    An error occurred: No action handlers found
    生产消费的经典案例
    SpringBoot 优雅的参数效验
    40 个 SpringBoot 常用注解
    极简入门,Shiro的认证与授权流程解析
    Java多线程批量处理、线程池的使用
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3379811.html
Copyright © 2011-2022 走看看