码迷,mamicode.com
首页 > 其他好文 > 详细

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 F. Islands

时间:2017-09-10 21:44:49      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:and   public   because   read   make   tween   present   blog   repr   

 

On the mysterious continent of Tamriel, there is a great empire founded by human. To develope the trade, the East Empire Company is set up to transport goods from place to place. Recently, the company wants to start their business in Solstheim, which is consists of N islands. Luckily, there are already M sea routes. All routes are one-way, and the i-th route can transport person and goods from island u to v . Now, the company nominates you a particular job to plan some new routes to make sure that person and goods can be transported between any two islands. Furthermore, because the neighboring regions are under attack by an increasing number of dragons, limited resources can be used to set up new routes. So you should plan to build new routes as few as possible. Input Format The first line contains an integer T, indicating that there are T test cases. For each test case, the first line includes two integers N (N ≤ 10000) and M (M ≤ 100000), as described above. After that there are M lines. Each line contains two integers u and v . Output Format For each test case output one integer, represent the least number of routes required to new.

Sample Input

2

4 3

1 2

2 3

3 4

4 4

1 2

1 4

3 2

3 4

Sample Output

1

2
题意: 加最少的边,使有向图是一个强联通图;
思路: Tarjan算法求出强联通分量之后缩点成一个DAG图,a是DAG图入度为0的点数,b是DAG图出度为0的点数,
因为要为所有入度为0的点加入边,为所有出度为0的点加出边,所以至少要加的边数就是max(a, b);
要注意的是,当整个图原本就是强联通图时(即只有一个强联通分量时),不需要加边;

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>

using namespace std;
const int N = 20000 + 10;

vector<int> edge[N];
stack<int> S;

int in[N], out[N], scc[N];

class Trajan{
private:
    int dfn[N], low[N];
public:
    int  dfs_clock, scc_cnt;
    void DFS(int u){
        dfn[u] = low[u] = ++ dfs_clock;
        S.push( u );
        for(int i = edge[u].size() - 1; i >= 0; -- i){
            int v = edge[u][i];
            if(!dfn[v]){
                DFS( v );
                if(low[v] < low[u]) low[u] = low[v];

            }else if(!scc[v] && dfn[v] < low[u])
                low[u] = dfn[v];
        }
        if(low[u] == dfn[u]){
            ++ scc_cnt;
            while(true){
                int v = S.top(); S.pop();
                scc[v] = scc_cnt;
                if(v == u) break;
            }
        }
    }

    void Work_scc(int n){
        dfs_clock = scc_cnt = 0;
        for(int i = 0; i < N; ++ i)
            dfn[i] = low[i] = scc[i] = 0;

        for(int i = 1; i <= n; ++ i)
            if(!dfn[i]) DFS( i );

    }
}Tar;

void Solve_question(int n){
    for(int i = 1; i <= Tar.scc_cnt; ++ i)
        in[i] = out[i] = 0;

    for(int i = 1; i <= n; i++)
        for(int j = edge[i].size() - 1; j >= 0; -- j)
            if(scc[i] != scc[edge[i][j]])
                ++ out[scc[i]], ++ in[scc[edge[i][j]]];

    int in_cnt = 0, out_cnt = 0;
    for(int i = 1; i <= Tar.scc_cnt; ++i){
        if(!in[i]) ++ in_cnt;
        if(!out[i]) ++out_cnt;
    }
    printf("%d\n", Tar.scc_cnt > 1 ? max(in_cnt, out_cnt) : 0); //只有一个点时无需加边
}

void Input_data(int &n, int &m){
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++) edge[i].clear();
    int u, v;
    while(m --){
        scanf("%d %d", &u, &v);
        edge[u].push_back(v);
    }
}

int main(){
    int T, n, m;
    scanf("%d", &T);
    while(T --){
        Input_data(n, m);
        Tar.Work_scc( n );
        Solve_question( n );
    }
}

 

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 F. Islands

标签:and   public   because   read   make   tween   present   blog   repr   

原文地址:http://www.cnblogs.com/Pretty9/p/7502207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!