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 NN islands.

Luckily, there are already MM sea routes.

All routes are one-way, and the ii-th route can transport person and goods from island u_iu
​i
​​ to v_iv
​i
​​ .

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 TT, indicating that there are TT test cases.

For each test case, the first line includes two integers N~(N \leq 10000)N (N≤10000) and M~(M \leq 100000)M (M≤100000), as described above.

After that there are MM lines. Each line contains two integers u_iu
​i
​​ and v_iv
​i
​​ .

Output Format

For each test case output one integer, represent the least number of routes required to new.

样例输入

2
4 3
1 2
2 3
3 4
4 4
1 2
1 4
3 2
3 4
样例输出

1
2
题目来源

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

题意:给你一个有向图,问最少要添加多少条有向边使图中任意两点可以相互到达。

解题思路:强联通分量 + 缩点 后得到一个有向无环图DAG。然后求这个DAG每个点的入度f[] 和 出度 d[],然后 all :DAG中出度为0点的个数;ans : DAG中入度为0点的个数。最后答案 = max(all,ans)。注意:当只有一个强联通分量时不用添加边。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e4 + 5;
int vis[maxn],low[maxn],dfn[maxn],in[maxn],f[maxn],d[maxn];
vector<int> G[maxn * 10];
stack<int> st; 
int n,m,ind,cnt;

void Init(){
    ind = cnt = 0;
    for(int i = 0;i <= n;i++) G[i].clear();
    memset(vis,0,sizeof(vis));
    memset(in,0,sizeof(in));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(f,0,sizeof(f));
    memset(d,0,sizeof(d));
}

void dfs(int x){//求联通分量 + 缩点。
    low[x] = dfn[x] = ++ind;
    st.push(x),vis[x] = 1;
    for(int i = 0;i < G[x].size();i++){
        int v = G[x][i];
        if(!dfn[v]){
            dfs(v);
            low[x] = min(low[x],low[v]);
        }
        else if(vis[v]){
            low[x] = min(low[x],dfn[v]);
        }
    }
    if(low[x] == dfn[x]){
        cnt++;
        while(!st.empty()){
            int tot = st.top();
            st.pop();
            vis[tot] = 0;
            in[tot] = cnt;//在同一连通分量缩成一点 cnt。(同时cnt的值也等于图中连通分量的个数)
            if(tot == x) break;
        }
    }
}

void slove(){//求缩点后,每个连通分量的 入度 f[] 和 出度 d[]。
    for(int i = 1;i <= n;i++){
        for(int j = 0;j < G[i].size();j++){
            int v = G[i][j];
            if(in[i] != in[v]){
                d[in[i]]++;
                f[in[v]]++;
            }
        }
    }
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        Init();
        int x,y;
        scanf("%d %d",&n,&m);
        for(int i = 1;i <= m;i++){
            scanf("%d %d",&x,&y);
            G[x].push_back(y);
        }
        for(int i = 1;i <= n;i++){
            if(!dfn[i]) dfs(i);
        }
        if(cnt == 1){
            printf("0\n");
            continue;
        }
        slove();
        int all,ans;
        all = ans = 0;
        for(int i = 1;i <= cnt;i++){
            if(d[i] == 0) all++;
            if(f[i] == 0) ans++;
        }
        printf("%d\n",max(ans,all));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值