Codeforces Round #286 (Div. 1) B. Mr. Kitayuta's Technology (强连通分量)
题目地址:http://codeforces.com/contest/506/problem/B
先用强连通判环。然后转化成无向图,找无向图连通块。若一个有n个点的块内有强连通环,那么须要n条边。即正好首尾相连形成一条环,那么有了这个环之后,在这个块内的全部要求都能实现。
假设没有强连通环,那么就是一棵树,那么仅仅须要n-1条边就可以。
代码例如以下:
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
#include <time.h>
using namespace std;
#define LL long long
#define pi acos(-1.0)
#pragma comment(linker, "/STACK:1024000000")
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=100000+10;
int head[MAXN], cnt;
int dfn[MAXN], low[MAXN], scc, belong[MAXN], instk[MAXN], stk[MAXN], indx, tot[MAXN], top;
int vis[MAXN];
int flag, num;
struct node
{
int u, v, next;
}edge[MAXN<<1];
void add(int u, int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void init()
{
memset(head,-1,sizeof(head));
cnt=indx=top=0;
memset(dfn,0,sizeof(dfn));
memset(instk,0,sizeof(instk));
memset(tot,0,sizeof(tot));
}
void tarjan(int u)
{
dfn[u]=low[u]=++indx;
stk[++top]=u;
instk[u]=1;
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(instk[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
scc++;
while(1){
int v=stk[top--];
belong[v]=scc;
instk[v]=0;
tot[scc]++;
if(u==v) break;
}
}
}
void dfs(int u)
{
num++;
vis[u]=1;
if(tot[belong[u]]>=2) flag=1;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(vis[v]) continue ;
dfs(v);
}
}
int main()
{
int n, m, u, v, i, j, ans;
while(scanf("%d%d",&n,&m)!=EOF){
init();
while(m--){
scanf("%d%d",&u,&v);
add(u,v);
}
ans=0;
for(i=1;i<=n;i++){
if(!dfn[i])
tarjan(i);
}
m=cnt;
for(i=0;i<m;i++){
u=edge[i].u;
v=edge[i].v;
add(v,u);
}
memset(vis,0,sizeof(vis));
ans=0;
for(i=1;i<=n;i++){
if(!vis[i]){
num=flag=0;
dfs(i);
ans+=num-1+flag;
}
}
printf("%d\n",ans);
}
return 0;
}
Codeforces Round #286 (Div. 1) B. Mr. Kitayuta's Technology (强连通分量)的更多相关文章
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...
- 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/ ...
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...
- Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP
题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...
- Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)
数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...
- Codeforces Round #286 (Div. 2)A. Mr. Kitayuta's Gift(暴力,string的应用)
由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...
- CodeForces Round #286 Div.2
A. Mr. Kitayuta's Gift (枚举) 题意: 给一个长度不超过10的串,问能否通过插入一个字符使得新串成为回文串. 分析: 因为所给的串很多,所以可以枚举 “在哪插入” 和 “插入什 ...
随机推荐
- Java Secret: Using an enum to build a State machine(Java秘术:用枚举构建一个状态机)
近期在读Hadoop#Yarn部分的源代码.读到状态机那一部分的时候,感到enmu的使用方法实在是太灵活了,在给并发编程网翻译一篇文章的时候,正好碰到一篇这种文章.就赶紧翻译下来,涨涨姿势. 原文链接 ...
- U盘安装 CentOS 64bit (dell c6100, CentOS6.3, 64bit)
在淘宝买了一个server,dell c6100,64bit, 曾经系统是black apple.近期又买了一块企业级硬盘打算装CentOS. 综合各方面原因决定安装6.3版本号. 我參考了http: ...
- js保留两位小数的解决的方法
var a = 123.456; a = a..toFixed(2); alert(a);//结果:123.46
- python的range()函数使用方法
python的range()函数使用非常方便.它能返回一系列连续添加的整数,它的工作方式类似于分片.能够生成一个列表对象. range函数大多数时常出如今for循环中.在for循环中可做为索引使用.事 ...
- log4j日志存储到数据库
一.前提条件 系统必须是使用LOG4J进行日志管理,否则方法无效. 系统必须包含commons-logging-xxx.jar,log4j-xxx.jar这两个JAR包,XXX为版本号. 二.操作步骤 ...
- NEU 1040 Count
1040: Count 时间限制: 1 Sec 内存限制: 128 MB提交: 59 解决: 23[提交][状态][讨论版] 题目描述 Many ACM team name may be very ...
- vue --- 关于多个router-view视图组件,渲染同一页面
vue.js多视图的使用,可以提高网页组件化,模块化 比如使用多视图,可以将网站页面封装header.footer.navbar等多个公共部分, 遇到修改公共部分的文案信息等数据的时候,不再需要逐一修 ...
- UIimageView和UIimage的小区别
UIimageView 用来显示一张图片或者显示一组动画图片 UIimage 不是一个控件,只是一个普通的类,用来生成一张图片,只单纯的生成一张图片,图片只会被加载到内存,如果想要让用户 ...
- HTML文档 html,html5,css,css3
HTML 各种标签及简单应用: http://www.w3school.com.cn 1 <p><p> 2 <br/> 3 <hr/>横线 4 < ...
- 图片词典 Picture Dictionary
图片词典/可视词典 Picture Dictionary 某些 APP 又有新功能可以加入了.