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 = + ; 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() - ; i >= ; -- 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 = ;
for(int i = ; i < N; ++ i)
dfn[i] = low[i] = scc[i] = ; for(int i = ; i <= n; ++ i)
if(!dfn[i]) DFS( i ); }
}Tar; void Solve_question(int n){
for(int i = ; i <= Tar.scc_cnt; ++ i)
in[i] = out[i] = ; for(int i = ; i <= n; i++)
for(int j = edge[i].size() - ; j >= ; -- j)
if(scc[i] != scc[edge[i][j]])
++ out[scc[i]], ++ in[scc[edge[i][j]]]; int in_cnt = , out_cnt = ;
for(int i = ; i <= Tar.scc_cnt; ++i){
if(!in[i]) ++ in_cnt;
if(!out[i]) ++out_cnt;
}
printf("%d\n", Tar.scc_cnt > ? max(in_cnt, out_cnt) : ); //只有一个点时无需加边
} void Input_data(int &n, int &m){
scanf("%d %d", &n, &m);
for(int i = ; 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的更多相关文章

  1. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...

  2. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)

    参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...

  3. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】

    2017 ACM-ICPC 亚洲区(南宁赛区)网络赛  M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...

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

    摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...

  5. ICPC 2018 徐州赛区网络赛

    ACM-ICPC 2018 徐州赛区网络赛  去年博客记录过这场比赛经历:该死的水题  一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进.     D. Easy Math 题意:   ...

  6. 【2017 ACM/ICPC 乌鲁木齐赛区网络赛环境测试赛 E】蒜头君的排序

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 莫队算法+树状数组. 区间增加1或减少1. 对逆序对的影响是固定的. (用冒泡排序变成升序的交换次数,就是逆序对的个数) [错的次数] 0 [ ...

  7. 2017 乌鲁木齐赛区网络赛 J Our Journey of Dalian Ends 费用流

    题目描述: Life is a journey, and the road we travel has twists and turns, which sometimes lead us to une ...

  8. [刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题

    第一次玩ACM...有点小紧张小兴奋.这题目好难啊,只是网赛就这么难...只把最简单的两题做出来了. 题目1: 代码: //#define _ACM_ #include<iostream> ...

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

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5979 按AC顺序: I - Convex Time limit    1000 ms Memory li ...

随机推荐

  1. 使用svn在github上下载文件夹

    今天想在github上下载mybatis-generator的eclipse插件,可是如何在github上下载一个文件夹而不用把这个项目clone呢,搜了一下,发现可以直接用svn来下载 只需将将路径 ...

  2. [CF666E]Forensic Examination:后缀自动机+线段树合并

    分析 用到了两个小套路: 使用线段树合并维护广义后缀自动机的\(right\)集合. 查询\(S[L,R]\)在\(T\)中的出现次数:给\(T\)建SAM,在上面跑\(S\),跑到\(R\)的时候先 ...

  3. flink⼿手动维护kafka偏移量量

    flink对接kafka,官方模式方式是自动维护偏移量 但并没有考虑到flink消费kafka过程中,如果出现进程中断后的事情! 如果此时,进程中段: 1:数据可能丢失 从获取了了数据,但是在执⾏行行 ...

  4. [CSP-S模拟测试]:花(DP)

    题目传送门(内部题111) 输入格式 一个整数$T$,表示测试数据组数. 每组测试数据占一行,两个整数,分别表示$L$和$S$. 输出格式 对每组数据,输出一个整数表示答案. 样例 样例输入1: 13 ...

  5. Vue学习日记(二)——Vue核心思想

    前言 Vue.js是一个提供MVVM数据双向绑定的库,其核心思想无非就是: 数据驱动 组件系统 数据驱动 Vue.js 的核心是一个响应的数据绑定系统,它让数据与DOM保持同步非常简单.在使用 jQu ...

  6. SQLite索引

    索引(Index)是一种特殊的查找表,数据库搜索引擎用来加快数据检索.简单地说,索引是一个指向表中数据的指针.一个数据库中的索引与一本书后边的索引是非常相似的. 例如,如果您想在一本讨论某个话题的书中 ...

  7. Java 里volatile关键字是什么意思啊?如何使用呢?

    一旦一个并发共享变量(类的成员变量.静态成员变量)被 volatile 关键字修饰就具备了可见性(即一个线程修改了一个变量的值对于另一个线程来说是立即可见的)和有序性(即禁止进行指令重排序),实质是在 ...

  8. windows spark1.6

    jdk1.7 scala 2.10.5 spark 1.6.1 http://spark.apache.org/downloads.html hadoop 2.6.4 只需要留bin https:// ...

  9. C++重写实践&与java的差异

    C++重写父类方法后,父类中同名的方法在子类中就无法被调用,回报这个问题: java中是没有这个问题的,显然java设计者在设计的时候有意解决了这个问题. C++实践代码: #include < ...

  10. 设置ubuntu14.04命令行启动

    编辑文件"/etc/default/grub",  把 GRUB_CMDLINE_LINUX_DEFAULT="quiet"  改成GRUB_CMDLINE_L ...