1. 程式人生 > >2017 ACM-ICPC 亞洲區(烏魯木齊賽區)網絡賽 F. Islands

2017 ACM-ICPC 亞洲區(烏魯木齊賽區)網絡賽 F. Islands

div new question esc tin vector ace 所有 const

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