codeforce 505 D. Mr. Kitayuta's Technology(tarjan+并查集)
题目链接:http://codeforces.com/contest/505/problem/D
题解:先用tarjan缩点然后再用并查集注意下面这种情况
‘
这种情况只需要构成一个大环就行了,也就是说不需要7条边只要6条就够了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int M = 2e6 + 10;
struct Edge {
int v , next;
}edge[M];
int head[M] , e;
int Low[M] , DFN[M] , Stack[M] , Belong[M];
int scc;
int Index , top;
bool Instack[M];
int num[M];
void init() {
memset(head , -1 , sizeof(head));
e = 0;
}
void add(int u , int v) {
edge[e].v = v , edge[e].next = head[u] , head[u] = e++;
}
void Tarjan(int u) {
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for(int i = head[u] ; i != -1 ; i = edge[i].next) {
int v = edge[i].v;
if(!DFN[v]) {
Tarjan(v);
Low[u] = min(Low[u] , Low[v]);
}
else if(Instack[v]) Low[u] = min(Low[u] , DFN[v]);
}
if(Low[u] == DFN[u]) {
scc++;
do {
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
}
while(v != u);
}
}
int f[M] , cnt[M] , flag[M];
int find(int x) {
if(x == f[x]) return x;
int tmp = find(f[x]);
return f[x] = tmp;
}
int main() {
int n , m;
scanf("%d%d" , &n , &m);
init();
for(int i = 0 ; i < m ; i++) {
int u , v;
scanf("%d%d" , &u , &v);
add(u , v);
}
memset(DFN , 0 , sizeof(DFN));
memset(Instack , false , sizeof(Instack));
memset(num , 0 , sizeof(num));
memset(cnt , 0 , sizeof(cnt));
memset(flag , 0 , sizeof(flag));
scc = 0 , top = 0 , Index = 0;
for(int i = 1 ; i <= n ; i++)
if(!DFN[i]) Tarjan(i);
for(int i = 1 ; i <= scc ; i++) {f[i] = i; if(num[i] > 1) flag[i] = 1;}
for(int i = 1 ; i <= n ; i++) {
for(int j = head[i] ; j != -1 ; j = edge[j].next) {
int v = edge[j].v;
if(Belong[i] != Belong[v]) {
int a = Belong[i] , b = Belong[v];
if(a != b) {
int t1 = find(a) , t2 = find(b);
if(t1 != t2) {
f[t2] = t1;
if(num[t2] > 1 || num[t1] > 1) flag[t1] = 1;
flag[t1] += flag[t2];
}
}
}
}
}
int ans = 0;
for(int i = 1 ; i <= scc ; i++) {
ans += num[i];
f[i] = find(i);
if(f[i] == i)
if(!flag[i]) ans--;
}
printf("%d\n" , ans);
return 0;
}
codeforce 505 D. Mr. Kitayuta's Technology(tarjan+并查集)的更多相关文章
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...
- LCA tarjan+并查集POJ1470
LCA tarjan+并查集POJ1470 https://www.cnblogs.com/JVxie/p/4854719.html 不错的一篇博客啊,让我觉得LCA这么高大上的算法不是很难啊,嘻嘻嘻 ...
- CodeForces 506B/505D Mr. Kitayuta's Technology
Portal:http://codeforces.com/problemset/problem/506/B http://codeforces.com/problemset/problem/505/D ...
- BestCoder冠军赛 - 1009 Exploration 【Tarjan+并查集缩点】
[题意] 给一个图,这个图中既有有向边,又有无向边,每条边只能走一次,问图中是否存在环. 最多10^6个点,10^6个无向边,10^6个有向边 [题解] 因为既有有向边又有无向边,所以不能单纯的用ta ...
- POJ3694 Network - Tarjan + 并查集
Description 给定$N$个点和 $M$条边的无向联通图, 有$Q$ 次操作, 连接两个点的边, 问每次操作后的图中有几个桥 Solution 首先Tarjan找出边双联通分量, 每个双联通分 ...
- hdu-2874 Connections between cities(lca+tarjan+并查集)
题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/327 ...
- [CF#286 Div2 D]Mr. Kitayuta's Technology(结论题)
题目:http://codeforces.com/contest/505/problem/D 题目大意:就是给你一个n个点的图,然后你要在图中加入尽量少的有向边,满足所有要求(x,y),即从x可以走到 ...
- 【CF505D】Mr. Kitayuta's Technology
题目大意: 在一个有向图中,有n个顶点,给出m对数字(u,v)表示顶点u和顶点v必须直接或者间接相连,让你构造一个这样的图,输出最少需要多少条边. 挖坑待填 官方题解链接:http://codefor ...
- 【codeforces 505D】Mr. Kitayuta's Technology
[题目链接]:http://codeforces.com/problemset/problem/505/D [题意] 让你构造一张有向图; n个点; 以及所要求的m对联通关系(xi,yi) 即要求这张 ...
随机推荐
- 【部分转载】:【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)、最长下降子序列模版】
二分 lower_bound lower_bound()在一个区间内进行二分查找,返回第一个大于等于目标值的位置(地址) upper_bound upper_bound()与lower_bound() ...
- H3C模拟器单臂路由配置实例
单臂路由:用802.1Q和子接口实现VLAN间路由 单臂路由利用trunk链路允许多个VLAN的数据帧通过而实现 网络拓扑图: RTA配置: SW1配置: PC1/2配置如图: 但是值得注意的是,在配 ...
- pytest
pytest可以生成多种样式的结果:1.生成JunitXML格式测试报告:命令: --junitxml=path(相对路径)2.生成result log 格式的测试报告: 命令:--resultlog ...
- linux CPU100%异常排查
1.top查找出占CPU比例最高的进程(5881): 2.查看该进程正在执行的线程: top -H -p 5881 3.将线程转换成16进制 printf ‘%x\n’ 5950 4.查看异常线程执 ...
- Java下载文件方法
public static void download(String path, HttpServletResponse response) { try { // path是指欲下载的文件的路径. F ...
- vscode c 语言 win10
在看 CSAPP 一些课程,一些c 语言的小程序的例子,想跑起来试试,用一个DEV c++ 简单上手,但这是一个上古的IDE, 前端开发中的代码不全,语法高亮,都不太好,就想着为什么不折腾一下 V ...
- Python递归函数,二分查找算法
目录 一.初始递归 二.递归示例讲解 二分查找算法 一.初始递归 递归函数:在一个函数里在调用这个函数本身. 递归的最大深度:998 正如你们刚刚看到的,递归函数如果不受到外力的阻止会一直执行下去.但 ...
- 基于.NET Core开发的个人博客发布至CentOS小计
早些时候,使用 .NET Framework 开发网站,只能部署在 Windows 服务器上面,近两年 .NET Core 如火如荼,乘此机会赶紧上车,最近将自己利用 .NET Core 开发的个人博 ...
- oracle 正则表达的使用
最近遇到有个项目,需要根据文件存储的根目录地址来判断是在云端获取,还是本地获取, 先看下具体有几个不同的根目录: , , 'i') from pmc.designmaterial d 去重关键字:di ...
- 5 X 5 方阵引出的寻路算法 之 路径遍历(完结)
此篇文章源自对一个有趣问题的思考,在我的另一篇博文<一个有趣的 5 X 5 方阵一笔画问题>中有详细介绍.在已知其结论的情况下,作为程序员的我,还是想利用该问题当做出发点,写一个可以遍 ...