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

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 );
    }
}
时间: 2024-12-25 18:52:46

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 F. Islands的相关文章

2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5984 Pocky 题意 给出一根棒子(可以吃的)的长度x和切割过程中不能小于的长度d,每次随机的选取一个位置切开,吃掉左边的一半,对右边的棒子同样操作,直至剩余的长度不大于d时停止.现在给出x和d,问切割次数的数学期望是多少. 解题思路 当看到第二个样例2 1时,结果是1.693147,联想到ln

2014ACM/ICPC亚洲区鞍山赛区现场赛——题目重现

2014ACM/ICPC亚洲区鞍山赛区现场赛--题目重现 题目链接 5小时内就搞了5题B.C.D.E,I. H题想到要打表搞了,可惜时间不够,后面打出表试了几下过了- - K题过的人也比较多,感觉是个几何旋转+ploya,但是几何实在不行没什么想法 B:这题就是一个大模拟,直接数组去模拟即可,注意细节就能过 C:类似大白上一题红蓝三角形的, 每个数字找一个互质和一个不互质个数,除掉重复就直接除2,然后总的C(n, 3)减去即可,问题在怎么处理一个数字互质和不互质的,其实只要处理出不互质的即可,这

ICPC 2018 徐州赛区网络赛

ACM-ICPC 2018 徐州赛区网络赛 ?去年博客记录过这场比赛经历:该死的水题 ?一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进. ? ? D. Easy Math 题意: ? 给定 \(n\), \(m\) ,求 \(\sum _{i=1}^{m} \mu(in)\) .其中 $ 1 \le n \le 1e12$ , $ 1 \le m \le 2e9$ ,\(\mu(n)\) 为莫比乌斯函数. ? 思路: ? 容易知道,\(i\) 与 \(n\) 不互质时, \(\m

2016 ACM/ICPC亚洲区大连站-重现赛 解题报告

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5979 按AC顺序: I - Convex Time limit    1000 ms Memory limit 65536 kB OS Windows We have a special convex that all points have the same distance to origin point. As you know we can get N segments after linki

2014ACM/ICPC亚洲区鞍山赛区现场赛1009Osu!

鞍山的签到题,求两点之间的距离除以时间的最大值.直接暴力过的. A - Osu! Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 5078 Appoint description:  System Crawler  (2014-10-22) Description Osu! is a very popular music game. B

hdu 4041 2011北京赛区网络赛F 组合数+斯特林数 ***

插板法基础知识 斯特林数见百科 1 #include<iostream> 2 #include<cmath> 3 #include<cstdio> 4 #include<cstring> 5 #define LL long long 6 #define eps 1e-7 7 #define MOD 1000000007 8 using namespace std; 9 int c[2001][2001]={1},stir2[1005][1005]={1};

hdu 4036 2011成都赛区网络赛F 模拟 **

为了确保能到达终点,我们需要满足下面两个条件 1.能够到达所有山顶 2.能够在遇到苦土豆时速度大于他 二者的速度可以用能量守恒定律做,苦土豆的坐标可通过三角形相似性来做 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<map> using na

hdu 4026 2011上海赛区网络赛F TSP ****

没看过TSP,先mark 1 //4838039 2011-10-27 23:04:15 Accepted 4026 2343MS 31044K 3143 B C++ Geners 2 //状态压缩DP的TSP问题 3 //优先级位运算小于判等 , 还有各种细节各种出错 4 #include <cstdio> 5 #include <cstring> 6 #include <stdlib.h> 7 #define mabs(a) (a>0?a:-(a)) 8 9

hdu 4006 优先队列 2011大连赛区网络赛F **

签到题都要想一会 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 1000000007 10 const int INF=0x3f3